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
Parent and Child Classes
📖 Scenario: Imagine you are creating a simple program to manage vehicles. You want to have a general vehicle type and a specific type for cars.
🎯 Goal: You will create a parent class called Vehicle and a child class called Car that inherits from Vehicle. You will then create a car object and display its details.
📋 What You'll Learn
Create a parent class named Vehicle with an __init__ method that sets make and model attributes.
Create a child class named Car that inherits from Vehicle and adds an attribute year.
Create an instance of Car with specific values for make, model, and year.
Print the car's details using the attributes.
💡 Why This Matters
🌍 Real World
Understanding parent and child classes helps organize code for things like vehicles, animals, or employees where many share common traits but also have unique features.
💼 Career
Inheritance is a key concept in many programming jobs to write clean, reusable, and organized code.
Progress0 / 4 steps
1
Create the parent class Vehicle
Create a parent class called Vehicle with an __init__ method that takes make and model as parameters and sets them as attributes.
Python
Hint
Use class Vehicle: to start the class. Inside, define def __init__(self, make, model): and set self.make = make and self.model = model.
2
Create the child class Car
Create a child class called Car that inherits from Vehicle. Add an __init__ method that takes make, model, and year as parameters. Call the parent __init__ method for make and model, and set self.year = year.
Python
Hint
Use class Car(Vehicle): to inherit. Inside __init__, call super().__init__(make, model) and then set self.year = year.
3
Create a Car object
Create an object called my_car from the Car class with make as 'Toyota', model as 'Corolla', and year as 2020.
Python
Hint
Create the object by calling Car('Toyota', 'Corolla', 2020) and assign it to my_car.
4
Print the car details
Print the details of my_car in this exact format: "Toyota Corolla 2020" using the attributes make, model, and year.
Python
Hint
Use print(f"{my_car.make} {my_car.model} {my_car.year}") to display the details.
Practice
(1/5)
1. What is the main purpose of a parent class in Python?
easy
A. To hold common features that child classes can inherit
B. To override methods in child classes
C. To create instances directly without child classes
D. To prevent child classes from adding new features
Solution
Step 1: Understand the role of parent class
A parent class is designed to hold common features like methods and attributes that multiple child classes can share.
Step 2: Compare options with this role
To hold common features that child classes can inherit correctly states this purpose. Other options describe incorrect or unrelated roles.
Final Answer:
To hold common features that child classes can inherit -> Option A
Quick Check:
Parent class = common features [OK]
Hint: Parent class shares features for children to reuse [OK]
Common Mistakes:
Thinking parent classes prevent changes in children
Believing parent classes are only for direct instances
Confusing overriding with inheritance
2. Which of the following is the correct syntax to define a child class Dog that inherits from a parent class Animal?
easy
A. class Dog extends Animal:
B. class Dog inherits Animal:
C. class Dog < Animal:
D. class Dog(Animal):
Solution
Step 1: Recall Python inheritance syntax
In Python, a child class inherits from a parent class by placing the parent class name in parentheses after the child class name.
Step 2: Match options with correct syntax
class Dog(Animal): uses class Dog(Animal): which is correct. Other options use incorrect keywords or symbols not valid in Python.
Final Answer:
class Dog(Animal): -> Option D
Quick Check:
Child class syntax = class Child(Parent): [OK]
Hint: Use parentheses with parent class name after child class [OK]
Common Mistakes:
Using 'inherits' or 'extends' keywords (not Python)
Using symbols like '<' instead of parentheses
Omitting the colon at the end
3. What will be the output of this code?
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
def greet(self):
return "Hello from Child"
obj = Child()
print(obj.greet())
medium
A. Hello from ParentChild
B. Hello from Parent
C. Hello from Child
D. Error: greet method not found
Solution
Step 1: Understand method overriding in child class
The child class Child defines its own greet method, which replaces the parent's method when called on a child instance.
Step 2: Check which method is called
Since obj is an instance of Child, calling obj.greet() uses the child's method, returning "Hello from Child".
Final Answer:
Hello from Child -> Option C
Quick Check:
Child method overrides parent method = Hello from Child [OK]
Hint: Child method overrides parent method when called on child [OK]
Common Mistakes:
Thinking parent method runs instead of child's
Expecting combined output from both methods
Assuming method not found error
4. Find the error in this code:
class Parent:
def __init__(self, name):
self.name = name
class Child(Parent):
def __init__(self, name, age):
self.age = age
c = Child('Anna', 10)
print(c.name, c.age)
medium
A. Child class __init__ does not call Parent __init__, so name is missing
B. Syntax error in class definition
C. Cannot create Child instance with two arguments
D. print statement syntax is wrong
Solution
Step 1: Check constructor chaining in child class
The child class __init__ method sets age but does not call super().__init__(name) to set name from the parent.
Step 2: Understand consequence of missing super call
Because name is not set in Child, accessing c.name will cause an error or missing attribute.
Final Answer:
Child class __init__ does not call Parent __init__, so name is missing -> Option A
Hint: Always call super().__init__ in child __init__ to set parent attributes [OK]
Common Mistakes:
Forgetting to call super().__init__ in child constructor
Assuming parent attributes set automatically
Confusing syntax errors with logic errors
5. Given these classes, what will print(c.describe()) output?
class Parent:
def describe(self):
return "I am a parent"
class Child(Parent):
def describe(self):
parent_desc = super().describe()
return parent_desc + " and I am a child"
c = Child()
hard
A. I am a parent
B. I am a parent and I am a child
C. I am a child
D. Error: super() used incorrectly
Solution
Step 1: Understand super() usage in child describe method
The child method calls super().describe() which runs the parent method returning "I am a parent".
Step 2: Combine parent and child strings
The child method adds " and I am a child" to the parent's string, so the full return is "I am a parent and I am a child".
Final Answer:
I am a parent and I am a child -> Option B
Quick Check:
super() calls parent method, combined output [OK]
Hint: Use super() to add parent behavior inside child method [OK]