Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Super function usage
📖 Scenario: You are creating a simple program with two classes: a parent class and a child class. The child class will use the super() function to call a method from the parent class. This is like asking your older sibling for help before doing your own work.
🎯 Goal: Build two classes where the child class uses super() to call the parent class method and then adds its own message.
📋 What You'll Learn
Create a parent class called Animal with a method sound() that returns the string 'Animal makes a sound'.
Create a child class called Dog that inherits from Animal.
In the Dog class, override the sound() method and use super().sound() to get the parent message.
Add the string ' and Dog barks' to the message from the parent method in the child method.
Create an object of class Dog and print the result of calling its sound() method.
💡 Why This Matters
🌍 Real World
Using <code>super()</code> is common when building programs with many related classes, like in games, apps, or websites where objects share common features.
💼 Career
Understanding <code>super()</code> helps you work with object-oriented programming, which is a key skill for many software development jobs.
Progress0 / 4 steps
1
Create the parent class
Create a class called Animal with a method sound() that returns the string 'Animal makes a sound'.
Python
Hint
Use class Animal: to start the class and define sound(self) method that returns the exact string.
2
Create the child class
Create a class called Dog that inherits from Animal. Inside it, define a method sound(self) that uses super().sound() to get the parent message and adds ' and Dog barks' to it.
Python
Hint
Use class Dog(Animal): to inherit. Inside sound, call super().sound() and add the extra string.
3
Create a Dog object
Create an object called dog of class Dog.
Python
Hint
Use dog = Dog() to create the object.
4
Print the Dog sound
Print the result of calling dog.sound().
Python
Hint
Use print(dog.sound()) to show the combined message.
Practice
(1/5)
1. What is the main purpose of using super() in a child class?
easy
A. To call a method from the parent class
B. To create a new instance of the child class
C. To delete the parent class
D. To override the child class method completely
Solution
Step 1: Understand what super() does
super() 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 A
Quick 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
A. super().greet()
B. super->greet()
C. super[greet]()
D. super.greet()
Solution
Step 1: Recall the syntax of super()
The correct way to call a parent method is using super() followed by dot and method name.
Step 2: Match the correct option
Only super().greet() uses the right parentheses and dot notation.
Final Answer:
super().greet() -> Option A
Quick 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
A. Hello from Parent
B. Hello from Child
C. Hello from Parent and Child
D. Error: super() not used correctly
Solution
Step 1: Understand method calls in Child.greet()
Child's greet calls super().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 C
Quick 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
A. Derived class should not override show()
B. Base class method show() is missing
C. print statements are incorrect
D. super.show() should be super().show()
Solution
Step 1: Identify how super() is called
The code uses super.show() which is invalid syntax; super must be called as a function.
Step 2: Correct the syntax
It should be super().show() to properly call the parent method.
Final Answer:
super.show() should be super().show() -> Option D
Quick 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
A. super(Dog, self).__init__(breed)
B. super().__init__(name)
C. Animal.__init__(self, breed)
D. super().__init__(breed)
Solution
Step 1: Understand parent __init__ parameters
Animal's __init__ takes name, so we must pass name to it.
Step 2: Use super() correctly in child __init__
Calling super().__init__(name) runs Animal's __init__ properly, then child adds breed.
Final Answer:
super().__init__(name) -> Option B
Quick Check:
super() calls parent with correct args = B [OK]
Hint: Pass parent's expected args to super().__init__() [OK]