Inheritance
π§ Concept:
Child class inherits data + behavior from parent class.
πΌ Real-world idea:
A Manager is an Employee but with extra privileges.
Creation Question
Create a base class Employee with attributes name, salary.
Create subclass Manager(Employee) with extra attribute department.
Add a method show_info() showing all details.
Debugging Question
class Vehicle:
def move(self):
print("Moving")
class Car(Vehicle):
def wheels():
print("4 wheels")
c = Car()
c.move()
c.wheels()
π― Bug: parameter error in subclass method.
** Polymorphism**
π§ Concept:
Same function name, different behavior per class.
πΌ Real-world idea:
Payment method β can be through CreditCard, UPI, or Cash.
Creation Question
Create 3 classes:
CreditCard, UPI, and Cash β each with a pay(amount) method showing how payment is made.
Then loop through all and call .pay() β observe polymorphism.
Debugging Question
class Dog:
def speak(self):
print("Woof!")
class Cat:
def speak():
print("Meow!")
for pet in [Dog(), Cat()]:
pet.speak()
π― Bug: missing argument in one classβs method definition.
Top comments (0)