Week 10
Week 10
Code
Output
Problem Statement -2
Create a base class Vehicle with attributes make, model, and year, and a method display_info() to
print these attributes. Create a subclass Car that inherits from Vehicle and adds an attribute
mileage. Implement a method display_info() in Car to include the mileage along with the other
attributes.
Code
Output
Problem Statement -3
Define a base class Animal with a method sound() that prints the sound the animal makes. Create
subclasses Dog and Cat that inherit from Animal and override the sound() method to print the
specific sound each animal makes.
Code
Output
Problem Statement -4
Create a base class Person with attributes name and age, and a method introduce() to print the
name and age of the person. Create a subclass Student that inherits from Person and adds an
attribute student_id. Implement a method introduce() in Student to include the student ID along
with the name and age.
Code
Output
Problem Statement -5
Define a base class BankAccount with attributes account_number, balance, and methods deposit()
and withdraw() to add or subtract funds from the account. Create a subclass SavingsAccount that
inherits from BankAccount and adds an attribute interest_rate. Implement a method add_interest()
in SavingsAccount to calculate and add interest to the account balance.
Code
Code
Conclusion
The experiment demonstrates inheritance and polymorphism by reusing and extending base class
attributes and methods. It showcases practical applications in real-world scenarios, highlighting
the flexibility and efficiency of object-oriented programming in Python.