Inheritance
"Say not you know another entirely,till you have divided an inheritance with him."
— Johann Kasper Lavater
Common
- Simple Inheritance: Create a
Vehicle
class and aCar
subclass. Create a base class calledVehicle
with two attributes:color
andmax_speed
. Then, create a derived class calledCar
that inherits fromVehicle
. TheCar
class should have an additional attribute:num_doors
. Initialize the attributes and print them to verify that inheritance is working correctly. - Inheriting Methods: Implement
Shape
andRectangle
classes. Define a base class calledShape
with two methods:area()
andperimeter()
. These methods should be placeholders (i.e., not implemented). Create a derived class calledRectangle
that inherits fromShape
. Implement thearea()
andperimeter()
methods in theRectangle
class, considering the rectangle's length and width. - Multilevel Inheritance: Create a
Person
-Student
-GraduateStudent
hierarchy. Create a base class calledPerson
with two attributes:name
andage
. Then, create a derived class calledStudent
that inherits fromPerson
. TheStudent
class should have two additional attributes:roll_number
andcourse
. Next, create another derived class calledGraduateStudent
that inherits fromStudent
. TheGraduateStudent
class should have two additional attributes:thesis_topic
andsupervisor
. Initialize the attributes and print them. - Hierarchical Inheritance: Create
HybridVehicle
inheriting fromElectricVehicle
andGasVehicle
. Define two base classes:ElectricVehicle
andGasVehicle
. Each class should have attributes specific to the vehicle type. Create a derived class calledHybridVehicle
that inherits from bothElectricVehicle
andGasVehicle
. Initialize the attributes and print them. - Method Overriding: Override
calculate_interest()
method inSavingsAccount
. Create a base class calledBankAccount
with a methodcalculate_interest()
. Create a derived class calledSavingsAccount
that overrides thecalculate_interest()
method. Consider the savings account's interest rate and balance. - Constructor Inheritance: Create
Employee
andManager
classes. Create a base class calledEmployee
with an__init__()
method that initializesname
andsalary
. Create a derived class calledManager
that inherits fromEmployee
. TheManager
class should have an additional attribute:department
. Initialize the attributes and print them. - Inheriting Class Variables: Access
University
'snum_students
inDepartment
class. Define a base class calledUniversity
with a class variablenum_students
. Create a derived class calledDepartment
that inherits fromUniversity
. Access and print thenum_students
variable from theDepartment
class. - Abstract Base Class: Implement
Shape
,Circle
,Rectangle
, andTriangle
classes. Create an abstract base class calledShape
with abstract methodsarea()
andperimeter()
. Create concrete derived classesCircle
,Rectangle
, andTriangle
that implement these methods. Consider the shapes' dimensions. - Inheritance with Encapsulation: Create
Customer
andPremiumCustomer
classes. Create a base class calledCustomer
with private attributesname
andemail
. Provideget_name()
andget_email()
methods to access these attributes. Create a derived class calledPremiumCustomer
that inherits fromCustomer
. Access and print the private attributes using the getter methods. - Complex Inheritance Scenario: Create
Vehicle
,Car
,Truck
,Motorcycle
, andElectricCar
classes. Create a base class calledVehicle
with attributescolor
andmax_speed
. Create derived classesCar
,Truck
, andMotorcycle
that inherit fromVehicle
. Then, create another derived classElectricCar
that inherits from bothCar
andElectricVehicle
. Initialize the attributes and print them. - Multiple Inheritance: Create
Person
,Employee
, andManager
classes using multiple inheritance. Define a base class calledPerson
with attributesname
andage
. Define another base class calledEmployee
with an attributeemployee_id
. Then, create a derived class calledManager
that inherits from bothPerson
andEmployee
. Initialize the attributes and print them to verify the working of multiple inheritance. - Diamond Problem Resolution: Demonstrate the diamond problem in Python using
A
,B
,C
, andD
classes. Define a classA
with a methodshow()
. Create two classesB
andC
that inherit fromA
and override theshow()
method. Finally, create a classD
that inherits from bothB
andC
. Call theshow()
method fromD
to demonstrate method resolution order (MRO). - Super() Function: Use
super()
to call parent class methods in aParent
andChild
class. Create a base class calledParent
with a methodgreet()
that prints a greeting. Then, create a derived class calledChild
that overrides thegreet()
method but also calls the parent class'sgreet()
method usingsuper()
. Print both messages to verify. - MRO Exploration: Explore method resolution order (MRO) with
X
,Y
, andZ
classes. Define three classesX
,Y
, andZ
with methods having the same namedisplay()
. Create a classXYZ
that inherits fromX
,Y
, andZ
. Call thedisplay()
method fromXYZ
and observe which method gets called first. - Interface Implementation: Create an interface
Flyable
and classesBird
andAirplane
that implement this interface. Define an interfaceFlyable
with a methodfly()
. Then, create two classes:Bird
andAirplane
that implement thefly()
method. Call thefly()
method from instances of both classes. - Polymorphism with Inheritance: Demonstrate polymorphism with
Animal
andDog
,Cat
classes. Define a base classAnimal
with a methodmake_sound()
. Then, create two derived classes:Dog
andCat
that override themake_sound()
method. Callmake_sound()
from both derived class instances and demonstrate polymorphism. - Class Hierarchy: Build a class hierarchy with
Appliance
,WashingMachine
, andRefrigerator
classes. Define a base classAppliance
with an attributebrand
. Then, create two derived classesWashingMachine
andRefrigerator
that add specific attributes. Initialize the attributes and print them for both objects. - Delegation Pattern: Implement the delegation pattern with
Printer
andLaserPrinter
classes. Create a classPrinter
with a methodprint_document()
. Then, create a classLaserPrinter
that delegates the task toPrinter
using composition. Callprint_document()
from an instance ofLaserPrinter
. - Composition over Inheritance: Demonstrate composition over inheritance using
Engine
andCar
classes. Define a classEngine
with attributeshorsepower
andtype
. Then, create a classCar
that usesEngine
as a component. Initialize both theCar
andEngine
attributes and print them. - Mixin Classes: Implement mixin classes with
LoggerMixin
andDatabase
classes. Create a mixin class calledLoggerMixin
that adds alog()
method. Then, create a classDatabase
that usesLoggerMixin
as a mixin and adds its own methodconnect()
. Demonstrate calling bothlog()
andconnect()
methods from an instance ofDatabase
.