top of page

Pillars Of Java

This blog will tell you about What are Pillars Of Java, How many Pillars are there. This blog will give explanations for Pillars with syntaxes and examples, Advantages and Notes also.


Java is an Object - oriented Programming Language(OOP). Here Objects contains both data and methods.

. OOP is faster and easier to execute.

. OOP provides a clear structure for the Programs.

. OOP helps to keep java code "DRY" (Don't Repeat Yourself) .

. OOP makes it possible to create full reusable applications with less code and shorter development time.


"Don't Repeat Yourself" principle is for reducing the repetition of code. You should extract out the codes that are common for the application, and keep them in a single place and reuse them instead of repeating it.

Four Pillars Of Java

There are four Pillars in Java. Those are

1. Encapsulation

2. Polymorphism

3. Abstraction

4. Inheritance


Meaning for encapsulation in dictionary is "the action of enclosing something in or as if in a capsule". The same way in java it will hide the sensitive data from users.

Encapsulation is also known as “data hiding”.

For encapsulation we have to follow these

. Declare class variables/ attributes as private.

. Provide public get /set methods to use and change the value of the private variables.

What is getter and setter?

Getters and setters are used to protect your data, particularly when creating classes. By convention, getters start with the word "get" and setters with the word "set", followed by a variable name.

Getters and setters are also known as accessors and mutators.

We know that private variables only can accessed within that declared class. we cannot use that variable outside of that class.

We can able to use that private variable in other classes when we provide public get and set methods for that.

Syntax :

Both get and set method start with either get or set and followed by the name of the variable and first letter of the variable's name is capitalized.

Example :

Advantages :

. Better control of class attributes and methods.

. Getters and setters allow you to control how important variables are accessed and updated in your code.

. class attributes can be made read only or write only if you only use get or set method.

. Because of this flexibility Programmer can easily change one part of the code without affecting other parts.

. Increased security of data.

Note :

get method returns the variable value.

set method sets(assigns) the variable value.

2. Polymorphism

Polymorphism is a Greek word that means "many-shaped" or "many- forms".

Polymorphism comes from a combination of two Greek words: "poly" meaning many and "morph" meaning form. Therefore, polymorphism enables methods to take on many forms.

Java OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables.

What is Method Overloading?

In method overloading, multiple methods can have the same name with different parameters.

Method overloading demonstrates compile-time polymorphism. Compile-time polymorphism means that the Java compiler binds an object to its functionality at runtime. The compiler checks method signatures to achieve this.

This type of polymorphism is also known as static or early binding.

Example :

What is Method Overriding?

In method overriding, multiple methods(Superclass method &Subclass method) can have the same name .

Method Overriding is a feature using which we implement runtime polymorphism.

In method overriding, a method of the parent class is overridden in the child class. This means, that the method prototype in both super and subclass remains the same but the implementations are different.

When overriding a method, you also need to be mindful of the access modifier used.

Example :

Advantages :

. It is useful for code re-usability.

. Reuse attributes and methods of an existing class when you create a new class.

. Method overriding and overloading are important for code simplification, and simple code is good practice.

. Flexibility.

Note :

In method overloading multiple methods can have the same name but the parameters should be different.

In method overriding multiple methods can have same name and same parameters.

3. Abstraction

Abstraction means using simple things to represent complexity.

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Abstraction can be achieved with either abstract classes or interfaces.

Abstract class is declared with abstract keyword.

abstract keyword is a non-access modifier, used for classes and methods.

Abstract class is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

Example :

Advantages :

. It is useful for code re-usability.

. Security

. hide certain details and only show the important details of an object.

. Abstract can have both abstract and non abstract methods.

Note :

Abstract method can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Abstract class can have both an abstract as well as concrete methods.

4. Inheritance

Inheritance means adopt the Properties from Parents to Kids.

In Java Inheritance concept acquires the properties from one class to other classes .

It is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

  • subclass (child) - the class that inherits from another class

  • superclass (parent) - the class being inherited from Inheritance is another

It works by letting a new class adopt the properties of another. We call the inheriting class a subclass or a child class. The original class is often called the parent. We use the keyword extends to define a new class that inherits properties from an old class.

Example :

Advantages : . labor-saving Java OOP concept.

Note :

Multiple inheritances are not allowed by Java.

Conclusion :

Pillars of Java are software design principles to helps us to write clean Object- Oriented Code.

I hope this blog has explained what are the four pillars of Object Oriented Programming are, and how they lead to cleaner and more robust code.

Thank you For reding my blog.

91 views0 comments
bottom of page