super()

🌟 What is super() in Python?

Let’s suppose...

Your parents built a small room in your house. Now you grow up and want to add LED lights, new paint, and furniture. But you still want to keep the original room structure, not remove it.

That’s exactly what super() does in OOP.

It lets the child class use the parent class's logic, without rewriting it.

So you can build your own version on top of the parent’s version.


🔹 What is super()?

  • It’s a built-in function in Python.

  • Used inside a child class to call:

    • Parent’s constructor

    • Parent’s methods

    • Parent’s properties


💡 With Constructor Example

class BankAccount:
    def __init__(self, account_holder):
        self.account_holder = account_holder
        print("Parent constructor called")

class SavingsAccount(BankAccount):
    def __init__(self, account_holder, balance):
        super().__init__(account_holder)  # calling parent constructor
        self.balance = balance
        print("Child constructor called")

Now if you create:

acc = SavingsAccount("Maham", 500)

Output:

Parent constructor called  
Child constructor called

➡ So, super().__init__() → brings in parent constructor


🧠 But Can I Use self.account_holder Directly?

Yes! If parent has already set self.account_holder, you can access it directly using self.

print(self.account_holder)  # ✅ works

You don’t need to use super() just to access a property.


🔹 Then Why Use super()?

Because if the parent constructor has logic, like setting up properties, validations, print statements, etc., and you skip super(), then that logic won't run.

So always use super() when you want to reuse parent logic.


💡 Without Constructor Example (Calling Method)

class BankAccount:
    def show(self):
        print("Showing account info")

class SavingsAccount(BankAccount):
    def show(self):
        super().show()  # calling parent method
        print("Showing savings account info")

Why super().show() instead of self.show()?

If you use self.show(), it will call the child’s own method, and it will keep calling itself (infinite loop or override).

But super().show() → jumps to parent method


🪜 Step-by-step Rules of super()

What You Want to Do

Use super()?

Why

Call parent constructor?

✅ Yes

Reuse logic inside __init__()

Use parent property set in constructor?

❌ No

Just use self.property

Call parent method in overridden method?

✅ Yes

Avoid recursion / loop

Access class-level property?

❌ No

Access with ClassName.property


🤔 What If I Don't Use super()?

  • The parent constructor or method will not be called

  • So, any logic written inside parent will be skipped

  • Only the child class logic will run

Sometimes it's fine, but mostly not safe


🧬 What About Multiple Inheritance?

class A:
    def show(self):
        print("A show")

class B:
    def show(self):
        print("B show")

class C(A, B):
    def show(self):
        super().show()
        print("C show")

Now if you do:

obj = C()
obj.show()

Output:

A show  
C show

Because Python follows MRO (Method Resolution Order), so it goes left to right.

Here super().show() picked A’s method, not B’s.


❓ Common Questions You Asked (Now Answered)

  • Can I access parent property without super()?
    → Yes, just use self.property

  • Why not use self.show() instead of super().show()?
    → Because self.show() means → call current method (child), not parent

  • Do I need constructor in child always?
    → No. If child doesn’t have its own __init__(), Python uses parent’s constructor automatically

  • What if parent has constructor, child doesn’t?
    → Then no need to write super() — it’ll use parent constructor by default

  • Can I still use super() without a constructor?
    → Yes, to call other parent methods


🔚 Final Thoughts

Use super() when:

  • You want to reuse or extend parent’s logic

  • Avoid code repetition

  • Make sure all logic flows in the proper order

And now, you’ve truly mastered the real superpower of OOP 😄


Stay Connected - @syedamahamfahim 🐬

Updated on