Java is an object-oriented language. We have learned about class and objects. Now we will learn the object oriented principles of Java in brief.
The main four principles of Java are Polymorphism, Inheritance, Encapsulation, and Abstraction.
First, let us talk about Encapsulation.
Encapsulation is basically a mechanism that binds the code and data, and keep both safe from outside misuse.
You have already seen that a class can have two members within it, the instance variable and method. Obviously you don’t want that your class members can be misused from outside. Let us say we have class Box like below:
public class Box
// private instance variable
private double height;
private double length;
private double width;
private double volume;
public Box(double height, double length, double width)
this.height = height;
this.length = length;
this.width = width;
public static void main(String args)
Box boxObject = new Box(10, 12, 15);
boxObject.volume = getVolume(boxObject.height, boxObject.length, boxObject.width);
System.out.println("The Volume of the box is " + boxObject.volume + " cubic unit");
private static double getVolume(double height, double length, double width)
double volume = height * length * width;
As you can see the four instance variable and the method getVolume has their access modifier set as private. So any other class cannot access these member from outside. That way Box class hides it member from outside view. Java implements encapsulation through class.
Of course, a class cannot always hide all of its members; doing so will make that class useless to all other classes. But if there is something to hide, class can do it.
You will know about other access modifiers of Java in the Access Modifier chapter.
Suppose that you are thinking about the classifications of Vehicle. Vehicle can be broadly classified on the basis of number of wheel. So there are three main sub divisions of Vehicle: 4 wheeler, 3 wheeler and 2 wheeler. Each of this can be divided into two divisions as Petrol vehicle and Diesel vehicle.
Now a Vehicle has some property, like it has engines, doors, chassis, gear, wheels etc. The 4, 3 and 2 wheeler car gets these properties from their parent and set their own values to wheels property. Same logic goes for 2nd level division. The petrol and diesel car of 4 Wheelers get all properties from 4 Wheelers and set their own values engine type variables.
Java implements this above mentioned features with Inheritance. That means a subclass will access all the property from its superclass and in addition can have its own properties as well. Consider the structure below:
public class A
// instance variable
public double parameter1;
public double parameter2;
private double parameter3;
public class B extends A
// instance variable for class B
public String abc;
Now you can see class B extends class A. That means it is defining itself as a child of class A. (extends is another operator of Java which is used to inherit a class. More on this will be discussed in the details of Inheritance)
So class B can access the parameters (instance variable) of class A. But can it access parameter3? You guessed it right, it cannot, because parameter3 is defined as private. So combining Encapsulation and Inheritance we can say:
- class B is subclass of class A.
- class B can use instance variable parameter1 and parameter2 as its own instance variable.
- class A has chosen to hide parameter3 from outside, so class B does not have access to it.
- class B has its own defined instance variable ‘abc’.
The literal meaning of the Greek word Polymorphism is ‘many forms’. Consider the scenario of Vehicle and its divisions. The engine of each every car is different, so each car will want to design its engine on its own way. Say every car does this with a general method designEngine. Even though the basic output of the method is same, a well-designed engine, but the underlying implementation is different.
Java provides this feature with the help of Interface, method overloading and method overriding.
We are going to see the structure of Interface here. Method overloading and overriding will be covered later, when we will examine Java method in more details.
Consider the program structure below (it may seem abstract now, but we will discuss this kind of program in detail later.) For now just know that Interface is another type of template java provides which can be implemented by multiple class.
public interface CarDesign
public void designEngine();
public void designSeats();
First class to implement CarDesign interface
public class HondaCity implements CarDesign
// method from CarDesign interface
public void designEngine()
Another class implementing the same Interface
public class MercedezsBenz implements CarDesign
// method from CarDesign interface
public void designEngine()
As you can guess that engine design will be different for HondaCity and MercedesBenz, so they have their own implementation in their respective class. So the same designEngine() method is implementing in different way. This is call one method, many forms, or in one word: Polymorphism.
Polymorphism, Inheritance and Encapsulation work together in a robust and scalable code.
To say simply, abstraction is the mechanism to hide complex implementation details or the details which is not relevant. This is not a property of Object Oriented programming language alone, any structural language provides it. Suppose that you know to drive Car. You can do it with the knowledge of handling gear, breaks and a tank filled with petrol. You do not need to know the mechanical details the car have inside its complex machinery. In the same say Java provides multiple level of abstraction. Such that to call a method, I only need to know what parameter(s) I need to pass to the method and what output I can get. May be I have not designed the method, my friend has done it, I just want to use it. So the method details are abstracted from me, because simply I do not need to know. Also java has features like Abstract Class and Interface to implement abstraction.
In a brief, java provides five main implementation way to support all object oriented principles, those are: class, object, inheritance, interface and package.