0% found this document useful (0 votes)
2 views

Unit-7

The document covers the concepts of inheritance and polymorphism in object-oriented programming (OOP) using Python. It explains the definitions, advantages, and implementations of these concepts, including method overloading and overriding. Additionally, it highlights the relationships between classes and the use of special functions for operator overloading.

Uploaded by

kayelpat2708
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)
2 views

Unit-7

The document covers the concepts of inheritance and polymorphism in object-oriented programming (OOP) using Python. It explains the definitions, advantages, and implementations of these concepts, including method overloading and overriding. Additionally, it highlights the relationships between classes and the use of special functions for operator overloading.

Uploaded by

kayelpat2708
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/ 60

Inheritance and

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>

❖ A class can inherit multiple classes


class Derived_Class(<Base_Class1>, … , <Base_ClassN>):
<class suite>

❖ The syntax of multi-level inheritance

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

❖ Effective in extending the functionality by altering


the inherited method
− if the method inherited from the parent class does
not fulfill the need of a child class, re-implement
the same method in the child class in a different
way
❖ Useful when a parent class has multiple child classes
− if one of the child class wants to redefine the
method, the other child classes can still use the
parent class method
❖ Python first checks the object’s class type and
executes the appropriate method when called
− if a Car object is created, then Python calls the
speed() method from the Car class
Override Built-in Functions
− the default behavior of the built-in
functions can be changed
▪ can change or extend the built-in
functions such as len(), abs(), or
divmod() by redefining them in the
class
Operator Overloading
− changing the default behavior of
an operator depending on the operands
− can use the same operator for multiple
purposes
▪ the + operator is used to carry out
different operations for distinct data
types, one of the simplest occurrences of
polymorphism in Python
• it will perform an arithmetic addition
operation when used with numbers and
perform concatenation when used with
strings
Overloading + Operator for Custom Objects

− there are two objects, and add these two


objects with a binary + operator
▪ it will throw an error if addition is
performed because the compiler doesn’t
add two objects
❖Python provides some special or magic
function that is automatically invoked when
associated with that particular operator.
− internally, + operator is implemented by
using the __add__() method
Overloading the * Operator
− the * operator is used to perform the
multiplication
▪ internally, * operator is implemented by
using the __mul__() method
Overloading Comparison Operators
Special Functions
Magic Methods for Binary Operators
Operator Magic Method
+ __add__(self, other)
- __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
% __mod__(self, other)
** __pow__(self, other)
Magic Methods for Comparison Operators
Operator Magic Method
< __lt__(self, other)
> __gt__(self, other)
<= __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)
Magic Methods for Assignment Operators
Operator Magic Method
+= __iadd__(self, other)
-= __isub__(self, other)
*= __imul__(self, other)
/= __itruediv__(self, other)
//= __ifloordiv__(self, other)
%= __imod__(self, other)
**= __ipow__(self, other)
Magic Methods for Unary Operators
Operator Magic Method
- __neg__(self)
+ __pos__(self)
~ __invert__(self)
Overloading vs Overriding
Method Overloading Method Overriding
same name, different same name, same
parameters parameters
inheritance may not be
inheritance may is required
required
done between methods done between parent class
within the class and child class methods
to add more to the behavior to change the behavior of
of the method the existing method
no need of more than one
needs at least two classes
class

60

You might also like