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
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):
✗ Incorrect
In Python, inheritance is shown by putting the parent class name in parentheses after the child class name.
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.
✗ Incorrect
The child inherits methods from the parent, so it uses the parent's walk() method.
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.
✗ Incorrect
super() is used to call a method from the parent class inside the child 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>.
✗ Incorrect
Child classes can add new attributes and methods beyond what the parent has.
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.
✗ Incorrect
Method overriding means the child class defines a method with the same name to replace the parent's version.
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.
Practice
(1/5)
1. What does it mean when a child class inherits from a parent class in Python?
easy
A. The child class must redefine all methods of the parent class to use them.
B. The child class can only use methods but not attributes of the parent class.
C. The child class automatically has all attributes and methods of the parent class.
D. The child class cannot add any new methods or attributes.
Solution
Step 1: Understand inheritance basics
Inheritance means the child class gets all features (attributes and methods) of the parent class automatically.
Step 2: Analyze each option
The child class automatically has all attributes and methods of the parent class. correctly states this. Options B, C, and D are incorrect because they limit or deny inheritance features.
Final Answer:
The child class automatically has all attributes and methods of the parent class. -> Option C
Quick Check:
Inheritance = automatic access to parent features [OK]
Hint: Inheritance means child gets all parent features automatically [OK]
Common Mistakes:
Thinking child must redefine parent methods
Believing child cannot add new features
Assuming attributes are not inherited
2. Which of the following is the correct syntax to make class Dog inherit from class Animal in Python?
easy
A. class Dog(Animal):
B. class Dog -> Animal:
C. class Dog inherits Animal:
D. class Dog : Animal
Solution
Step 1: Recall Python inheritance syntax
In Python, inheritance is shown by putting the parent class name in parentheses after the child class name.
Step 2: Match syntax to options
class Dog(Animal): uses class Dog(Animal): which is correct. Others use invalid syntax.
Final Answer:
class Dog(Animal): -> Option A
Quick Check:
Inheritance syntax = class Child(Parent): [OK]
Hint: Use parentheses with parent class name after child class [OK]
Common Mistakes:
Using 'inherits' keyword instead of parentheses
Using arrow or colon incorrectly
Omitting parentheses
3. What will be the output of this code?
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
pass
c = Child()
print(c.greet())
medium
A. Hello from Child
B. Hello from Parent
C. AttributeError
D. SyntaxError
Solution
Step 1: Understand method inheritance
Child class inherits greet method from Parent because it has no own greet.
Step 2: Trace the method call
Calling c.greet() runs Parent's greet returning "Hello from Parent".
Final Answer:
Hello from Parent -> Option B
Quick Check:
Inherited method runs if child has none [OK]
Hint: Child uses parent's method if not overridden [OK]
Common Mistakes:
Expecting child's own greet method when none exists
Confusing AttributeError with missing method
Thinking syntax error occurs
4. Find the error in this code:
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound():
return "Bark"
d = Dog()
print(d.sound())
medium
A. print statement syntax error
B. Dog class should not inherit Animal
C. Cannot override methods in child class
D. Missing self parameter in Dog's sound method
Solution
Step 1: Check method definitions
In Python, instance methods must have self as first parameter.
Step 2: Identify error in Dog's sound
Dog's sound method lacks self, causing a TypeError when called on instance.
Final Answer:
Missing self parameter in Dog's sound method -> Option D
Quick Check:
Instance methods need self parameter [OK]
Hint: Always include self as first method parameter [OK]