According to object oriented paradigm, there are four principles namely:
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
Abstraction:
Hiding the complex details and presenting only the general necessary details is known as abstraction. For example, when you go to a car dealer to purchase a car, he/she explains you about the features of the car like: mileage, type of engine, color, details about interior and exterior etc. But, the car dealer never explains about how the engine is built, which nuts and bolts are used build the car etc. The later details are not useful for the buyer. Abstraction is like a concept/theory and encapsulation is the practical implementation of abstraction.
In java, abstraction can be implemented using abstract keyword and interfaces.
Encapsulation:
Wrapping or combining code (methods) and data (instance variables) together is known as encapsulation. Encapsulation provides abstraction. A common side effect of encapsulation is information hiding. For example, there is no need for a person to know about the internal working of a car to drive it. A person interacts with the car through its interface like driving wheel, gear, buttons etc.
In java, encapsulation is implemented using the class concept.
In java, data/information hiding is implemented using the access specifiers.
Polymorphism:
Simply can be defined as many forms or single interface multiple implementations. Polymorphism gives the ability for an object to perform multiple behaviors through a single interface. A real world example for polymorphism is the animal chameleon. Based on the environment, a chameleon can change its body color to save itself from the predators.
In java, polymorphism can be implemented using overloading and overriding.
Inheritance:
Defined as deriving properties and behavior from one class (parent) to another (child) class. For example, consider a general class laptop (parent). All the laptops will have common properties like a screen, keyboard, touchpad etc. and behavior like: laptop starts on the push of a power button.
Now, consider a sub class or child class of laptop like a gaming laptop. A gaming laptop (child class) will have all the properties and behavior of a laptop as mentioned above. In addition, the gaming laptop can have its own properties and behavior.
In java, abstraction can be implemented using the keywords extends and implements.
Note:
If in an interview, you are asked the question, what are the three object orientation principles, you can say:
Encapsulation, Polymorphism and Inheritance.