Python
Module 7: Python Object-Oriented ProgrammingBeginner20 min read

Inheritance & Polymorphism

Reuse and extend code with parent classes, super(), and method overriding.

Key Takeaways & Core Concepts
  • Parent class (base class): The class being inherited from.
  • Child class (derived class): The class that inherits from another class.

Inheritance & Polymorphism

### Inheritance in Python

Inheritance allows us to define a class that inherits all the methods and properties from another class:

  • Parent class (base class): The class being inherited from.
  • Child class (derived class): The class that inherits from another class.
PYTHON Code Example
1class Employee:
2 def __init__(self, name, emp_id):
3 self.name = name
4 self.emp_id = emp_id
5
6 def get_info(self):
7 return f"{self.name} (ID: {self.emp_id})"
8
9class SoftwareEngineer(Employee):
10 def __init__(self, name, emp_id, tech_stack):
11 super().__init__(name, emp_id)
12 self.tech_stack = tech_stack
13
14 # Polymorphic method override
15 def get_info(self):
16 base_info = super().get_info()
17 return f"{base_info} - Stack: {', '.join(self.tech_stack)}"
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
17 lines
Polymorphism with Different Classes
1class Dog:
2 def speak(self):
3 return "Woof!"
4
5class Cat:
6 def speak(self):
7 return "Meow!"
8
9animals = [Dog(), Cat()]
10for a in animals:
11 print(a.speak())
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
11 lines
Note: Polymorphism enables calling speak() without needing to check the concrete animal type.
Practice & AssessmentPython Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)