Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Lecture 8 - Inheritance in Python

The document discusses inheritance in Python including defining inheritance with an example, using the super() function, and overriding methods. Inheritance allows a child class to inherit properties and methods from a parent class and reuse code. The super() function allows a child class to inherit from the parent without naming it directly. A method can be overridden in a child class if it has the same name, parameters, and return type as the parent method.

Uploaded by

Shuvo Roy
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)
8 views

Lecture 8 - Inheritance in Python

The document discusses inheritance in Python including defining inheritance with an example, using the super() function, and overriding methods. Inheritance allows a child class to inherit properties and methods from a parent class and reuse code. The super() function allows a child class to inherit from the parent without naming it directly. A method can be overridden in a child class if it has the same name, parameters, and return type as the parent method.

Uploaded by

Shuvo Roy
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/ 14

Lecture-8

Inheritance in Python
Content

● Defines Inheritance with Example


● Use of super() Function
● Overriding in Python

sazzad@diucse
What is Inheritance?
Inheritance allows us to define a class that inherits all the methods
and properties from another class.

• Parent class: is the class being inherited from, also called base
class.
• Child class is the class that inherits from another class, also called
derived class.

sazzad@diucse
Inheritance Example

Ball Base Class

Child Class

sazzad@diucse
Create Base Class

sazzad@diucse
Create Child Class

sazzad@diucse
Create Object & Call Base Class Method

sazzad@diucse
Benefits of Inheritance

• 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

sazzad@diucse
Use the Super() Function

• Python has a super() function that will make the child class inherit
all the methods and properties from its parent
• By using the super() function, you do not have to use the name of
the parent element, it will automatically inherit the methods and
properties from its parent.

sazzad@diucse
Use the Super() Function

sazzad@diucse
Overriding

When a method in a subclass has the same name, same


parameters or signature and same return type (or sub-type) as a
method in its super-class, then the method in the subclass is said
to override the method in the super-class.

sazzad@diucse
Overriding Example

Base Class Ball


who_am_i()
Overriding
Extends

Child Class FootBall


who_am_i()

sazzad@diucse
Overriding Example

sazzad@diucse
Thank You

sazzad@diucse

You might also like