Object-Oriented
Programming in
Python
Inheritance
Inheritance allows us to define a class that inherits
all the methods and attributes from another class.
Parent
Super Base
Class Class Class
Methods
Attributes
Sub Child Class Derived
Class Class
Different Types of Inheritance
1. Single Inheritance
2. Multi-level
3. Hierarchical
4. Multiple Inheritance
Single Inheritance
When a class inherits another
class, it is known as a single
inheritance.
Single Inheritance
Animal
eat()
Dog
bark() eat()
Basic Single Inheritance:
Inheritance with Constructor:
Overriding a Method:
Calling Parent Class Method using super():
Single Inheritance with Additional Attributes:
Exercises:
1. Create a Bird class with a method fly. Inherit it in Parrot and override the
method to print a different message.
2. Write a Book class and create an inherited EBook class that has an
additional attribute for the file size.
3. Implement a Shape class with a method area. Create a Circle class that
inherits from Shape and calculates the area of the circle.
4. Build a Parent class with a method greet. Inherit it in Child and add a new
method study.
5. Develop a Computer class with attributes brand and model. Inherit it in
Laptop and add another attribute battery_life.
Multilevel Inheritance
When there is a chain of
inheritance, it is known
as multilevel inheritance.
Multilevel Inheritance
Animal
eat()
extends
Dog
bark() eat()
extends
BabyDog
bark() eat()
weep()
Basic Multi-Level Inheritance:
Using Constructors in Multi-Level Inheritance:
Multi-Level Inheritance with Method Overriding:
Accessing Parent Methods with super():
Attribute Inheritance:
Exercises:
1. Create a Device class with an attribute power. Inherit it in
Phone, then Smartphone, adding new attributes.
2. Implement a Vehicle class, then inherit Car and then
ElectricCar, each with specific attributes.
3. Build a Machine class and inherit it into Printer, then
LaserPrinter. Add unique methods for each class.
4. Write a Country class, inherit State, and then City. Add
attributes at each level.
5. Develop a Gadget class with basic attributes. Inherit it into
Tablet, then GamingTablet, adding features at each level.
Hierarchical Inheritance
When two or more classes
inherit a single class, it is
known as hierarchical
inheritance.
Hierarchical Inheritance
Bird
eat()
extends
extends
Penguin Parrot
eat() swim() eat() fly()
Basic Hierarchical Inheritance:
Overriding Methods in Hierarchical Inheritance:
Shared Methods in Hierarchical Inheritance:
Hierarchical Inheritance with Constructor:
Adding Attributes in Hierarchical Inheritance:
Exercises:
1.Create a Vehicle class and then inherit Car and Bike. Add
specific attributes and methods for each.
2.Write a Food class with a method nutrition_info. Inherit Fruit
and Vegetable, adding unique methods for each.
3.Implement a Bank class and then inherit CheckingAccount and
SavingsAccount. Add methods for each account type.
4.Build a Person class with a method introduce. Inherit Teacher
and Student, each adding different features.
5.Create a Course class with a method details. Inherit
OnlineCourse and OfflineCourse, and add unique attributes.
Multiple Inheritance
When a child class
inherits multiple parent
classes, it is known
as multiple inheritance.
Multiple Inheritance
Animal
eat()
extends
Fish Bird
swim() fly()
Penguin
swim() eat() fly()
Basic Multiple Inheritance:
Multiple Inheritance with Overlapping Methods:
Multiple Inheritance Using super():
Unique Methods in Multiple Inheritance:
Multiple Inheritance with Attributes:
Exercises:
1.Create classes Artist and Athlete. Then create a Person class
that inherits from both and includes methods from each.
2.Write a Car class and a Boat class. Create an
AmphibiousVehicle class that inherits both, with methods for
each mode of transport.
3.Implement a Tablet class and a Laptop class. Inherit both in a
HybridDevice class with features from each.
4.Build a Painter class and a Musician class. Create an Artist
class that inherits both and adds a new method.
5.Develop a Bird class and a Fish class. Inherit both in a
FlyingFish class, adding unique attributes for both land and