Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Method super() | Inheritance
Object-Oriented Programming in Python

bookMethod super()

To call the parent's class method instead of the current one, we use the_super()_method in Python.

Code

We create the Cat object. The super() method inside the Cat object refers to the parent's class Animal. So Animal constructor is called first.

Afterward, go back to the Cat constructor, initialize the age attribute and print the message.

123456789101112
class Animal: def __init__(self, name): print('Calling Animal constructor') self.name = name class Cat(Animal): def __init__(self, name, age): super().__init__(name) self.age = age print('Calling Cat constructor') cat = Cat('Archie', 12)
copy
question mark

The last output is:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Ask me questions about this topic

Summarize this chapter

Show real-world examples

Awesome!

Completion rate improved to 7.69

bookMethod super()

Swipe to show menu

To call the parent's class method instead of the current one, we use the_super()_method in Python.

Code

We create the Cat object. The super() method inside the Cat object refers to the parent's class Animal. So Animal constructor is called first.

Afterward, go back to the Cat constructor, initialize the age attribute and print the message.

123456789101112
class Animal: def __init__(self, name): print('Calling Animal constructor') self.name = name class Cat(Animal): def __init__(self, name, age): super().__init__(name) self.age = age print('Calling Cat constructor') cat = Cat('Archie', 12)
copy
question mark

The last output is:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
some-alt