DEV Community

Cover image for OOP -Part 1 Inheritance
Sukrut Patil
Sukrut Patil

Posted on

OOP -Part 1 Inheritance

Object Oriented Programming, or I call it as common sense of programming. And we very well know not lot of people use their common sense very often and so OOPs concepts are generally misunderstood or misinterpreted. In this article, I am not going to associate any language with OOPs because not many of them are purely object oriented.
“What? But Sukrut as soon as we hear object oriented the names that pop out of our mouth are Java, C++, python. What about those?” Let me bring it to you all, none of the above are purely object oriented. The languages that are purely object oriented are Smalltalk and self. Heard of them? Probably not. As some things don’t make since in real world but yet they exist (Donald Trump). Similarly, there are some concepts in such languages that don’t make sense according to OOPs concepts but yet they are there and that what makes them not purely object oriented. We will also be looking at those concepts… Stay tuned.
There are 4 main concepts of OOPS, Inheritance, Polymorphism, Abstraction and Encapsulation. And the concepts of OOPs revolve around these 4 only. Let’s look at them one by one and in this blog, we will be looking at Inheritance. But first, few basic terminologies that you need to understand.

  1. Object: Object is anything or any real-world entity that you can think of. Wait don’t think about women. Ethically that not right but conceptually it is right…
  2. Class: Collection of objects is known as class. Since you are still objectifying women (conceptually). Let me carry on with the same example. Consider group of ladies than, “Women” is the class for each and every on of them and Miss. Shital, Miss. Tiffany, Miss. Nargis are the objects of that class women. Remember, classes are logical and do not occupy any memory.

Okay now, let’s talk about inheritance. As its name suggest when an object acquires properties and behavior of parent object it is known as inheritance. For example, there is super class called ‘Humans’ which has child classes ‘Men’ and ‘Women’. Human class has predefined properties in it like eyes, legs, brain, hands only those that are common in both men and women. Now, men class or women class don’t need to redeclare those for themselves. They can inherit these properties from super class Humans. Okay, they can redefine them within themselves. For example, men class can redeclare property ‘hair’ and change its behavior to ‘short’ and women class can redeclare it to ‘long’ but that is not in the scope of this blog we will study that later in polymorphism.
So, what inheritance does for us is reduce our work and let us work with prewritten code. In other words, make us lazier. And according to me lazier the programmer better is the code written. What say? Anyways, Inheritance is of 5 different kinds, 1. Single, 2. Multiple 3. Multilevel 4. Hierarchical 5. Hybrid (Virtual) Inheritance

  1. Single Inheritance: Consider this as a father child relation.
  2. Multiple Inheritance: This is many to one relation between classes. That means child class will have more than one parent classes.
  3. Multilevel Inheritance: Here child class will have a parent class and a parent class will also have a parent class and so on.
  4. Hierarchical Inheritance: This is like a family tree. Where more then one subclasses are derived from one single super class. As explained in above Humans’ example.
  5. Hybrid Inheritance: This is mixture of any of the above inheritance. This is what I understood studying inheritance in object-oriented programming. I might be wrong somewhere (Just a curtesy sentence. I know I am not wrong) so, please correct me if I am. This might help me understand better. I kept it as simple as I could and if you are interested in knowing other concepts of OPPs stay tuned. I will be posting them soon.

Top comments (3)

Collapse
 
sumit profile image
Sumit Singh

Just so good...

Collapse
 
thejscode profile image
Jaskirat Grewal

An interesting and engaging read (pun oriented). Waiting for further follow ups in the series:-))

Collapse
 
sukrutpatil profile image
Sukrut Patil

Thanks Jaskirat, I'm sure you are going to enjoy the series...