L03 Inheritance and Polymorphism
L03 Inheritance and Polymorphism
You can often make many specific classes, by inheriting from a generic class:
§ When we declare the same method in child class which is already present in the
parent class, this is called method overriding.
example # Base class or Parent class L03-01.py 4
class Child:
# Constructor
Note that “Child” is the
def __init__(self, name): name of class.
self.name = name In the context of
Parent (Base or Super) Class # To get name inheritance, it is a
def getName(self): parent class, not a child
return self.name class .
# To check if this person is student
def isStudent(self):
return False
object = Child()
object.func1()
object.func2()
Output:
def print_name(self):
print('GrandParent name:', self.gpname)
print("Parent name:", self.pname)
print("Child name:", self.name)
Output:
GrandParent name: Charlotte
Parent name: Emma
Child name: Lola
Types of inheritance: Hierarchical Inheritance 8
# Derived class2
class Child2(Parent):
def func2(self):
print("func2 in child 2.")
object1 = Child1()
object2 = Child2()
object1.func()
object1.func1()
object2.func()
object2.func2()
Output:
func in parent class.
func1 in child 1.
func in parent class.
func2 in child 2.
Types of inheritance: Multiple Inheritance 9
# Derived class
class Child(Mother, Father):
def parents(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
obj = Child()
obj.fathername = "Stefan"
obj.mothername = "Emily"
obj.parents()
Output:
Father : Stefan
Mother : Emily
Types of inheritance: Hybrid Inheritance 10
class Person: L03-06.py
def func(self):
Hybrid inheritance refers to print("func in Person.")
of inheritance.
def func1(self):
print("func1 in student 1.")
class Student2(Person):
def func2(self):
print("func2 in student 2.")
object = Student3()
object.func()
object.func1()
object.func2()
object.func3()
Output:
func in Person.
func1 in student 1.
func2 in student 2.
func3 in student 3.
BENEFITS OF INHERITANCE
11
1. Code reusability.
3. It is transitive in nature.
§ with Inheritance
§ Operator Overloading
Polymorphism with Function and Objects 14
obj_tomato = Tomato()
obj_apple = Apple()
func(obj_tomato)
func(obj_apple)
Output:
Vegetable
Red
Fruit
Green
Polymorphism with Classes Methods (Duck typing) 15
In this type, Python uses two (or more) different class types in the same way.
§ The term ‘Duck Typing’ comes from the saying: “If it walks like a duck, and it
quacks like a duck, then it must be a duck.”
class Duck: L03-08.py
check types at all. Instead, you check for for animal in Duck(), Sparrow(), Whale():
animal.fly()
the presence of a given method or
Output:
attribute. Duck flying
Sparrow flying
AttributeError: 'Whale' object has no attribute 'fly'
Polymorphism with Inheritance 16
class parrot(Bird):
§ We re-implement the functions in the def flight(self):
print("Parrots can fly.")
derived class.
class penguin(Bird):
def flight(self):
§ The phenomenon of re-implementing print("Penguins do not fly.")
a function in the derived class is obj_bird = Bird()
obj_parr = parrot()
known as Method Overriding. obj_peng = penguin()
obj_bird.intro()
obj_bird.flight() Output:
There are different types of birds.
obj_parr.intro()
Most of the birds can fly but some cannot.
obj_parr.flight() There are different types of birds.
Parrots can fly.
obj_peng.intro() There are different types of birds.
obj_peng.flight() Penguins do not fly.
Polymorphism with Operator Overloading 17
def __str__(self):
Example: return "({0},{1})".format(self.x, self.y)
A = Point(1, 2)
§ (1,2) + (4,1) = ? B = Point(4, 1)
print(A + B)
Output:
(5,3)
P.S. This type of polymorphism will be discussed in more details in Lesson 11.
At the moment, a general understanding of the operator overloading is
18