Course Title: Programming Language II Course Code: CSE 111 Semester: Summer 2020 Topic: Object-Oriented Programming (Inheritance)
Course Title: Programming Language II Course Code: CSE 111 Semester: Summer 2020 Topic: Object-Oriented Programming (Inheritance)
1. Inheritance:
Inheritance is a golden rule and powerful feature in OOP (Object Oriented Programming). It allows
to define a Derived Class (Child Class) which takes all functionalities (attributes and methods)
of the Base class (Parent Class). We can add more features in the child class according to our
preference.
The best mechanism to reuse a code while building a software or system is inheritance. Otherwise,
there will be a number of duplicate codes which can add more complexity.
The benefits of inheritance are:
It represents real-world relationships well.
It provides reusability of a code. We don’t have to write the same code again and again.
Also, it allows us to add more features to a class without modifying it.
It is transitive in nature, which means that if class B inherits from another class A, then all
the subclasses of B would automatically inherit from class A.
class Person:
def __init__(self, fname, lname): Output:
self.firstname = fname
XY
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("X", "Y")
x.printname()
Example:
2|Page
Here, a class named Student, which inherit the properties and methods from the Person
class.
class Person:
def __init__(self, fname, lname): Output:
self.firstname = fname
self.lastname = lname XY
AB
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
pass
x = Person("X", "Y")
x.printname()
x = Student("A", "B")
x.printname()
2. Single Inheritance:
When the child class inherits the properties or functionalities of a single parent class.
Example:
The ch1 object is an instance of the Child class, but it has an access to data attributes and methods
described by both Parent and Child classes.
3|Page
3. Multilevel Inheritance:
Multi-level inheritance is archived when a derived class inherits another derived class.
There is no limit on the number of levels up to which, the multi-level inheritance is archived
in python. In this type of inheritance, a class can inherit from a child class or derived class.
4|Page
Example:
Here, Son class inherited from Father and Mother classes which derived from Family class.
class Family:
Output:
def show_family(self):
This is our family:
print("This is our family:")
Father : Mark
# Father class inherited from Family
class Father(Family): Mother : Sonia
fathername = ""
def show_father(self):
print(self.fathername)
def show_mother(self):
print(self.mothername)
Method Resolution Order(MRO) it denotes the way a programming language resolves a method
or attribute. Python supports classes inheriting from other classes. The class being inherited is
called the Parent or Superclass, while the class that inherits is called the Child or Subclass. In
python, method resolution order defines the order in which the base classes are searched when
executing a method. First, the method or attribute is searched within a class and then it follows
5|Page
the order we specified while inheriting. This order is also called Linearization of a class and set
of rules are called MRO(Method Resolution Order). While inheriting from another class, the
interpreter needs a way to resolve the methods that are being called via an instance.
In the following example, to find the output, python will travel all the parents of Class4 first (from left to
right) then to the upper level parent classes (if any).
So, Python also has a super() function that will make the child class inherit all the methods and
properties from its parent.
6|Page
Python's built-in super() function provides a shortcut for calling base classes, because it automatically
follows Method Resolution Order. If the child class inherits more than one class and Super() is used
inside its constructor then the constructor of the leftmost parent class will be invoked.
class NonWingedMammal(Mammal):
def __init__(self, NonWingedMammal): Bat can't swim.
print(NonWingedMammal, "can't fly.") Bat is a warm-blooded animal.
super().__init__(NonWingedMammal)
Bat is an animal.
class NonMarineMammal(Mammal):
def __init__(self, NonMarineMammal):
print(NonMarineMammal, "can't swim.")
super().__init__(NonMarineMammal)
d = Dog()
print('')
bat = NonMarineMammal('Bat')
Here, a method in the derived calls is always called before the method of the base class.
In example, Dog class is called before NonMarineMammal or NoneWingedMammal. These two
classes are called before Mammal, which is called before Animal, and Animal class is called
before the object.
If there are multiple parents like Dog(NonMarineMammal, NonWingedMammal), methods of
NonMarineMammal is invoked first because it appears first.