i

JAVA Complete Course

The Three OOP principles

Encapsulation is one of the OOP main principles. It is wrapping or binding code and data into a single unit. We can imagine a medical capsule, this is one of the example of encapsulation in which different medicines are wrapped. A java class is an example of encapsulation. Java bean is the fully encapsulated class as it has all the data members private.

Inheritance is the OOP principle which allows the children classes to use the properties and methods from parent classes. Inheritance helps us to make our code reusable. It is also used to achieve runtime polymorphism.

Another main OOP principle is polymorphism. To say shortly, it is when one task can be done in different ways. In Java, in order to achieve polymorphism we use method overloading and method overriding. Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. Let’s revisit the example:

In this example Deer class is polymorphic as it has multiple inheritance. All IS-A tests are passed here:

A Deer is a Animal

A Deer is a Vegetarian

A Deer is a Deer

A Deer is a Object