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`.
car = Car("Toyota", 2022, "Corolla")
car.display_info()
Brand: Toyota, Year: 2022
Model: Corolla
๐ View Solution
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.
student = Student("Alice")
student.greet()
Hello, I am Alice, a student.
๐ View Solution
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.
rectangle = Rectangle(5, 3)
print(f"Area of rectangle: {rectangle.area()}")
Area of rectangle: 15
๐ View Solution
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()`.
puppy = Puppy()
puppy.sound() # Inherited from Dog
puppy.play() # Specific to Puppy
Bark!
Puppy is playing!
๐ View Solution
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.
emp = Employee("John", 30, "Software Engineer")
emp.info()
stu = Student("Jane", 20, "Computer Science")
stu.info()
Employee John, Age: 30, Position: Software Engineer
Student Jane, Age: 20, Major: Computer Science
๐ View Solution
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.
bird = Bird()
bird.fly()
flying_bird = FlyingBird()
flying_bird.fly()
penguin = Penguin()
penguin.fly()
Bird is flying.
Flying bird is soaring high.
Penguins cannot fly.
๐ View Solution