0
0
Pythonprogramming~15 mins

Super function usage in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a hint?

Use dog = Dog() to create the object.

4
Print the Dog sound
Print the result of calling dog.sound().
Python
Need a hint?

Use print(dog.sound()) to show the combined message.