What are the 4 pillars of OOP?

7 min read

In most of the interviews, candidates will be tested on their knowledge of programming languages. Among them, Java is one of the top-resourced languages from which the interviewers tend to ask questions. 

While the concept of Java can be explained easily such as Java benefits, loop types, and such, there are other big concepts that help one qualify completely in this topic.

Specifically, we’re talking about Object-oriented programming (OOP), which is the core base of the Java language. Object-oriented programming is a format that depends on classes and objects. If aspiring programmers don’t have a thorough understanding of the OOPs concept, then it can be troublesome at Java-related interviews, since questions will be mostly asked related to OOPs. 

And that is why, we’re going to give you a guide about OOP for your learning.

Speaking of OOP, there are four important pillars that have to be known since it is based on these four, the Java code runs. 

Without much ado and to enrich your knowledge, get ahead and cover yourself in our detailed blog about OOP pillars and why it’s necessary.

What is Object-oriented programming?

In software development, Object-oriented programming is a main aspect that uses objects to construct computer or mobile phone programs. By using OOP, errors can be reduced and the development process can be sped up. Not only can Java use this type but as C++, Python, etc can make use of OOP. 

To understand this concept better, let’s take an example. Consider a car as an object. Its properties would be size, color, etc. When it comes to the car’s behavior, the actions of engine starting, accelerating, etc, would be considered.. Now to combine all this so a user can interact, we inculcate Object-oriented programming.

Under this, there are four pillars that should be studied well to clearly understand its functioning. Let’s read about them in detail. 

Four pillars of Object-oriented programming

Pillars or otherwise commonly considered the principles of Object-oriented programming help in determining the result in a way that’s convenient, fast, and errorless. The four pillars are:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Abstraction

Abstraction is the most important pillar as it helps in hiding what is working inside so that the user gets to see only the needed features and services. For example, let’s take an ATM machine. The machine has mechanisms inside it that the user won’t see. Rather they can only put in their card and withdraw money.  As a user of course we will not want to see how the machine gives us the money but we need only the services it offers. 

Likewise, in programming the user gets to see only the needed program rather than the entire code. In Java, we tend to use the keyword “abstract” which applies this principle to the particular class we’re working on. This word is called a Super keyword in Java and they make the process simple. 

Encapsulation

Encapsulation, as the name suggests, encloses or binds the information, its features, and processes into a single unit. Inside classes, we have information and its attributes which will be merged into a single one to make sure the data is secured well. This security of data can be done by data hiding as well. Hence encapsulation is nothing but the combination of both abstraction and data hiding. 

What is data hiding?

This simply means restricting access to a particular data by hiding it. To activate this in the Java compiler, we use keywords like private that makes the data accessible only to those who are allowed. 

Let’s take a real-life example to understand this. If we have a fever, we will tend to buy a tablet right?

 Inside a capsule tablet, there will be various medical compositions that we’re unaware of but we know this tablet is for fever. This merge of compositions is called encapsulation and the items that are merged are hidden inside the capsule (data hiding and abstraction). 

Inheritance 

Inheritance simply means inheriting or deriving attributes of a class for another new one. Let’s say there is a newly married couple. When they have a baby, it will inherit the gene of the parents. This is similar to what happens in coding. We call the initial class as parent and the other classes that inherit its properties as children. 

Another interesting example would be mobile phones. Mobile phones are used for various needs like social media, camera, dialing, etc. Now the parent class is mobile phones and the apps inside are children classes since they inherit properties from the phone. 

In Inheritance, there are types that we usually make use of in the Java compiler where we code a program. The four forms are,

  • Single: Only one inherited class will be there.
  • Multiple: More than one children class is there.
  • Multilevel: When there are more than one parent class.
  • Hierarchical: When multiple children's classes get derived from it’s base class (parent) in an order from old to new.

Polymorphism

Poly means many and morph means types. Hence the principle denotes that it is of multiple types. This principle states that any object we make use of has different types and this is because of polymorphism. 

For example, let’s say we want to purchase a mobile phone. There are different companies like Apple, Samsung, etc that produce it. When we buy a mobile phone, say, from Samsung, do we tell others that we’ve got a Samsung mobile phone or simply just a mobile phone? Definitely the second one right? Because irrespective of which brand we got it from, a phone is a phone.

This aspect of generalizing is what we call polymorphism. It’s the same object but different types are available. 

This is also called generalization since we call any mobile phone, irrespective of its brand as a mobile phone since they all inherit the properties of what a smart mobile would have. 

In OOPs, we have two types of polymorphism, and they are:

Compile Time Polymorphism

Also known as Static binding, this type makes use of method overloading, a process where more than one method is defined with identical names but with different rules. 

Runtime polymorphism

This type follows the method overriding process where more than one method is defined with identical names, and rules but in various classes. Firstly the child class will override the method of the parent class. This means the child class will dominate the parent class. 

Final Thoughts

We hope all your doubts regarding Object-oriented programming and it’s four principles are cleared up. Remember to practice Java coding using free Java compilers that are available online.

Additionally, ensure that you make use of the Super keyword in Java which will make the processes much efficient and faster. If you wish to learn more coding-related topics, do visit our website.

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up