Lesson 7 Inheritance
Lesson 7 Inheritance
🎯 Objective:
Understand how to use inheritance to let one class derive the properties and behaviors of
another.
🧠 Concept Breakdown
Inheritance allows a class (child or subclass) to inherit fields and methods from another
class (parent or superclass).
Example: A Car is a Vehicle — Car can reuse properties like speed, start(), etc., from Vehicle.
🧾 Syntax Side-by-Side
Feature Java Python
🔧 Java Example
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
🐍 Python Example
class Animal:
def sound(self):
print("Animal makes sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
d = Dog()
d.sound() # Output: Dog barks
Visual Diagram
[Class: Animal]
┌──────────────┐
│ + sound() │
└──────────────┘
↑
inherits
↓
[Class: Dog]
┌──────────────┐
│ + sound() │
└──────────────┘
🧩 Mini Quiz
1. Fill in the blank:
- class Bike ______ Vehicle {} (Java)
- class Bike(____): (Python)
3. True or False:
- A subclass can override methods of its superclass. ____