Four Fundamental Object-Oriented Programming Concepts in Java

Encapsulation, abstraction, inheritance and polymorphism. Those are four primary Java OOP concepts. Understanding them can help us understand better how Java works. Those words can be intimidating we you first saw them, but it is not difficult to understand once we see some examples.

Encapsulation

Encapsulation is binding variables and methods together. In encapsulation, the variables of a class are private, other classes can only access they (modify and review the variables values) through the public getter and setter methods you provided. So encapsulation is also known as data hiding.

   private String name   public String getName(){
return name
}
public void setName(String inputName){
name = inputName
}
}public class testEncap{ public static void main(String args[]) { Encap a = new Encap()
a.setName("Tom")
System.out.print("Name : " + a.getName())
}}//Output:
//Name: Tom

Abstraction

Abstraction is the process of hiding the implementation details from the user. It only shows the user what the object does but not how it does it. It hides all the mess from the user.

For example, when you send a text to your friend, you do not have to worry about establishing a network connection with your friend’s phone, or finding the IP address to determine where to direct the message. You simply have to type your message and hit send.

Inheritance

When one class (child class, subclass) extends another class (parent class, superclass), the child class acquires the properties (methods and fields) of the parent class.

For example, class P has functions getSum() and getDifference(). If class C decides to extend class P, class C automatically carries the functions getSum() and getDifference() without explicitly implementing those functions inside class P.

Polymorphism

Polymorphism is similar to inheritance, but now we can rewrite/reimplement the functions that are created from the parent object to the child class.

For example, class Shapes has getSurface(), we have Triangle , Circle , and Rectangle classes. When they extends class Shapes , each of them can have their own version of getSurface() .