0% found this document useful (0 votes)
0 views3 pages

Lesson 7 Inheritance

This lesson covers inheritance in Java and Python, explaining how subclasses can inherit properties and methods from superclasses. It provides syntax comparisons between the two languages, along with examples of defining classes and overriding methods. Additionally, a mini quiz is included to reinforce understanding of the concepts presented.

Uploaded by

sopoy59260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Lesson 7 Inheritance

This lesson covers inheritance in Java and Python, explaining how subclasses can inherit properties and methods from superclasses. It provides syntax comparisons between the two languages, along with examples of defining classes and overriding methods. Additionally, a mini quiz is included to reinforce understanding of the concepts presented.

Uploaded by

sopoy59260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lesson 7: Inheritance in Java & Python

🎯 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

Define Superclass class Animal {} class Animal:

Define Subclass class Dog extends Animal {} class Dog(Animal):

Access Superclass super() super().__init__()


Constructor

Override Method @Override + method Redefine method in


subclass

🔧 Java Example
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}

class Dog extends Animal {


@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // Output: Dog barks
}
}

🐍 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() │
└──────────────┘

📋 Cheat Sheet – Inheritance


Concept Java Python

Superclass class A {} class A:

Subclass class B extends A {} class B(A):

Call Parent Constructor super() super().__init__()


Method Override @Override + redefine Redefine in child class

🧩 Mini Quiz
1. Fill in the blank:
- class Bike ______ Vehicle {} (Java)
- class Bike(____): (Python)

2. What keyword is used to call the parent constructor?

3. True or False:
- A subclass can override methods of its superclass. ____

You might also like