NotesPythonObject-Oriented Programming

Inheritance & Polymorphism

Extending classes with super(), overriding methods, and treating different types uniformly.

A subclass extends a base ("parent") class with class Sub(Base):, inheriting all its attributes and methods, and can add or override its own.

class Creator:
    def __init__(self, name):
        self.name = name

    def upload(self):
        return f"{self.name} uploaded content"

class YouTuber(Creator):
    def __init__(self, name, subscribers):
        super().__init__(name)          # calls Creator.__init__ to set self.name
        self.subscribers = subscribers

    def upload(self):                    # overrides Creator.upload
        return f"{self.name} uploaded a video to {self.subscribers:,} subscribers"

munna = YouTuber("CodeWithMunnaX", 250_000)
print(munna.upload())        # uses YouTuber's version, not Creator's
print(isinstance(munna, Creator))   # True — a YouTuber IS-A Creator

super().__init__(name) calls the parent class's constructor so you don't have to duplicate its setup logic — a very common pattern when a subclass needs everything the parent has, plus a bit more.

Overriding vs. extending a method

You can also call the parent's version of a method and then add to it, instead of fully replacing it:

class YouTuber(Creator):
    def upload(self):
        base_message = super().upload()   # get the parent's behavior
        return base_message + " (as a video!)"

Polymorphism — same method call, different behavior

Polymorphism means you can call the same method name on different types and each does the right thing for itself, without the caller needing to know which exact type it's dealing with.

class Blogger(Creator):
    def upload(self):
        return f"{self.name} published a blog post"

creators = [YouTuber("CodeWithMunnaX", 250_000), Blogger("Some Blogger")]
for creator in creators:
    print(creator.upload())   # each runs its own version of upload()

The loop doesn't care whether creator is a YouTuber or a Blogger — it just calls .upload() and trusts each object to know how to handle it. This is the practical payoff of inheritance: code that works uniformly across a whole family of related classes.

Multiple inheritance (brief note)

Python allows a class to inherit from more than one parent: class Both(A, B):. It's powerful but can get confusing fast (method resolution order); most real code sticks to single inheritance plus composition instead.

Key points to remember
  • class Sub(Base): inherits everything from Base; super().__init__(...) calls the parent's constructor without duplicating its logic.
  • A subclass can override a parent's method entirely, or extend it by calling super().method() and adding to the result.
  • isinstance(obj, ParentClass) is True for subclass instances too — a YouTuber IS-A Creator.
  • Polymorphism means calling the same method name on different types and letting each one's own implementation run.
  • Python supports multiple inheritance (class Both(A, B):), but most code prefers single inheritance or composition to avoid resolution-order confusion.

Overriding upload() across a class family

Revision Flashcards

1 / 3

Tap a card to flip it and see the answer.