DEV Community


Posted on

Encapsulation, abstraction, and polymorphism in Java OOP. (Vahagn Mesropyan)

Understanding the Pillars of Java OOP

Java Object-oriented programming concepts let us create working methods and variables and then re-use all or part of them without compromising security. Grasping OOP concepts is key to understanding how Java works. Java OOP has four pillars: inheritance, encapsulation, abstraction, and polymorphism.
In this blog post, we will discuss three of them which are encapsulation, abstraction, and polymorphism.


Imagine you have a magic box that not only holds your belongings but also comes with a set of rules on how to use them. In Java, this magical box is "encapsulation". It involves bundling data and methods into a single unit, like a class, and setting rules on how the outside world can interact with it. This helps in keeping related things together and hides the inner workings, making our code more organized and less prone to unintended interference.

For example, if you have a class representing a car, encapsulation ensures that the details of the car, like its speed or fuel level, are accessed and modified through well-defined methods. This way, you don't accidentally set your car's speed to a negative value or mess wtih its internal mechanisms directly.


Abstraction is like reading a book without knowing the intricacies of printing and bookbinding. In Java, abstraction allows us to focus on the essential features of an object while hiding the complex implementation details. It simplifies the way we interact with our code and provides a clear and understandable interface.

Consider a smartphone. All of us use it to make calls, send messages, and take pictures without worrying about the underlying hardware and software stuff. Similarly, in Java, abstraction lets us create classes and methods that are easy to understand and use without burdening us with unnecessary details.


Polymorphism is the ability to use a single name or method in different contexts. It's like having a universal remote control that works for various TVs. In Java, polymorphism enables objects of different types to be treated as objects of a common type. This allows us to write code that can work with different objects using a shared interface.

Let's take a music player with different types of audio files. Regardless of the file format, you can use the same play button to enjoy the music. In Java, polymorphism allows us to design code that adapts to anny scenario, making it easier to maintain and use.

In conclusion, encapsulation, abstraction, and polymorphism form the foundation of Java OOP. They help developers to create code that is not only efficient but also easy to understand.

Top comments (0)