OOP's Concepts
OOP's Concepts:
- Class
- Object
- Encapsulation
- Inheritance
- Polymorphism
- 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.
- 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.
- Abstract Class
- Interface
Comments
Post a Comment