What if you could write shared code once and magically create many unique versions without repeating yourself?
Why Parent and child classes in Python? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have to create many similar objects, like different types of vehicles, and you write all their details separately for each one.
This means repeating the same information over and over, making your code long, confusing, and easy to mess up when you want to change something.
Using parent and child classes lets you write common features once in a parent, then create child classes that add or change only what is different, saving time and avoiding mistakes.
class Car: def __init__(self): self.wheels = 4 self.engine = 'gas' class Truck: def __init__(self): self.wheels = 6 self.engine = 'diesel'
class Vehicle: def __init__(self, wheels, engine): self.wheels = wheels self.engine = engine class Car(Vehicle): def __init__(self): super().__init__(4, 'gas') class Truck(Vehicle): def __init__(self): super().__init__(6, 'diesel')
It makes your code cleaner and easier to grow by sharing common parts and customizing only what changes.
Think of a video game where many characters share basic moves but have unique powers; parent and child classes help organize their abilities neatly.
Parent classes hold shared features.
Child classes add or change details.
This reduces repeated code and errors.
Practice
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 AQuick Check:
Parent class = common features [OK]
- Thinking parent classes prevent changes in children
- Believing parent classes are only for direct instances
- Confusing overriding with inheritance
Dog that inherits from a parent class 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): usesclass Dog(Animal):which is correct. Other options use incorrect keywords or symbols not valid in Python.Final Answer:
class Dog(Animal): -> Option DQuick Check:
Child class syntax = class Child(Parent): [OK]
- Using 'inherits' or 'extends' keywords (not Python)
- Using symbols like '<' instead of parentheses
- Omitting the colon at the end
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
def greet(self):
return "Hello from Child"
obj = Child()
print(obj.greet())Solution
Step 1: Understand method overriding in child class
The child classChilddefines its owngreetmethod, which replaces the parent's method when called on a child instance.Step 2: Check which method is called
Sinceobjis an instance ofChild, callingobj.greet()uses the child's method, returning "Hello from Child".Final Answer:
Hello from Child -> Option CQuick Check:
Child method overrides parent method = Hello from Child [OK]
- Thinking parent method runs instead of child's
- Expecting combined output from both methods
- Assuming method not found error
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)Solution
Step 1: Check constructor chaining in child class
The child class__init__method setsagebut does not callsuper().__init__(name)to setnamefrom the parent.Step 2: Understand consequence of missing super call
Becausenameis not set inChild, accessingc.namewill cause an error or missing attribute.Final Answer:
Child class __init__ does not call Parent __init__, so name is missing -> Option AQuick Check:
Missing super() call = missing parent attributes [OK]
- Forgetting to call super().__init__ in child constructor
- Assuming parent attributes set automatically
- Confusing syntax errors with logic errors
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()Solution
Step 1: Understand super() usage in child describe method
The child method callssuper().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 BQuick Check:
super() calls parent method, combined output [OK]
- Expecting only child or only parent output
- Thinking super() causes error without arguments
- Ignoring string concatenation
