System Design ( Day - 34 )

Manoj KumarManoj Kumar
2 min read

OOP’s Real-World Examples | Inheritance, Polymorphism

Inheritance
In Simple words, let’s take an example of a Car, what are the characteristics and functions of a car
Brand, model, enginePower etc, Functionalities are like Startengine, stopEngine, accelerate, Brakes like that, these are the common features for every cars that are present in the market, and we have Manual car and automatic cars that are launching, then the existing all the features and characteristics will be there with some additional characteristics and functionalities, then the Car which contains common features is the parent, those features and characteristics are implemented into the child class which is manual and automatic cars these type of implementation from parent to child is called inheritance.

If we are implementing from parent to child then we don’t need to define it again and again, this will reduce the code reusability, and we can write only the things that we want in our new features, so that the code looks neat and clean.
If we declare the class with the private key then that class can’t be inherited because it’s not visible to others right.
usually we don’t inherit other that public because other won’t follow the inheritance rules like code reusability.

Polymorphism
in Simple words, Let’s take a example of the living beings → Duck, Tiger, Human these are living organisms, everyone can run right but Duck will run in a different way, Human will run in a different way and the tiger will run in a different way.
in terms of programming this is called as Dynamic polymorphism, Run time polymorphism in which each time Run( ) is called above three living beings have same Run( ) functions in it, in other words it’s also called as Method Over Riding ,in different situation the same function will perform differently with overRiding the existing function,

Another example for RunTime polymorphism
Now we are taking only human for the example, run method is there for the Human, if a person is dull or not active then he will run slowly, if tiger chases that same person then that person will run in a very fast to save his life, here Human is same, Run( ) method is also same but the situation is different that is called compile time polymorphism or Static polymorphism or it can be called as Method overloading where as above example in which methods with same name but difference in parameters which is called Method Overloading or static polymorphism or compile time polymorphism.

0
Subscribe to my newsletter

Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manoj Kumar
Manoj Kumar