Inheritance

"Say not you know another entirely,till you have divided an inheritance with him."

— Johann Kasper Lavater

Common


  1. Simple Inheritance: Create a Vehicle class and a Car subclass. Create a base class called Vehicle with two attributes: color and max_speed. Then, create a derived class called Car that inherits from Vehicle. The Car class should have an additional attribute: num_doors. Initialize the attributes and print them to verify that inheritance is working correctly.
  2. Inheriting Methods: Implement Shape and Rectangle classes. Define a base class called Shape with two methods: area() and perimeter(). These methods should be placeholders (i.e., not implemented). Create a derived class called Rectangle that inherits from Shape. Implement the area() and perimeter() methods in the Rectangle class, considering the rectangle's length and width.
  3. Multilevel Inheritance: Create a Person-Student-GraduateStudent hierarchy. Create a base class called Person with two attributes: name and age. Then, create a derived class called Student that inherits from Person. The Student class should have two additional attributes: roll_number and course. Next, create another derived class called GraduateStudent that inherits from Student. The GraduateStudent class should have two additional attributes: thesis_topic and supervisor. Initialize the attributes and print them.
  4. Hierarchical Inheritance: Create HybridVehicle inheriting from ElectricVehicle and GasVehicle. Define two base classes: ElectricVehicle and GasVehicle. Each class should have attributes specific to the vehicle type. Create a derived class called HybridVehicle that inherits from both ElectricVehicle and GasVehicle. Initialize the attributes and print them.
  5. Method Overriding: Override calculate_interest() method in SavingsAccount. Create a base class called BankAccount with a method calculate_interest(). Create a derived class called SavingsAccount that overrides the calculate_interest() method. Consider the savings account's interest rate and balance.
  6. Constructor Inheritance: Create Employee and Manager classes. Create a base class called Employee with an __init__() method that initializes name and salary. Create a derived class called Manager that inherits from Employee. The Manager class should have an additional attribute: department. Initialize the attributes and print them.
  7. Inheriting Class Variables: Access University's num_students in Department class. Define a base class called University with a class variable num_students. Create a derived class called Department that inherits from University. Access and print the num_students variable from the Department class.
  8. Abstract Base Class: Implement Shape, Circle, Rectangle, and Triangle classes. Create an abstract base class called Shape with abstract methods area() and perimeter(). Create concrete derived classes Circle, Rectangle, and Triangle that implement these methods. Consider the shapes' dimensions.
  9. Inheritance with Encapsulation: Create Customer and PremiumCustomer classes. Create a base class called Customer with private attributes name and email. Provide get_name() and get_email() methods to access these attributes. Create a derived class called PremiumCustomer that inherits from Customer. Access and print the private attributes using the getter methods.
  10. Complex Inheritance Scenario: Create Vehicle, Car, Truck, Motorcycle, and ElectricCar classes. Create a base class called Vehicle with attributes color and max_speed. Create derived classes Car, Truck, and Motorcycle that inherit from Vehicle. Then, create another derived class ElectricCar that inherits from both Car and ElectricVehicle. Initialize the attributes and print them.
  11. Multiple Inheritance: Create Person, Employee, and Manager classes using multiple inheritance. Define a base class called Person with attributes name and age. Define another base class called Employee with an attribute employee_id. Then, create a derived class called Manager that inherits from both Person and Employee. Initialize the attributes and print them to verify the working of multiple inheritance.
  12. Diamond Problem Resolution: Demonstrate the diamond problem in Python using A, B, C, and D classes. Define a class A with a method show(). Create two classes B and C that inherit from A and override the show() method. Finally, create a class D that inherits from both B and C. Call the show() method from D to demonstrate method resolution order (MRO).
  13. Super() Function: Use super() to call parent class methods in a Parent and Child class. Create a base class called Parent with a method greet() that prints a greeting. Then, create a derived class called Child that overrides the greet() method but also calls the parent class's greet() method using super(). Print both messages to verify.
  14. MRO Exploration: Explore method resolution order (MRO) with X, Y, and Z classes. Define three classes X, Y, and Z with methods having the same name display(). Create a class XYZ that inherits from X, Y, and Z. Call the display() method from XYZ and observe which method gets called first.
  15. Interface Implementation: Create an interface Flyable and classes Bird and Airplane that implement this interface. Define an interface Flyable with a method fly(). Then, create two classes: Bird and Airplane that implement the fly() method. Call the fly() method from instances of both classes.
  16. Polymorphism with Inheritance: Demonstrate polymorphism with Animal and Dog, Cat classes. Define a base class Animal with a method make_sound(). Then, create two derived classes: Dog and Cat that override the make_sound() method. Call make_sound() from both derived class instances and demonstrate polymorphism.
  17. Class Hierarchy: Build a class hierarchy with Appliance, WashingMachine, and Refrigerator classes. Define a base class Appliance with an attribute brand. Then, create two derived classes WashingMachine and Refrigerator that add specific attributes. Initialize the attributes and print them for both objects.
  18. Delegation Pattern: Implement the delegation pattern with Printer and LaserPrinter classes. Create a class Printer with a method print_document(). Then, create a class LaserPrinter that delegates the task to Printer using composition. Call print_document() from an instance of LaserPrinter.
  19. Composition over Inheritance: Demonstrate composition over inheritance using Engine and Car classes. Define a class Engine with attributes horsepower and type. Then, create a class Car that uses Engine as a component. Initialize both the Car and Engine attributes and print them.
  20. Mixin Classes: Implement mixin classes with LoggerMixin and Database classes. Create a mixin class called LoggerMixin that adds a log() method. Then, create a class Database that uses LoggerMixin as a mixin and adds its own method connect(). Demonstrate calling both log() and connect() methods from an instance of Database.

Find it difficult?

Don’t lose heart, don’t be under confident, just be consistent in your preparation and be sure of everything you’ve studied. You can request a class so that we can help you understant this topic.

Feel Confident?

Your first step in learning any new topic is to be aware of your strengths and weaknesses. Once you know this, your self-preparation can be meaningful and result-oriented. Attempt an quiz to get tested.