Inheritance ๐จโ๐ฉโ๐งโ๐ฆ๐๐
Get everything for free! Child classes inherit features from parent classes and add their own powers.
Day 27: Inheritance โ Get Everything for Free!
What's Inheritance?
When a parent passes their tools to their child, the child doesn't rebuild those tools from scratch โ they just use them! In Python, a child class automatically gets everything the parent class has. Then it can add its own extra features on top. Reuse everything, repeat nothing!
Creating a Parent and Child Class
class Person:
def __init__(self, name):
self.name = name
```
def greet(self):
print(f"Hi, I am {self.name}.")
```
class Student(Person):
def study(self):
print(f"{self.name} is studying Python.")
rohith = Student("Rohith")
rohith.greet() # inherited from Person!
rohith.study() # Student's own method
Output: Hi, I am Rohith. Rohith is studying Python. Student never defined greet() but it works โ inherited free from Person! Student only added its own study() method on top!
How Python Finds Methods
When you call rohith.greet(), Python first looks inside Student. Not found? It climbs up to Person and finds it there. This is called the lookup chain โ child first, then parent. Always!
Real World Connection
Instagram has a User class with basic login and profile features. Then AdminUser inherits from User and adds extra powers like deleting posts. PremiumUser inherits from User and adds extra features like stories. Same base, different extensions โ no code repeated!
Child Adds Its Own Twist
class Animal:
def __init__(self, name):
self.name = name
```
def speak(self):
print(f"{self.name} makes a sound.")
```
class Dog(Animal):
def speak(self):
print(f"{self.name} says Woof!")
class Cat(Animal):
def speak(self):
print(f"{self.name} says Meow!")
dog = Dog("Bruno")
cat = Cat("Whiskers")
dog.speak() # Bruno says Woof!
cat.speak() # Whiskers says Meow!
Both Dog and Cat inherited from Animal but overrode speak() with their own version. Same method name, different behaviour. This is called method overriding โ super powerful!
Common Mistakes
Mistake 1 โ Lowercase parent name.
class Student(person): # WRONG โ Python can't find "person"!
class Student(Person): # CORRECT โ capital P matches the class name!
Mistake 2 โ Empty brackets means no parent.
class Student(): # WRONG โ no parent, inherits nothing!
class Student(Person): # CORRECT โ Person is the parent!
Mini Challenge
Mini Challenge
Create a Vehicle class with a brand property and a move() method. Then create a Car class and a Bike class that both inherit from Vehicle. Add a unique method to each. Create objects of both and call all their methods. You just built the same vehicle system that Uber and Ola use to manage different transport types!
Quick Quiz
Q: How does a child class inherit from a parent? A: Put the parent name in brackets โ class Student(Person):
Q: If a method isn't in the child class, what happens? A: Python looks up to the parent class and uses it from there!
Q: Can a child class override a parent's method? A: Yes! Just define the same method name in the child with different behaviour!
Key Takeaways
Key Takeaways
- Inheritance lets child classes reuse everything from the parent for free.
- Write class Child(Parent): to inherit from a parent class.
- Child classes can add their own methods on top of inherited ones.
- Child classes can override parent methods with their own version.
- Never repeat code โ if two classes share features, put them in a parent class!
Continue Learning with Rohi
You've used your 3 free Rohi questions. Create a free account to continue learning.