4 Pillars of Object-Oriented Building

Object-oriented programming is a way of writing computer programs(list of instructions to the computer) using the idea of “objects” to represent data and methods.

Bhavik Savaliya
The Startup
4 min readAug 16, 2019

--

Class

Class is the blueprint of the object.

Object

An object is a real-world entity Or The object is an instance of a class.

Why OOP is Important?

By using OOP you can easily reuse your program, Maintain Scalability, Modular structure and It also helps us to manage the complexity of software systems.

OOP is standing with 4 pillars

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

1.) Inheritance

Inheritance is a mechanism in which one object acquires all the properties(states of objects) and behaviors(methods of objects) of a parent object. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. It is used for code reusability

Example of Is-A relationship(parent-child relationship),

  • A Bus is Vehicle, In other words, the bus is a child and Vehicle is a parent. or Bus can use properties and behaviors which is defined in Vehicle.
  • A Car is a vehicle. In other words, a car can use properties and behaviors which is defined in Vehicle.

Note: Multiple inheritances are also possible but most of the languages like Java, Javascript, etc doesn’t support Multiple inheritances because of Diamond Problem.

Java Inheritance Code Example

2.) Encapsulation

Encapsulation is a mechanism of wrapping the properties(states) and code acting on the behaviors(methods) together as a single unit like a capsule. In encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class.

To achieve encapsulation,

  • Declare the properties of a class as private so other class can not access directly.
  • Provide public setter and getter methods to modify the properties of a class.
  • it is also known as Data hiding.

Java Encapsulation Example

3.) Polymorphism

Polymorphism (“having multiple forms”) allows us to perform a single action in different ways. A real-life example of polymorphism, A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.

In Java polymorphism is mainly divided into two types

  • Overloading (Compile-time Polymorphism, static polymorphism)
  • Overriding (Runtime Polymorphism, Dynamic Polymorphism)

Overloading

When there are multiple methods(behaviors) with the same name but different parameters then these functions are said to be Overloading. Functions can be overloaded by the change in a number of arguments or/and change in the type of arguments.

Java Overloading Example

Overriding

When child class has a definition for one of the member methods of Parent class that method is said to be overridden.

For example,

Thre is an “RBIbank” Parent class and it has getInterest method that will return “18%”. Now there are other classes such as Abank, Bbank and they extending RBIbank Class and Abank and Bbank have different interest rate so both classes will override a getIntetest method pf RBIbank and give his definition.

Java Overriding Example

4.) Abstraction

Abstraction is a process of hiding complexity from the user and shows only functionality. A real-life example of Abstraction, If you are going to ATM for withdrawing money and ATM is showing you background processes like server connection, checking amount, etc. Is required for you? No, so Abstraction comes in picture.

Java Abstraction Example

So If you are using “String.valueOf()” methods to convert any value into String so You don’t need to know how Java convert your value to String. You need the only output.

I believe if you understand four pillars of OOP, then you can build a reusable, scalable, Optimized and Flexible solutions.

Happy Coding!

--

--