0
0
Pythonprogramming~10 mins

Parent and child classes in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parent and child classes
Define Parent Class
Define Child Class inherits Parent
Create Child Object
Call Child Method
If method not in Child
Yes
Use Parent Method
End
This flow shows how a child class inherits from a parent class and uses its methods if not overridden.
Execution Sample
Python
class Parent:
    def greet(self):
        return "Hello from Parent"

class Child(Parent):
    pass

c = Child()
print(c.greet())
This code defines a parent class with a method and a child class that inherits it, then calls the inherited method.
Execution Table
StepActionEvaluationResult
1Define class ParentParent class createdParent class ready
2Define method greet in ParentMethod greet addedParent.greet() available
3Define class Child inheriting ParentChild class createdChild inherits Parent
4Create object c of Childc is instance of Childc created
5Call c.greet()Method lookup in ChildNot found in Child
6Lookup method in ParentMethod greet foundUse Parent.greet()
7Execute Parent.greet()Return string"Hello from Parent"
8Print outputOutput to consoleHello from Parent
💡 Method found in Parent, output printed, program ends
Variable Tracker
VariableStartAfter Step 4After Step 7Final
cundefinedChild instanceunchangedunchanged
Key Moments - 2 Insights
Why does c.greet() work even though Child class has no greet method?
Because the method is found in the Parent class during lookup (see execution_table step 6). Child inherits Parent's methods.
What happens if Child defines its own greet method?
The Child's greet method will be used instead of Parent's (method lookup stops at Child). This is called overriding.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the method greet found in the Parent class?
AStep 7
BStep 5
CStep 6
DStep 4
💡 Hint
Check the 'Evaluation' column for method lookup results in execution_table.
According to variable_tracker, what is the value of variable c after step 4?
Aundefined
BChild instance
CParent instance
DNone
💡 Hint
Look at variable_tracker row for c after step 4.
If we add a greet method to Child class, what changes in the execution_table?
AMethod lookup finds greet in Child at step 5
BMethod lookup still finds greet in Parent at step 6
CObject c becomes Parent instance
DOutput changes to an error
💡 Hint
Think about method overriding and lookup order in execution_table steps 5 and 6.
Concept Snapshot
Parent and child classes:
- Define Parent class with methods.
- Child class inherits Parent using class Child(Parent).
- Child objects can use Parent methods if not overridden.
- Method lookup checks Child first, then Parent.
- Overriding allows Child to replace Parent methods.
Full Transcript
This example shows how a child class inherits from a parent class in Python. First, the Parent class is defined with a greet method. Then, the Child class is defined to inherit from Parent but does not add any methods. When we create an object c of Child and call c.greet(), Python looks for greet in Child. It is not found there, so it looks in Parent and finds it. The greet method from Parent runs and returns the string 'Hello from Parent', which is printed. This shows inheritance where the child class uses the parent's methods if it does not have its own version. If Child had its own greet method, that would be used instead, demonstrating method overriding.