[go: up one dir, main page]

0% found this document useful (0 votes)
47 views8 pages

Course Title: Programming Language II Course Code: CSE 111 Semester: Summer 2020 Topic: Object-Oriented Programming (Inheritance)

This document discusses object-oriented programming concepts like inheritance, single inheritance, multilevel inheritance, and method resolution order (MRO) in Python. It provides examples to illustrate each concept. Single inheritance allows a child class to inherit from one parent class. Multilevel inheritance allows a derived class to inherit from another derived class, forming a chain. MRO defines the order that Python searches through the class hierarchy to resolve methods, traversing from left to right.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Course Title: Programming Language II Course Code: CSE 111 Semester: Summer 2020 Topic: Object-Oriented Programming (Inheritance)

This document discusses object-oriented programming concepts like inheritance, single inheritance, multilevel inheritance, and method resolution order (MRO) in Python. It provides examples to illustrate each concept. Single inheritance allows a child class to inherit from one parent class. Multilevel inheritance allows a derived class to inherit from another derived class, forming a chain. MRO defines the order that Python searches through the class hierarchy to resolve methods, traversing from left to right.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Course Title: Programming Language II

Course Code: CSE 111


Semester: Summer 2020
Topic: Object-Oriented Programming (Inheritance)
Table of Contents
1. Inheritance: ......................................................................................................................................... 1
2. Single Inheritance: .............................................................................................................................. 2
3. Multiple Inheritance: ........................................................................... Error! Bookmark not defined.
4. Multilevel Inheritance:…………………………………………………………………………………………………………………5

5. Method Resolution Order (MRO): .................................................................................................... 4


1|Page

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.

1.1. Parent Class:


Parent class is the class being inherited from, also called base class.
Any class can be a parent class, so the syntax is the same as creating any other class
Example:

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()

1.2. Child Class:


Child class is the class that inherits from another class, also called derived class.
To create a class that inherits the functionality from another class, send the parent class as
a parameter when creating the child class:

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

# Parent class created


class Parent: Output:
parentname = ""
childname = "" Mark
John
def show_parent(self):
print(self.parentname)

# Child class created inherits Parent class


class Child(Parent):
def show_child(self):
print(self.childname)

ch1 = Child() # Object of Child class


ch1.parentname = "Mark" # Access Parent class attributes
ch1.childname = "John"
ch1.show_parent() # Access Parent class method
ch1.show_child() # Access Child class method

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)

# Mother class inherited from Family


class Mother(Family):
mothername = ""

def show_mother(self):
print(self.mothername)

# Son class inherited from Father and Mother classes


class Son(Father, Mother):
def show_parent(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)

s1 = Son() # Object of Son class


s1.fathername = "Mark"
s1.mothername = "Sonia"
s1.show_family()
s1.show_parent()

4. Method Resolution Order (MRO):

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 Animal: Output:


def __init__(self, Animal): Dog has 4 legs.
print(Animal, 'is an animal.');
Dog can't swim.
class Mammal(Animal): Dog can't fly.
def __init__(self, mammalName):
print(mammalName, 'is a warm- Dog is a warm-blooded animal.
blooded animal.')
Dog is an animal.
super().__init__(mammalName)

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)

class Dog(NonMarineMammal, NonWingedMammal):


def __init__(self):
print('Dog has 4 legs.');
super().__init__('Dog')

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.

You might also like