Programming

Real Life OOP examples

Object Oriented Programming just got real!

Priyansh Khodiyar
7 min readSep 4, 2022
Photo by Ben Griffiths on Unsplash

Before we start,

  • Object Oriented Programming is based on Objects and not Classes.

Let’s start now.

CLASS:

Example 1:

Class: Expensive Cars
Objects: Mercedes, BMW, Toyota
Data Member: Price, Speed, Wheels, Mileage, Model, Colour
Member Function: brakes(), speed(), start(), stop(), etc

Example 2:

Let’s say you have the blueprint of your future house over a hilltop ⛰️

Class: BluePrint
Object: House
Data: People living in the house

Important Points:

  • No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.

1. Parent Class: A class that an object shares all the properties off. (base class or superclass class)
2. Class: A particular type of class that does have objects.
3. Sub Class: A particular type of class that has a parent class. (derived class, extended class, or child class)

OBJECT:

Class: Animal
Object: Cat, Dog, Lion

  • Each object has its own identity, attribute, and behavior.
  • Consider both class and object. When we consider an actual laptop, the factory or the mechanical system that manufactures the laptop is the class and the laptop produced is it’s object

ENCAPSULATION (Information Hiding)

State (Private variables)
Methods (Behaviour)

Example 1:

Class: Person
Data Member: “socialSecurityNumber” (private)

A method called “bankTransaction()” is implemented, that function might then access the “socialSecurityNumber” variable.

Such a class would effectively encapsulate a person’s personal information.

Example 2:

class Encapsulation{
private:
int x;
public:
void set(int a){
x =a;
}
int get(){
return x;
}
};
int main() {
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
OUTPUT: 5

The variable x and the functions get() and set() are binded together which is nothing but encapsulation.

CAR = [Steering, Wheels, Engine, etc]

More Examples:

  1. In real time we are using Encapsulation for Security Purpose.
  2. Most commonly used example is the Medical Capsule.
Encapsulation = Abstraction + Data Hiding.

ABSTRACTION

Examples:

  1. Dialling a number call: Some method internally runs and connects the call but what is it doing we don’t know.
  2. Clicking on green button: Sends signals to calling person’s mobile but we are unaware of how it is doing.
  3. ATM machines: Withdraw cash, deposit cash, check balance, print bills, etc. It shows only the main things like getting inputs and giving the output.
  4. Gmail: Type email and password and click submit, It will show Compose, Inbox, Outbox, Sent mails. How it works internally?
  5. TV Remote is a interface between User and TV. User does not need to know the inside circuit details.

INHERITANCE

There are mainly 5 types of inheritance,

1. Single level inheritance
2. Multi-level inheritance
3. Hierarchical inheritance
4. Hybrid inheritance
5. Multiple inheritance

Examples:

Parent Class: Animals.
Class: Insect
SubClass: Ants, Grasshopper

Insects all have almost the same characteristics, such as six legs and an exoskeleton.

Here also we can also inherit common properties like name, sound, color, breed from Animal class and create classes like Dog, Cat, Horse and etc.

You probably won’t finish this article. In fact, I may have already lost you to another tab in your browser. :(

POLYMORPHISM

Examples:

Base Class: Animal
Derived Class: “Cat” and “Dog”

class Animal{public:
nameNoise(){...}
}
class Cat: public Animal{
...
meow(){...}
}
class Dog: public Animal{
bark(){...}
...
}

We can override the “makeNoise” function that is inherited by the subclasses “Cat” and “Dog” to be “meow” and “bark,” respectively if the Animal class contains a way to create a noise named “makeNoise.”

Then, another function may be built that takes any Animal object as a parameter and calls the “makeNoise” member function on it.

Depending on the type of animal object that was supplied to the function, the noise will be either a “meow” or a “bark.”

Let’s say Samsung mobile has a 64 MP camera available i.e. — it is having a functionality of CameraClick().

Now same mobile is having Panorama mode available in camera, so functionality would be same but with mode.

This type is said to be Static polymorphism or Compile time polymorphism.

More examples:

  1. Girl: This girl can be anything, a student, a writer, a friend, a mother, a sister, a daughter, a God.
    This girl can exist in different ways in different situations.
  2. Sound of animals: Different animals make different sounds.
  3. Behaviour: We behave differently in front of elders, and friends. A single person is behaving differently at different time.
  4. A Software Engineer can perform different task at different instance of time depending on the task assigned to him. She can do coding, testing, analysis and designing depending on the task assign and the requirement.
  5. Stadium of commonwealth games. Single stadium but it perform multiple task like swimming, lawn tennis etc.

Summary:

OOPs have following features:

  1. Object: Instance of Class
  2. Class: Blue print of Object
  3. Encapsulation: Protecting our Data | Secure implementation
  4. Polymorphism: Different instances according to different behaviors depending on data types used
  5. Abstraction: Hiding our irrelevant Data, or Restricts the data for relevance
  6. Inheritance: One property of object is acquiring to another property of object, saves time.

Related Example 1:

Let’s consider a class Person,

It defines what a “Person” is.
— (She/he) is a human being,
— (She/he) got a brain, hands, legs, etc

The people are like an object.
— They obey the rules specified above (being a human being, having hands, legs etc) ~ Object, instances of the class.

Derived class: Artists
— Artists have all the properties of persons, they also have additional features like, they can draw nice pictures.~ Inheritance.

— An artist will have some understanding of pictures, colour combinations etc… like data inside an object. If you ask him to draw a nice picture(call the function to draw a picture), he draws(returns) a nice picture.

— The internal details like what was happening in his brain, movements of his fingers etc, are hidden from you (user). ~ Abstraction.

— His understandings (data), fingers, ears (functions) etc, some how coordinate internally to provide a simple interface (ears: you can tell him) to you(user). ~ Encapsulation.

Related Example 2:

Jeff Goodell: Would you explain, in simple terms, exactly what object-oriented software is?

Steve Jobs: Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things.

And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction.

Here’s an example:

If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please?.”

I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets.

So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket.

Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction.

That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

Related Example 3:

You bought an iPhone. Kudos!

Your mom and dad have iPhones too. It’s an iFamily! Each of those iPhone is a copy (Object) of the design specs (Class) stored with Apple.

You wan’t to open it up? Nope! Glued shut. Typical Jobs, Use the touchscreen and buttons to use.
~ Abstraction.

You wanna make a call? Play a movie? Hear some music? Camera. Touch screen. Accelerator. Gyrometer. Everything built into that slim device.
~ Encapsulation.

You hate the boring theme. Want to change it? Jailbreak! Cydia! You started with an iPhone, and ended with something way better.
~ Inheritance.

Your iPhone was brand new. Not anymore. Why? Because you broke the warranty by jailbreaking!
~ Polymorphism.

The above 3 examples are taken from Quora.

OOP Principles

There are some OOPS principle that need to be satisfied while creating a class. This principle is called as SOLID, where each letter has some specification.

  1. SRP (The Single Responsibility Principle) — A class should have one, and only one responsibility.
  2. OCP (The Open Closed Principle) — You should be able to extend a classes behaviour, without modifying it. (Inheritance)
  3. LSP (The Liskov Substitution Principle) — Derived classes must be substitutable for their base classes. (Polymorphism)
  4. ISP (The Interface Segregation Principle) — Make fine chopped interface instead of huge interface as client cannot be forced to implement an interface which they don’t use.
  5. DIP (The Dependency Inversion Principle) — Depend on abstractions, not on concretions. (Abstraction)

I hope you got clarity out of this article and would love to hear your thoughts about (I missed something?) this, so feel free to reach out to me on LinkedIn or Twitter or respond in the comments below.

Do reach out, I would love to talk to you.

— If this article helped you in any way, consider sharing it with 2 friends you care about.

Till then stay alive.

--

--

Priyansh Khodiyar

I write highly researched technical articles on things I daily learn, sometimes code, and interview people. My DevOps publication - unyaml.com