Share Your Feedback

Python OOP Inheritance Coding Exercises for Beginners

๐Ÿงช Coding Exercises

๐ŸŸข Beginner Exercises

  1. Basic Inheritance (Easy)
  2. Create a `Vehicle` class with attributes like `brand` and `year`. Then, create a `Car` class that inherits from `Vehicle` and adds an additional attribute `model`.

    Requirements

    • - Define a method in the `Vehicle` class that prints basic vehicle details. - Override this method in the `Car` class to print additional details (e.g., model).
    Input
    car = Car("Toyota", 2022, "Corolla")
    car.display_info()
    
    Expected Output
    Brand: Toyota, Year: 2022
    Model: Corolla
    

    ๐Ÿ“š View Solution


  3. Method Overriding (Easy)
  4. Create a `Person` class with a method `greet()`. Then, create a `Student` class that inherits from `Person` and overrides the `greet()` method to display a custom greeting message.

    Requirements

    • - The `Person` class should have a `name` attribute. - The `Student` class should override the `greet()` method to say "Hello, I am [name], a student".
    Input
    student = Student("Alice")
    student.greet()
    
    Expected Output
    Hello, I am Alice, a student.
    

    ๐Ÿ“š View Solution


  5. Using `super()` in Inheritance (Easy)
  6. Create a `Shape` class with a method `area()` that returns `None`. Then, create a `Rectangle` class that inherits from `Shape` and calculates the area of the rectangle using the length and width. Use `super()` to call the `Shape` constructor.

    Requirements

    • - The `Rectangle` class should accept `length` and `width` as arguments. - The `area()` method in `Rectangle` should calculate and print the area of the rectangle.
    Input
    rectangle = Rectangle(5, 3)
    print(f"Area of rectangle: {rectangle.area()}")
    
    Expected Output
    Area of rectangle: 15
    

    ๐Ÿ“š View Solution


  7. Multilevel Inheritance (Easy)
  8. Create a class `Animal` with a method `sound()`. Then, create a `Dog` class that inherits from `Animal` and overrides the `sound()` method. Afterward, create a `Puppy` class that inherits from `Dog` and adds a new method `play()`.

    Requirements

    • - The `sound()` method should print a generic animal sound in the `Animal` class. - The `Puppy` class should print a playful message in the `play()` method.
    Input
    puppy = Puppy()
    puppy.sound()  # Inherited from Dog
    puppy.play()   # Specific to Puppy
    
    Expected Output
    Bark!
    Puppy is playing!
    

    ๐Ÿ“š View Solution


  9. Hierarchical Inheritance (Easy)
  10. Create a `Person` class with a method `info()` to display basic information. Then, create two classes `Employee` and `Student` that inherit from `Person` and override the `info()` method to show their specific details.

    Requirements

    • - The `Employee` class should display "Employee [name], [age], [position]". - The `Student` class should display "Student [name], [age], [major]".
    Input
    emp = Employee("John", 30, "Software Engineer")
    emp.info()
    
    stu = Student("Jane", 20, "Computer Science")
    stu.info()
    
    Expected Output
    Employee John, Age: 30, Position: Software Engineer
    Student Jane, Age: 20, Major: Computer Science
    

    ๐Ÿ“š View Solution


  11. Dynamic Method Resolution Order (MRO) (Easy)
  12. Create a `Bird` class, a `FlyingBird` class, and a `Penguin` class. The `Penguin` class should inherit from `Bird` and override the `fly()` method to indicate that penguins can't fly.

    Requirements

    • - Use the `super()` function to call methods from the `Bird` class, demonstrating method resolution order (MRO).
    Input
    bird = Bird()
    bird.fly()
    
    flying_bird = FlyingBird()
    flying_bird.fly()
    
    penguin = Penguin()
    penguin.fly()
    
    Expected Output
    Bird is flying.
    Flying bird is soaring high.
    Penguins cannot fly.
    

    ๐Ÿ“š View Solution


๐ŸŸก Intermediate Exercises

๐Ÿ”ด Advanced Exercises


๐Ÿง  Practice & Progress

Explore More Topics

๐Ÿฃ Python for Beginners

๐Ÿง  Python Advanced