0
0
Pythonprogramming~5 mins

Inheriting attributes and methods in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to inherit attributes and methods in Python?
It means a new class (child) can use the properties (attributes) and actions (methods) of an existing class (parent) without rewriting them.
Click to reveal answer
beginner
How do you define a child class that inherits from a parent class in Python?
You write the child class name followed by parentheses containing the parent class name, like: <code>class Child(Parent):</code>
Click to reveal answer
beginner
If a child class does not have a method, but the parent class does, what happens when you call that method on a child object?
The child object uses the parent's method automatically because it inherits it.
Click to reveal answer
intermediate
What is the purpose of the super() function in inheritance?
<code>super()</code> lets the child class call a method from its parent class, often to extend or reuse the parent's behavior.
Click to reveal answer
beginner
Can a child class have its own attributes and methods different from the parent class?
Yes, a child class can add new attributes and methods or override existing ones from the parent.
Click to reveal answer
How do you indicate that a class Dog inherits from a class Animal in Python?
Aclass Dog extends Animal:
Bclass Dog inherits Animal:
Cclass Dog -> Animal:
Dclass Dog(Animal):
If a parent class has a method walk() and the child class does not, what happens when you call walk() on a child object?
AIt uses the parent's <code>walk()</code> method.
BIt causes an error because the child has no method.
CIt does nothing.
DIt calls a default method from Python.
What does the super() function do in a child class?
ACreates a new parent class.
BCalls a method from the parent class.
CDeletes the parent class.
DOverrides the parent class.
Can a child class have attributes that the parent class does not have?
ANo, child classes must have the same attributes.
BOnly if the parent allows it.
CYes, child classes can add new attributes.
DOnly if you use <code>super()</code>.
Which of these is true about method overriding in inheritance?
AChild class can provide its own version of a parent's method.
BParent class can override child's methods.
CMethods cannot be changed once inherited.
DOverriding deletes the parent's method.
Explain in your own words what happens when a child class inherits from a parent class in Python.
Think about how a child can reuse and extend what the parent has.
You got /3 concepts.
    Describe how and why you would use the super() function inside a child class method.
    Imagine you want to add something extra but keep the parent's work.
    You got /3 concepts.