Editor

Predict the super() Output

Intermediate · Predict the output · from Python Inheritance: Subclasses, Polymorphism, and super()

From the lesson: Python Inheritance: Subclasses, Polymorphism, and super()

Think about how scientists classify animals. A Dog is a type of Mammal, which is a type of Animal. Each level inherits traits from the one above it: all animals breathe, all mammals are warm-blooded, and all dogs bark.

Read the lesson

Read the code carefully and predict what it prints. Pay attention to the order of super() calls and string concatenation.

class A:
    def greet(self):
        return 'Hello'

class B(A):
    def greet(self):
        return super().greet() + ' World'

class C(B):
    def greet(self):
        return super().greet() + '!'

obj = C()
print(obj.greet())
class A:
def greet(self):
return 'Hello'
class B(A):
def greet(self):
return super().greet() + ' World'
class C(B):
def greet(self):
return super().greet() + '!'
obj = C()
print(obj.greet())

More Inheritance exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz