OOP Principles in java

The four principles of object-oriented programming in java


3 min read

OOP Principles in java


Java is object-oriented. what does that mean? Unlike other languages, that focus on "Do this/ Do that". But Object-oriented languages focus on data. Object-oriented languages are better than those languages because it helps to organize the data, modify the data and you can build what you already have rather than start again from scratch. A famous barry burd the writer of Java for Dummies book said "Java is A Great Object-Oriented Language". In this blog, we can see about the four oop principles Inheritance, Encapsulation, Abstraction and Polymorphism in Java.


Allows you to define a new class from an existing one and inherit its attributes and methods. Let's assume we have many cars in our project like Tesla, Audi, Toyota and so on. We create clusters for every model. But the problem is they have many common functions and attributes which causes code duplication. It means that if you like to change a common function, you need to change it everywhere. That's the problem inheritance can solve.

How to use inheritance:

If we would like to inherit attributes and methods from a class, we need to define the class first. An example is:


Wrapping up data in a single unit(a class) and prevents data from being accessed by the code outside. (i.e.) Information(data) hiding. Assume there are situations you don't want your data to be modified by another class after object creation. For example, is the price of the car you want to use to print out but you don't want to modify that's where encapsulation use to solve. Using classes to encapsulate data and using access modifiers to hide them. In encapsulation methods are used to control read-only and write-only data access.

  1. use get - read-only

  2. use set - write-only


Only the essential details are displayed to the user and hiding certain parts of the implementation. (i.e.) implementation(detail) hiding. Assume when you start a car, you don't know anything about the inner parts but you slip in the key or push the button and that's it. car is running and ready to go. you don't have to know anything about the engine how the air and fuel are mixed how the oil pump. That's what abstraction does. Hides the detail from the user.

Abstract Class:

The abstract class can have both abstract method and regular methods. They cannot be used to create an object. Confusing? Then why do we need the abstract class then? it has in the definition itself, To hide the details from the user. To Access the abstract class, it must be extended by another class.

Abstract Method:

The abstract method can only use in an abstract class. It does not have a body. In the abstract method, the body is provided by the subclass. All the abstract methods must be defined to create objects.


Polymorphism allows you to have many forms with the same purpose. (i.e.) poly(many), morphism(form). It has two types:

  1. Compile-time polymorphism (method overloading)

  2. Run-time polymorphism (method overriding)

Method overloading:

Creating a new function with the same method name (same method name, different parameters, different return types).

Method overriding:

Creating a new body for the inherited functions. It has the same method name, same parameters, and same return types only the body is different. Private methods can't be overridden and methods are in a different class.


Thank you, We learned about the four principles of OOP in Java. Through my blog posts, I aim to provide valuable information and practical tips. Feel free to leave comments, share your thoughts, and connect with me. For more future blogs follow me and also connect with me on LinkedIn.