Inheritance in Object Oriented Programming

Inheritance in Object Oriented Programming

Introduction

Inheritance is one of the most important aspect of object oriented programming. In real life, inheritance means child receives some qualities and 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 blueprint 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.

child-parent-class.png

In the example given above, we have two classes animal and dog, where Animal class is parent class and Dog class is 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

types-of-inheritance.png

Now, let's understand each type of inheritance with examples.

1) Single Inheritance :-

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

single-inheritance.png

# parent class
class Doctor:
    # constructor
    def __init__(self, name ,degree):
        self.name = name
        self.degree = degree

    def introduce(self):
        print("I am a doctor. My name is "+self.name+
        " and I have "+self.degree+" degree")

#The child class Surgeon inherits from base class Doctor
class Surgeon (Doctor):
    def __init__(self,name,degree,surgeon_type):
        self.surgeon_type= surgeon_type
        # call constructor of parent class
        Doctor.__init__(self,name,degree)

    def tell_speciality(self):
        print("I am a "+self.surgeon_type+" surgeon")


# make object of child class

surgeon_obj = Surgeon("Ramesh","MBBS","Orthopedic")
surgeon_obj.introduce()
surgeon_obj.tell_speciality()

# output
# I am a doctor. My name is Ramesh and I have MBBS degree
# I am a Orthopedic surgeon

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(class Surgeon).

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.

multilevel-inheritance.png

# parent class
class Doctor:
    # constructor
    def __init__(self, name ,degree):
        self.name = name
        self.degree = degree

    def introduce(self):
        print("I am a doctor. My name is "+self.name+
        " and I have "+self.degree+" degree")

#The child class Surgeon inherits from base class Doctor
class Surgeon (Doctor):
    def __init__(self,name,degree,surgeon_type):
        self.surgeon_type= surgeon_type
        # call constructor of parent class
        Doctor.__init__(self,name,degree)

    def tell_speciality(self):
        print("I am a "+self.surgeon_type+" surgeon")

# The child class OrthopedicSurgeon inherits from class Surgeon which is child class of class Doctor
class OrthopedicSurgeon(Surgeon):
    def __init__(self,name,degree,surgeon_type):
        Surgeon.__init__(self,name,degree,surgeon_type)

    def tell_duties(self):
        print("I treat patients through surgical procedures")

# make object of grand child
orthopedic_surgeon_obj = OrthopedicSurgeon("Ramesh","MBBS","Orthopedic")
orthopedic_surgeon_obj.introduce()
orthopedic_surgeon_obj.tell_speciality()
orthopedic_surgeon_obj.tell_duties()

# output
# I am a doctor. My name is Ramesh and I have MBBS degree
# I am a Orthopedic surgeon
# I treat patients through surgical procedures

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(class OrthopedicSurgeon).

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 ) of class OrthopedicSurgeon.

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

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 or more parent classes.

multiple-inheritance.png

# parent class
class Doctor:
    # constructor
    def __init__(self, name ,degree):
        self.name = name
        self.degree = degree

    def introduce(self):
        print("I am a doctor. My name is "+self.name+
        " and I have "+self.degree+" degree")

# another parent class
class Neurologist :
    def __init__(self,patient_type):
        self.patient_type= patient_type

    def tell_speciality(self):
        print("I am a "+self.patient_type+" Neurologist")

# The child class ChildNeurologist inherits from parent classes 
class ChildNeurologist(Doctor, Neurologist):
    def __init__(self,name,degree,patient_type):
        Doctor.__init__(self,name,degree)
        Neurologist.__init__(self,patient_type)

    def tell_duties(self):
        print("I treat disorders that affect the brain, spinal cord, and nerves of children")

# make object of child class
child_neurologist_obj = ChildNeurologist("Rajesh","MBBS","Child")
child_neurologist_obj.introduce()
child_neurologist_obj.tell_speciality()
child_neurologist_obj.tell_duties()

# output
# I am a doctor. My name is Rajesh and I have MBBS degree
# I am a Child Neurologist
# I treat disorders that affect the brain, spinal cord, and nerves of children

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 (class ChildNeurologist).

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 are inherited from a single class, which means there will be one parent class and many child classes.

hierarchical-inheritance.png

# parent class
class Doctor:
    # constructor
    def __init__(self, name ,degree):
        self.name = name
        self.degree = degree

    def introduce(self):
        print("I am a doctor. My name is "+self.name+
        " and I have "+self.degree+" degree")

#The child class Surgeon inherits from parent class Doctor
class Surgeon (Doctor):
    def __init__(self,name,degree,type):
        self.type= type
        # call constructor of parent class
        Doctor.__init__(self,name,degree)

    def tell_speciality(self):
        print("I am a "+ self.type)

#The child class Dentist inherits from parent class Doctor
class Dentist (Doctor):
    def __init__(self,name,degree,type):
        self.type= type
        # call constructor of parent class
        Doctor.__init__(self,name,degree)

    def tell_speciality(self):
        print("I am a "+ self.type)

# make objects of child classes
surgeon_obj = Surgeon("Ramesh","MBBS","Surgeon")
surgeon_obj.introduce()
surgeon_obj.tell_speciality()

dentist_obj = Dentist("Radha","BDS","Dentist")
dentist_obj.introduce()
dentist_obj.tell_speciality()

# output
# I am a doctor. My name is Ramesh and I have MBBS degree
# I am a Surgeon

# I am a doctor. My name is Radha and I have BDS degree
# I am a Dentist

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.

I'd love to connect with you at Twitter | Instagram | LinkedIn .

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.