Bird
Raised Fist0
Pythonprogramming~5 mins

Parent and child classes in Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

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 is a parent class in Python?
A parent class is a class that provides attributes and methods which can be inherited by other classes called child classes. It is also known as a base or superclass.
Click to reveal answer
beginner
What does a child class do in Python?
A child class inherits attributes and methods from a parent class. It can also add new features or change existing ones.
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. For example: <br><code>class Child(Parent):</code>
Click to reveal answer
intermediate
What happens if a child class has a method with the same name as the parent class?
The child class method overrides the parent class method. When called on a child object, the child's version runs.
Click to reveal answer
beginner
Why use parent and child classes in programming?
They help reuse code, organize related features, and make programs easier to understand and maintain by grouping shared behavior in the parent class.
Click to reveal answer
How do you indicate that a class Child inherits from a class Parent in Python?
Aclass Child extends Parent:
Bclass Child(Parent):
Cclass Child -> Parent:
Dclass Child inherits Parent:
If a child class defines a method with the same name as the parent class, which method is used when called on a child object?
AChild class method
BParent class method
CBoth methods run
DAn error occurs
What is the main benefit of using parent and child classes?
ATo reuse code and organize related features
BTo make code longer
CTo avoid using functions
DTo make programs run slower
Which term is NOT related to parent and child classes?
AInheritance
BEncapsulation
CPolymorphism
DCompilation
What keyword is used to call the parent class constructor inside a child class in Python?
Abase()
Bparent()
Csuper()
Dthis()
Explain how a child class inherits from a parent class and how method overriding works.
Think about how you can reuse and change behavior from the parent.
You got /3 concepts.
    Describe why using parent and child classes can make your code easier to manage.
    Consider how grouping shared things helps in real life.
    You got /4 concepts.

      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

      1. 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.
      2. 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.
      3. Final Answer:

        To hold common features that child classes can inherit -> Option A
      4. 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

      1. 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.
      2. 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.
      3. Final Answer:

        class Dog(Animal): -> Option D
      4. 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

      1. 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.
      2. 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".
      3. Final Answer:

        Hello from Child -> Option C
      4. 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

      1. 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.
      2. 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.
      3. Final Answer:

        Child class __init__ does not call Parent __init__, so name is missing -> Option A
      4. Quick 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
      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

      1. 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".
      2. 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".
      3. Final Answer:

        I am a parent and I am a child -> Option B
      4. Quick 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