4 Pillars Of OOP With Real World New Examples

Asiri Gunasena

Published:

Object Oriented Programming

4 pillars of OOP

Before moving on to the 4 Pillars of OOP I would like to add a small recap on what is Object-Oriented Programming (OOP). OOP is one of the famous techniques when programming. It is developed on a famous concept called objects.

The concept behind the OOP is to map programming objects with real-world objects, things, or concepts. Let’s try to understand OOP with small examples.

Object-oriented programming out looks just like building blocks. Consider we want to build a house. We can consider the house as a single object once completed the construction of the building process.

But if you think about the structure of the house it’s built on a collection of different objects like windows, doors, walls, etc. In the same way, windows are built using Glass, Iron frames, etc.

oop house
OOP Real World Example- House

We use the same mechanism for Object-oriented programming. Applications are built with Objects but that object contains other inside objects. When comes to the OOP programming real-world every tangible item or intangible things consider an object, and those objects contain different types of behaviors, attitudes, and characteristics.

What are the 4 pillars of OOP?

The 4 Pillars of OOP are well defined and those are the base concepts of OOP. The four pillars of object-oriented programming are

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

4 pillars of oop
4 Pillars of OOP

1. Encapsulation

Encapsulation in Object-Oriented Programming is everything (data-attributes / behaviors- method or function) combining and keeping together as a single unit. Encapsulation is used in the implementation time in OOP. Data hiding is part of the encapsulation.

Let’s take a car as an example. Now consider the implementation of the car. There are components like an accelerating paddle, steering wheel, and gear-changing handle for control of the car. Those are the access points of each unit but others are compacted into a single unit with limitations.

When considering the limitation of the car There is an upper and lower level limit for speed when accelerating. Also, gear levels are selectable.  The steering wheel has maximum rotation. That means all the above access points for the user but there are limitations and validation when implementing the car.

OOP Encapsulation
Required points access by User in cars: OOP Encapsulation

According to the example, we can see everything bundled into a single unit with user access points. All validation is done inside the car according to the user input. That will help to improve the security and safety of the car.

If you like to see this example with source code and a very descriptive way you can see our Encapsulation article.

2. Inheritance

Inheritance in OOP is acquiring all the behaviors and attributes from one another. This means one class’s properties are derived from another class.

To complete inheritance successfully, there should be a minimum of two classes. Because inherit things with hierarchy. Those are called Superclass and Subclass. The subclass class is known as the child class and inherits property from Superclass. Superclass is also known as Parent class or base class. Things are inherited from superclass to subclass.

After creating an inheritance there is create a relationship between them. It’s called IS A relationship. As an example, when inheriting BMW from the basic car there is a relationship between BMW IS A Car. Likewise, a Physician IS A Doctor.

Let’s move on to a real-life example of inheritance. My example is dogs and birds are inherited from animals. When it’s come to example animals have common attitudes and behaviors including eating and sleeping. 

Oop Inheritance
Animal Things inherited with each type of Animal: Oop Inheritance

Those common attitudes and behaviors are inherited by dogs and birds. But dogs have special types of attitudes and behaviors such as barking and sensing smell. Also, birds can fly. Consequently, dogs and birds have all the behaviors that animals have. Dogs can Bark, eat, sleep, and have a smell sense ability likewise birds fly, eat, and sleep.

According to the animal example inheritance refers to acquiring everything from parent to child. Likewise, OOP inheritance also refers to the same concept.

3. Polymorphism

Polymorphism in Object-Oriented Programming name describes the meaning. Poly into Many and morphism into Form. Then Polymorphism easily says a single class or object’s ability to play as many forms. In other words, there can be more characteristics with one class according to the situation. 

This is very similar to object play in the real world. As a person, everyone has different duties according to the situation. The polymorphic object can change its behavior according to the usage of an object. The below example describes polymorphism further

In Programming, Polymorphism is made through inheritance. If a single class inherits many classes, then the child class has all the abilities of the parent class. As we discuss in the inheritance there are many IS A relationships with a single class.

Let’s take the example of humans. Your father is a human in your house. But he is the employee in his working office or location. Also, he is the husband of your mother. Maybe a Social worker in society. The role and the behaviors change according to the situation and location.

Oop polymorphism
Man has different Roles according to the situation: Oop polymorphism
Oop polymorphism
Father works in the office: Oop polymorphism

This is one simple example of Polymorphism in the real world and is very easy to understand using real-world examples. If you would like complete examples of Polymorphism may help you here.

4. Abstraction

Abstraction is one of the main concepts of Object-Oriented Programming (OOP). The concept of abstraction is showing the required information to the outside and hiding the lower-level complex implementation.

It is hiding the complexity of implementation. Therefore able to design things on top of abstract information but do not want to worry about implementation. So abstraction is a better design-level concept for planning the program without thinking about the complexity of the implementation. Encapsulation is a bit similar to this but it comes with an implementation time. Both help to hide the data for security.

Let’s take your car as an example from the real world. When it considers car design, it’s giving you a Key(Switch) to start the car, a steering wheel for turning the car Brake paddles, accelerator paddles to speed up and slow down the car, etc…

Oop Abstraction
Car design: OOP Abstraction
Oop Abstraction
OOP Abstraction

This means car users do not need to know about how works engine, brakes, and steering wheel, and their complexity. Once design the car Just only need to think about what the car looks like, what type of features includes, and which plan is used to build it.

According to the example, cars are designed to remove complexity from users. That is hidden by a user and all come from the design level. This feature is implemented and those are available in realty with encapsulation.

Summing up things

The article tries to summarize 4 pillars of object-oriented programming(4 Pillars of OOP) and these four concepts are the heart of OOP. 4 Pillars of OOP are directly mapped with real-world object behavior and this is easy to learn when you think about real life and concepts.

Abstraction, encapsulation, inheritance, and polymorphism are the four pillars of object-oriented programming. If you are happy to see more descriptive examples and sources on each pillar see our article on each title.

Frequently Asked Questions

1 What is meant by the term OOPs?

The concept behind the OOP is to map programming objects with real-world objects, things, or concepts. We can consider the house as a single object once completed the construction of the building process. But if you think about the structure of the house it’s built on a collection of different objects like windows, doors, walls, etc. In the same way, windows are built using Glass, Iron frames

2 What are the 4 pillars of object-oriented programming?

The 4 pillars of OOP are well defined and those are the base concepts of OOP. The four pillars of object-oriented programming are

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

3 What is inheritance?

Inheritance in OOP is acquiring all the behaviors and attributes from one another. This means one class’s properties are derived from another class.

4 What is polymorphism?

Polymorphism in Object-Oriented Programming name describes the meaning. Poly into Many and morphism into Form. Then Polymorphism easily says a single class or object’s ability to play as many forms. In other words, there can be more characteristics with one class according to the situation. 

5 What is Encapsulation?

Encapsulation in OOP1 is everything (data-attributes / behaviors- method or function) combining and keeping together as a single unit. Encapsulation is used in the implementation time in oop. Data hiding is part of the encapsulation

6 What is Abstraction?

Abstraction is one of the main concepts of OOP. The concept of abstraction is showing the required information to the outside and hiding the lower-level complex implementation. It is hiding the complexity of implementation

7 What is different between Abstraction Vs Encapsulation

Abstraction Encapsulation
The concept of abstraction is showing the required information to the outside and hiding the lower-level complex implementationEncapsulation is everything combining and keeping together as a single unit
Abstraction is Design level programming conceptEncapsulation is an implementation-level programming concept
Abstract classes or interfaces used to achieve abstractionAccess modifiers used to achieve encapsulation
Data hiding is part of the abstractionData hiding is part of the encapsulation
Code complexity hides using abstract classes and interfacesUse getter and setter to hide data
Key Identifiers of Abstraction and Encapsulation

Feel free to add your 4 pillars of OOP questions or any other topic in the comment section. We are happy to write about that and thank you for reading us and liking us. Share and Subscribe to us!

ennicode

Address: 89/1 Rabbegamuwa, Handessa, Kandy, Central, Sri Lanka

Email to: Primary: [email protected]

Services

E-Learning

Company Websites

Support and Configuration work

Banners, Covers, and Post

Web Development & Configurations

Content Writing and Marketing

Contact

Ennicode