OOP's Concepts

 OOP's Concepts:

  1. Class
  2. Object
  3. Encapsulation
  4. Inheritance
  5. Polymorphism
  6. Abstraction

1. Class:

             Class is a collection of objects. Class is a blueprint which defines some properties and behaviors. It is logical entity.

2. Object: 

            Any real world entity which has behavior and state known as Object. It is logical or physical entity.

For Ex. chair, book, computer, bottle etc.

3. Encapsulation:

            It is a process of wrapping up of data or binding up of data into a single unit, known as encapsulation.

For Ex. Capsule(wrapped with different medicines).

            It is a process of making fields as private and providing access to those fields with the help of public methods that is through setters and getters methods.

4. Inheritance:

            Acquiring the properties of one class into another class, known as Inheritance.

            It we want to achieve inheritance we need to use either extends or implements keyword.

                            ClassA                                        ClassB

                            Parent class                                Child class

                            super class                                  sub class

                            Base class                                   Derived class


   Keypoint about Inheritance:

  • We can hold parent class object with parent class reference  and with that reference we can call only parent class methods.
    • For Ex.- ClassA obj=new ClassA();// Call parent class
  • We can hold a child class object with a parent class reference and with that reference we can call only parent class methods.
    • For Ex.- ClassA obj= new ClassB();
  • We can hold a child class object with a child class reference and with that reference we can call both parent and child class methods.
    • For Ex.- ClassB obj=new ClassB(); //call parent and child class methods.
  • We can't hold a parent class class object with a child class reference. We will getting the compile time error.
    • For Ex.- ClassB obj= new ClassA(); // Compile time error.
Types Of Inheritance:

5. Polymorphism:
            It is an ability to perform multiple task with the same identity.(Same method but functionality different), known as Polymorphism.
Polymorphism is a GREEK word, in which poly= many and morph = forms. It means many forms.
        Types of Polymorphism:
  • Method Overloading: Writing two or more methods in the same class having same method name but different parameters.
  • Method Overriding: Writing two or more methods in two different classess having same method name, same parameters and same return type also.
6. Abstraction:
            It is the process of hiding the implementation details from the user & showing only necessary details to the user.
Abstraction can be achieve in two ways:
  1. Abstract Class
  2. Interface
a. Abstract Method:
            1. A method which is declared as abstract with 'abstract' keyword known as Abstract method.
            2. An abstract method always end with semicolon.
            3. For an abstract method there will be any method body.
            4. Implementation for an abstract method be given in the next class by using method overriding.

b. Abstract Class:
            1. A class which is declared as abstract with 'abstract' keyword is known as Abstract class.
            2. Inside an abstract class with both normal and abstract methods.
            3. Writing abstract methods in abstract class it is not mandatory, it is always optional.
            4. If we are writing an abstract method in a normal Java class then 100% we need to make that                    class otherwise we will be getting an compile time error.
            5. Inside an abstract class we can write normal methods, abstract methods, static methods, main                 methods and even constructor also. 
            6. Abstract class can't be instantiated(we can not create an object for abstract class).
            7. If we are inheritating an abstract class and if that abstract class is having abstract method then                 in the child class is having abstract method then in the child class 100%. We need to provide                 implementation for the abstract class otherwise we will be getting compile time error.
            8. In the child if we don't want to provide implementation for the abstract method which is                         present in the parent class then in order to resolve the compile time error we need to make                     the child class also as abstract.
        
       



Comments

Popular posts from this blog

Basics about Core Java

Data types and Variables