Parent and child classes help organize code by sharing common features. The child class can use or change what the parent class has.
Parent and child classes in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
class ParentClass: def __init__(self, value): self.value = value class ChildClass(ParentClass): def __init__(self, value, extra): super().__init__(value) self.extra = extra
The child class name is followed by parentheses containing the parent class name.
Use super() to call the parent class's methods inside the child class.
Examples
Dog class inherits from Animal and changes the speak method.Python
class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Dog barks")
Car class adds a new property model but keeps brand from Vehicle.Python
class Vehicle: def __init__(self, brand): self.brand = brand class Car(Vehicle): def __init__(self, brand, model): super().__init__(brand) self.model = model
Sample Program
This program shows a parent class Person and a child class Student. The child class adds a student ID and changes the greeting.
Python
class Person: def __init__(self, name): self.name = name def greet(self): print(f"Hello, my name is {self.name}.") class Student(Person): def __init__(self, name, student_id): super().__init__(name) self.student_id = student_id def greet(self): print(f"Hi, I'm {self.name} and my student ID is {self.student_id}.") p = Person("Alice") p.greet() s = Student("Bob", "S12345") s.greet()
Important Notes
If the child class does not have a method, it uses the parent's method automatically.
You can add new methods or properties in the child class that the parent does not have.
Summary
Parent classes hold common features for child classes.
Child classes inherit and can change or add features.
Use super() to access parent class methods inside child classes.
Practice
1. What is the main purpose of a parent class in Python?
easy
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]
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
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]
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
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]
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
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]
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
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]
Hint: Use super() to add parent behavior inside child method [OK]
Common Mistakes:
- Expecting only child or only parent output
- Thinking super() causes error without arguments
- Ignoring string concatenation
