Unit-7
Unit-7
Polymorphism
Unit 7
CCS 1500
Objectives
❖Define inheritance and polymorphism
❖Differentiate inheritance and polymorphism
❖Distinguish overloading and overriding
❖Implement overloading and overriding
❖The four core aspects of OOP are supported
by Python
▪ Inheritance
▪ Polymorphism
▪ Data abstraction
▪ Encapsulation
3
Inheritance
4
Inheritance
− allows a class to inherit methods and
properties from another class
5
❖Write a program using a new class named
FirstYearStudent, easier if
FirstYearStudent is inherited from an
existing class Student
▪ FirstYearStudent
−might require additional data members
and functions (applicationFee or
orientation())
−might also have the more general
Student members
−might require a different display format
than the Student class
6
❖FirstYearStudent inherits/derives from
Student
− Student
▪ parent class, base class, superclass, or
ancestor
− FirstYearStudent
▪ a child class, derived class, subclass, or
descendant
7
Sample Hierarchy
8
Some Advantages of Inheritance
❖Much of the code is already written
❖Existing code has already been tested and is
reliable
❖You already understand how the base class
works, and you can concentrate on writing the
extensions
❖You can extend and revise a parent class
without corrupting the existing parent class
features
❖If other classes have been derived from the
parent class, the parent class is even more
reliable
9
super() Function
− no need to use the name of the parent
class, it will inherit the methods and
properties from its parent automatically
10
11
❖is-a relationship
− use inheritance only if there exists an is-a
relationship between two classes
▪ Truck is a Vehicle
▪ Orange is a Fruit
▪ Dog is an Animal
12
Uses of Inheritance
❖allows code reusability
❖cleaner code and easier to maintain
❖can inherit only the useful functionalities
and define other required features
13
Types of Inheritance
class Derived_Class(Base_Class):
<class suite>
class Class1:
<class suite>
class Class2(Class1):
<class suite>
class Class3(Class2):
<class suite> 14
15
16
17
18
issubclass(sub,sup) Function
− used to check the relationships between
the specified classes
− returns True if the first class is the subclass
of the second class, otherwise False
19
20
isinstance(obj,class) Function
− used to check the relationship between
the objects and classes
− returns True if the first parameter, obj, is
the instance of the second parameter,
class
21
22
Method Overriding
− specific implementation of a parent class
method is defined in the child class
23
24
25
Polymorphism
26
❖Depending on the context, programming
modules might change. Object-oriented
programs use polymorphism to perform the
same operation in a way customized to the
object. Such is not allowed in other
languages that are not object-oriented.
❖Without polymorphism, programmers need
to use a separate module or method name
for a method that multiplies three numbers
and one that multiplies only two numbers.
Polymorphism
− taken from the Greek words Poly (many)
and morphism (forms)
− refers to the use of a single type entity
(method, operator, or object) to represent
different types in different situations
▪ same function name can be used for
different types
− a very important concept in programming
Features of Polymorphism
► Method functionality implements differently
in different situations
► Thebehavior of the method changes
depending on the data provided
► Itallows the same name for a member of
the method in a class with many types
► It supports implicit type conversion
Polymorphic len() Function
Polymorphism with Functions and
Objects
− a function that can take any object as a
parameter and execute its methods
without checking the class type
▪ call object actions using the same
function instead of repeating method
calls
Polymorphism in Methods
− uses two different class types in the same
way
1. create a for loop that iterates through
a tuple of objects
2. call the methods without being
concerned about which class type each
object is
Polymorphism vs Inheritance
❖Inheritance
− describes the act of inheriting another
class, but
❖Polymorphism
− clarifies what objects achieve if objects
consist of a superclass
− example: person own several behaviors in
various situations
▪ a person can be a mother, sister,
daughter, wife, employee, and friend
Polymorphism with Inheritance
❖Polymorphism
− methods in the child class have the same
name as the methods in the parent class
❖Inheritance
− the child class inherits the methods from the
parent class, possible to modify a method in
a child class that it has inherited from the
parent class
❖If the method inherited from the parent class
does not fit the child class, re-implement the
method in the child class, known as method
overriding
Advantage of Method Overriding
60