OOP Concepts With Real-World Examples

The Application of OOP Concepts in the Real World Scenarios

Samadhi Jayawardena
LinkIT
3 min readJun 3, 2021

--

Photo by Emile Perron on Unsplash

OOP stands for Object-Oriented Programming. Actually, what is this object-oriented programming means? Simply OOP is a paradigm that creates objects which contain both data and methods. When it comes to OOP Classes and Objects are the main aspects of it. Class is like a blueprint that can be used to create objects. An object is an instance of a class and it has state and behavior. For example, when we build a house first we create a plan. Using this plan we can build as many as houses we want. Just like that classes can be used to create many objects.

Another example is that if we consider fruits as a class, then Apple, Mango, and Banana can be considered as objects. OOP also has some important concepts such as Data Abstraction, Encapsulation, Inheritance, and Polymorphism. So in this article, I am going to talk about these concepts with real-world examples.

The rest of the article is organized as follows:

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

1. Data Abstraction

First I will talk about Data Abstraction. Simply this means showing only relevant data while hiding unnecessary details of objects. In other words, we can say that represents essential features without including the background details. A real-world example of this is when we ride a bike we only concern about things like accelerating, braking but we do not know how it happens.

Another example is that when we log into Facebook we enter our email and password. Then when we press enter we can log into our account. But we do not know how the input data is sent and how it is verified from the server-side.

2. Encapsulation

Encapsulation means binding or wrapping code and data together into a single unit. So what this encapsulation does is that it keeps the data and code safe from external interference. In Java class is an example of this encapsulation. A real-world example for encapsulation is a capsule in which all medicine is encapsulated inside a capsule.

Another example is the power steering of a car. It is a very complex system in which internally many components are functioning together to turn the car in the right direction. But users see it as one interface.

3. Inheritance

When one object acquires all or some of a parent object's properties and behaviors, it is known as inheritance. This inheritance provides code reusability and this is used to achieve runtime polymorphism. OOP often organizes classes in a hierarchy to avoid duplication and reduce redundancy. Inheritance comes from the fact that the subclass contains the attributes and methods of the parent class.

There are several types of inheritance as Single Inheritance, Multiple Inheritance, Hierarchical Inheritance, Multilevel Inheritance, and Hybrid Inheritance. A real-world example of this concept is when a child is born he or she inherits the features of parents such as beauty from mother and intelligence from father. Another example is Dog inherits from the Animal superclass.

4. Polymorphism

The last one is polymorphism which we can explain as “many forms”. From inheritance it allows us to inherit attributes and methods from another class. So in polymorphism, it uses those methods to perform tasks in different ways. For example, let’s take Animal class which is the superclass. It has a method call sound which outputs ‘Animal can make a sound’. Let’s assume there is a subclass that inherits from the Animal class as Dog. So what basically polymorphism does is change that sound method.

For example, the Dog class will have the same method sound inherited from the Animal class and will output ‘Dog can Bark’. This polymorphism has two types as Runtime polymorphism and Compile Time Polymorphism. A real-time example of this is when you are in the university you act as an undergraduate when you are in a shop you act like a customer, when you are at home you act like a child. So here we can see the same person has different behaviors in different situations.

So in this article, I talked about the main concepts of OOP in brief with real-world examples. I hope you will gain some knowledge about the concepts of OOP from this article. Stay tuned for more interesting articles.

If you enjoyed this article, please share it with your friends. Thanks for Reading!

--

--