The super() function helps you use methods from a parent class inside a child class easily. It avoids repeating code and keeps things organized.
Super function usage in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
super().method_name(arguments)super() calls the parent class method with the same name.
You usually use it inside a method of a child class.
Examples
greet method first, then adds its own message.Python
class Parent: def greet(self): print("Hello from Parent") class Child(Parent): def greet(self): super().greet() print("Hello from Child")
Dog class uses super() to call Animal's sound method, then adds "Bark".Python
class Animal: def sound(self): print("Some sound") class Dog(Animal): def sound(self): super().sound() print("Bark")
Sample Program
This program shows how the Car class uses super() to run the start method from Vehicle, then adds its own message.
Python
class Vehicle: def start(self): print("Vehicle started") class Car(Vehicle): def start(self): super().start() print("Car is ready to go") my_car = Car() my_car.start()
Important Notes
If you forget to use super(), the parent method won't run automatically.
Using super() helps when you change the parent class later; child classes still work correctly.
Summary
super() lets child classes use parent class methods easily.
It helps avoid repeating code and keeps your program organized.
Use it inside child class methods to add or extend behavior from the parent.
Practice
1. What is the main purpose of using
super() in a child class?easy
Solution
Step 1: Understand what
super()doessuper()is used to access methods from the parent class inside a child class.Step 2: Identify the correct purpose
Calling a parent class method helps reuse code and extend functionality.Final Answer:
To call a method from the parent class -> Option AQuick Check:
super() calls parent method = A [OK]
Hint: Remember: super() means 'call parent method' [OK]
Common Mistakes:
- Thinking super() creates new objects
- Believing super() deletes classes
- Assuming super() overrides child methods fully
2. Which of the following is the correct syntax to call a parent class method
greet inside a child class method using super()?easy
Solution
Step 1: Recall the syntax of super()
The correct way to call a parent method is usingsuper()followed by dot and method name.Step 2: Match the correct option
Onlysuper().greet()uses the right parentheses and dot notation.Final Answer:
super().greet() -> Option AQuick Check:
Use parentheses with super() = D [OK]
Hint: super() always needs parentheses before method call [OK]
Common Mistakes:
- Omitting parentheses after super
- Using square brackets instead of parentheses
- Using arrow notation which is invalid in Python
3. What will be the output of this code?
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
def greet(self):
return super().greet() + " and Child"
c = Child()
print(c.greet())medium
Solution
Step 1: Understand method calls in Child.greet()
Child's greet callssuper().greet()which runs Parent's greet returning Hello from Parent.Step 2: Combine returned strings
Child's greet adds and Child to the parent's string, so final output is Hello from Parent and Child.Final Answer:
Hello from Parent and Child -> Option CQuick Check:
super() calls parent method + extra text = A [OK]
Hint: super() returns parent result; child can add more [OK]
Common Mistakes:
- Expecting only parent's message without child addition
- Thinking super() causes error here
- Ignoring the string concatenation
4. Find the error in this code using
super():
class Base:
def show(self):
print("Base show")
class Derived(Base):
def show(self):
super.show()
print("Derived show")
d = Derived()
d.show()medium
Solution
Step 1: Identify how super() is called
The code usessuper.show()which is invalid syntax; super must be called as a function.Step 2: Correct the syntax
It should besuper().show()to properly call the parent method.Final Answer:
super.show() should be super().show() -> Option DQuick Check:
super() needs parentheses before method = C [OK]
Hint: Always use super() with parentheses before method call [OK]
Common Mistakes:
- Calling super without parentheses
- Thinking parent method is missing
- Believing overriding is not allowed
5. You want to extend a parent class
__init__ method to add a new attribute in the child class. Which code correctly uses super() to do this?
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
???
self.breed = breed
Choose the correct replacement for ???.hard
Solution
Step 1: Understand parent __init__ parameters
Animal's __init__ takesname, so we must passnameto it.Step 2: Use super() correctly in child __init__
Callingsuper().__init__(name)runs Animal's __init__ properly, then child addsbreed.Final Answer:
super().__init__(name) -> Option BQuick Check:
super() calls parent with correct args = B [OK]
Hint: Pass parent's expected args to super().__init__() [OK]
Common Mistakes:
- Passing wrong argument to super()
- Calling parent __init__ without self
- Using old super() syntax incorrectly
