4 Pillars of OOPs in Java

As programmers work with real-life entities, object-oriented programming allows programmers to achieve the same. Objects are used to store knowledge, data, task, state, and other different OOP tasks and methods. 

This article is going to explain the fundamental four pillars of object oriented programming in java.

4 Pillars of OOPs in Java

Image Source

1. Encapsulation

Encapsulation is achieved when the object of the class maintains the private state. Other objects are not allowed to access these objects; despite that, the other objects can only access or invoke the list of public functions.

In simple words, encapsulation means wrapping the data, methods into the class, i.e., into a single unit. Encapsulation automatically accepts the data hiding by making variables private and access them with the help of methods that are public. Access modifiers are used to accomplished data hiding.

Example: 

Output:

My age is 21

Here, the age field is private, so it cannot be accessed from outside the class. To access age the methods getAge() and setAge() is used. By declaring age as private, it allows restricting any unauthorized access from outside the class. If the main class wants to access age, the output will show the error. Encapsulation also provides control over data. 

2. Abstraction

Abstraction in OOP is used to collect more detail and show the only appropriate amount of the object’s attributes. The primary purpose of Abstraction in OOP is to hide unnecessary information from the outside world. That helps in reducing the effort and complexity in programming. Fetching, removing, and selecting the user information is known as Abstraction. Programmers can use the same data and information for other instances and programs with a few changes or no modifications.

3. Inheritance

The ability to take complete or some characteristics of one object by other objects is known as inheritance. Reusability in inheritance is a significant advantage. 

Example:

Consider a car class with properties like price, fuel type, color, capacity and methods serprice(), setfueltype(), setcolor(), setcapacity() getprice(), getfueltype(), getcolor(), getcapacity(). 

The child class with the name sportscar has all methods and properties of the parent class and has some extra methods and properties and refers to the child class.

The sportscar class inherited all the class car properties, so there is no need to specify the sportscar class’s methods and properties. 

The data reusability of car class is there for sportscar class.

4. Polymorphism

This feature leads us to use the same function with different arguments. And that function can perform differently.

It mainly uses when the reference of a class is used to refer to the child class. A subclass can have its unique features and still share some characteristics with its parent or base class. But this condition is not for the parent class. Parent class cannot have the behavior of child class.

Leave a Reply

Your email address will not be published. Required fields are marked *