cft

Inheritance in Object Oriented Programming

In this blog, I explained types of inheritance with examples using python and also discussed advantages and limitations of inheritance.


user

Viresh Fegade

3 years ago | 4 min read

Introduction

Inheritance is one of the most important aspect of object oriented programming.In real life, inheritance means child receives some qualities or behavior from parents.Similarly, in object oriented programming, the child class receives the properties from parent class . Child class can access all the attributes and methods defined in the parent class.

Before deep diving into inheritance, lets understand basic terms used in object oriented programming.

  1. Class :-
  • It an logical entity which consist of attributes and methods(functions).It is blue print of an object.
  • for example, if we are making car racing video game , then Car class should contain attributes like color, speed, price and methods like go_forward, apply_break.

2. Object :-

  • An object is an instance of a class. It has its own state, behavior, and identity.
  • It can be any real-world object like the car, table, person, bag, etc.

3. Constructor :-

  • Constructors are generally used for instantiating an object. They are invoked as soon as object of a class is created.
  • when an object of the class is created, constructors initialize to the data members of the class .

4. Child Class :-

  • It is also called as sub class or derived class. It is a class which is inherited from another class.

5. Parent Class :-

  • It is also called as base class or super class. It is a class which allows child classes to inherit from itself.

for example , if we have two classes animal and dog, then animal will be parent class and dog will be child class.

Types of Inheritance

There are total 5 types of inheritance.

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

  1. Single Inheritance :-

In single inheritance, child class is inherited from only one parent class.

Here, Doctor is a parent class and Surgeon is a child class. All methods and variables of class Doctor are accessible in Surgeon class. Hence, we made object of child class.

surgeon_obj.introduce() will print I am a doctor. My name is Ramesh and I have MBBS degree , introduce is method of parent class.

surgeon_obj.tell_speciality() will print I am a Orthopedic surgeon , tell_speciality is method of child class.

2. Multi-Level Inheritance :-

In Multi-Level Inheritance, a class is inherited from a class that is already inherited from another class. we have child and grandchild relationship.

Here, class Doctor is parent class which inherits child class Surgeon and class Surgeon also inherits another class OrthopedicSurgeon which is grand child of class Doctor.

All methods and variables of class Doctor are accessible in class Surgeon.Similarly All methods and variables of class Surgeon are accessible in class OrthopedicSurgeon .Hence, we made object of grand child class.

orthopedic_surgeon_obj.introduce() will print I am a doctor. My name is Ramesh and I have MBBS degree, introduce is method of grand parent class Doctor.

orthopedic_surgeon_obj.tell_speciality() will print I am a Orthopedic surgeon,tell_speciality is method of parent class Surgeon.

orthopedic_surgeon_obj.tell_duties() will print I treat patients through surgical procedures, tell_duties is method of class OrthopedicSurgeon itself.

3. Multiple Inheritance :-

In Multiple Inheritance, one class is inherited from two or more classes, which means a child class has two parent classes.

Here, class Doctor and class Neurologist are parent classes which inherits the child class ChildNeurologist .

All methods and variables of class Doctor and class Neurologist are accessible in child class ChildNeurologist .Hence, we made object of child class.

child_neurologist_obj.introduce() will print I am a doctor. My name is Rajesh and I have MBBS degree, introduce is method of parent class Doctor.

child_neurologist_obj.tell_speciality() will print I am a Child Neurologist, tell_speciality is method of parent class Neurologist.

child_neurologist_obj.tell_duties() will print I treat disorders that affect the brain, spinal cord, and nerves of children, tell_duties is a method of class ChildNeurologist itself.

4. Hierarchical Inheritance :-

In Hierarchical Inheritance, two or more classes inherits a single class, which means there will be one parent class and many child classes.

Here, class Doctor is parent class and class Surgeon and class Dentist are two child classes.

All methods and variables of class Doctor are accessible in both classes i.e. class Surgeon and class Dentist.

surgeon_obj.introduce() will print I am a doctor. My name is Ramesh and I have MBBS degree, introduce is method of parent class Doctor.

surgeon_obj.tell_speciality() will print I am a Surgeon, tell_speciality is method of class Surgeon itself.

similarly, dentist_obj.introduce() will print I am a doctor. My name is Radha and I have BDS degree.

dentist_obj.tell_speciality() will print I am a Dentist.

5. Hybrid Inheritance :-

Hybrid Inheritance is a combination of Inheritances. In this type of Inheritance, more than one type of inheritance is present.

For example, if we have class A and class B that inherits class C and then there is another class D that inherits class A.

Advantages of Inheritance

  • Inheritance enables code reusability and saves time.
  • After creating one super class we can use its features to create several sub classes that inherits some features from its parent class and have few additional features.

Limitations of Inheritance

  • Inheritance needs more time to process, as it needs to navigate through multiple classes for its implementation.
  • The classes involved in Inheritance - the base class and the child class, are very tightly coupled together. So if one needs to make some changes, they might need to do nested changes in both classes. If you want to know more about coupling, read this.

Final words

We have covered all the types of inheritance with examples. I hope it was a great read for you.If you got any questions or doubts, feel free to ping me on Twitter .

If you have any feedback please share it in the comment below. Also, if you find it helpful, please like and hit the follow button.

Upvote


user
Created by

Viresh Fegade


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles