📘 Lesson · Lesson 86
Inheritance & Polymorphism
Two OOP Pillars
💡 At a Glance
Inheritance lets a class reuse another's code. Polymorphism lets the same method behave differently in different classes.
Inheritance
Python
class Animal:
def sound(self):
print("Some sound")
class Dog(Animal): # Dog inherits Animal
def sound(self): # overrides parent
print("Bark")
d = Dog()
d.sound()Bark
Polymorphism
Python
class Cat:
def sound(self): print("Meow")
class Cow:
def sound(self): print("Moo")
for animal in [Cat(), Cow()]:
animal.sound() # same call, different resultMeow
Moo
Summary
- Inheritance reuses parent code; child can override methods.
- Polymorphism = same method name, different behavior per class.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.