Inheritance
"Say not you know another entirely,till you have divided an inheritance with him."
— Johann Kasper Lavater
Common
- Simple Inheritance: Create a
Vehicleclass and aCarsubclass. Create a base class calledVehiclewith two attributes:colorandmax_speed. Then, create a derived class calledCarthat inherits fromVehicle. TheCarclass should have an additional attribute:num_doors. Initialize the attributes and print them to verify that inheritance is working correctly. - Inheriting Methods: Implement
ShapeandRectangleclasses. Define a base class calledShapewith two methods:area()andperimeter(). These methods should be placeholders (i.e., not implemented). Create a derived class calledRectanglethat inherits fromShape. Implement thearea()andperimeter()methods in theRectangleclass, considering the rectangle's length and width. - Multilevel Inheritance: Create a
Person-Student-GraduateStudenthierarchy. Create a base class calledPersonwith two attributes:nameandage. Then, create a derived class calledStudentthat inherits fromPerson. TheStudentclass should have two additional attributes:roll_numberandcourse. Next, create another derived class calledGraduateStudentthat inherits fromStudent. TheGraduateStudentclass should have two additional attributes:thesis_topicandsupervisor. Initialize the attributes and print them. - Hierarchical Inheritance: Create
HybridVehicleinheriting fromElectricVehicleandGasVehicle. Define two base classes:ElectricVehicleandGasVehicle. Each class should have attributes specific to the vehicle type. Create a derived class calledHybridVehiclethat inherits from bothElectricVehicleandGasVehicle. Initialize the attributes and print them. - Method Overriding: Override
calculate_interest()method inSavingsAccount. Create a base class calledBankAccountwith a methodcalculate_interest(). Create a derived class calledSavingsAccountthat overrides thecalculate_interest()method. Consider the savings account's interest rate and balance. - Constructor Inheritance: Create
EmployeeandManagerclasses. Create a base class calledEmployeewith an__init__()method that initializesnameandsalary. Create a derived class calledManagerthat inherits fromEmployee. TheManagerclass should have an additional attribute:department. Initialize the attributes and print them. - Inheriting Class Variables: Access
University'snum_studentsinDepartmentclass. Define a base class calledUniversitywith a class variablenum_students. Create a derived class calledDepartmentthat inherits fromUniversity. Access and print thenum_studentsvariable from theDepartmentclass. - Abstract Base Class: Implement
Shape,Circle,Rectangle, andTriangleclasses. Create an abstract base class calledShapewith abstract methodsarea()andperimeter(). Create concrete derived classesCircle,Rectangle, andTrianglethat implement these methods. Consider the shapes' dimensions. - Inheritance with Encapsulation: Create
CustomerandPremiumCustomerclasses. Create a base class calledCustomerwith private attributesnameandemail. Provideget_name()andget_email()methods to access these attributes. Create a derived class calledPremiumCustomerthat inherits fromCustomer. Access and print the private attributes using the getter methods. - Complex Inheritance Scenario: Create
Vehicle,Car,Truck,Motorcycle, andElectricCarclasses. Create a base class calledVehiclewith attributescolorandmax_speed. Create derived classesCar,Truck, andMotorcyclethat inherit fromVehicle. Then, create another derived classElectricCarthat inherits from bothCarandElectricVehicle. Initialize the attributes and print them. - Multiple Inheritance: Create
Person,Employee, andManagerclasses using multiple inheritance. Define a base class calledPersonwith attributesnameandage. Define another base class calledEmployeewith an attributeemployee_id. Then, create a derived class calledManagerthat inherits from bothPersonandEmployee. 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, andDclasses. Define a classAwith a methodshow(). Create two classesBandCthat inherit fromAand override theshow()method. Finally, create a classDthat inherits from bothBandC. Call theshow()method fromDto demonstrate method resolution order (MRO). - Super() Function: Use
super()to call parent class methods in aParentandChildclass. Create a base class calledParentwith a methodgreet()that prints a greeting. Then, create a derived class calledChildthat 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, andZclasses. Define three classesX,Y, andZwith methods having the same namedisplay(). Create a classXYZthat inherits fromX,Y, andZ. Call thedisplay()method fromXYZand observe which method gets called first. - Interface Implementation: Create an interface
Flyableand classesBirdandAirplanethat implement this interface. Define an interfaceFlyablewith a methodfly(). Then, create two classes:BirdandAirplanethat implement thefly()method. Call thefly()method from instances of both classes. - Polymorphism with Inheritance: Demonstrate polymorphism with
AnimalandDog,Catclasses. Define a base classAnimalwith a methodmake_sound(). Then, create two derived classes:DogandCatthat override themake_sound()method. Callmake_sound()from both derived class instances and demonstrate polymorphism. - Class Hierarchy: Build a class hierarchy with
Appliance,WashingMachine, andRefrigeratorclasses. Define a base classAppliancewith an attributebrand. Then, create two derived classesWashingMachineandRefrigeratorthat add specific attributes. Initialize the attributes and print them for both objects. - Delegation Pattern: Implement the delegation pattern with
PrinterandLaserPrinterclasses. Create a classPrinterwith a methodprint_document(). Then, create a classLaserPrinterthat delegates the task toPrinterusing composition. Callprint_document()from an instance ofLaserPrinter. - Composition over Inheritance: Demonstrate composition over inheritance using
EngineandCarclasses. Define a classEnginewith attributeshorsepowerandtype. Then, create a classCarthat usesEngineas a component. Initialize both theCarandEngineattributes and print them. - Mixin Classes: Implement mixin classes with
LoggerMixinandDatabaseclasses. Create a mixin class calledLoggerMixinthat adds alog()method. Then, create a classDatabasethat usesLoggerMixinas a mixin and adds its own methodconnect(). Demonstrate calling bothlog()andconnect()methods from an instance ofDatabase.