The __init__ Method and Instance Variables
A child class extends a parent class using the syntax class ChildClass(ParentClass):, inheriting all parent variables and methods automatically.
From Parent to Child
Inheritance lets one class extend another class. The existing class is the parent class, and the new class is the child class. A child class inherits the parent class's variables and methods, then can add variables and methods of its own.
The syntax class ChildClass(ParentClass): defines ChildClass as a child of ParentClass. The parent-child relationship is one-way: the child inherits from the parent, but the parent does not inherit from the child.
What the Child Receives
A child class automatically inherits all variables and methods from its parent. This means an instance of the child class can use the parent's variables and methods without redefining them in the child class.
PartyAnimal and CricketFan
Determine which features belong to the parent class and which features are added by the child class.
- 1Identify the parent
PartyAnimal is the parent class in the example.
- 2Identify the child
CricketFan is the child class. Its inheritance syntax is class
CricketFan(PartyAnimal):. - 3List inherited features
CricketFan inherits the name variable and the
party()method from PartyAnimal. - 4List added features
CricketFan adds the points variable and the
six()method. - 5Inspect the instance
An instance named j has access to name,
party(), points, andsix().
The child instance combines inherited functionality from PartyAnimal with new functionality defined by CricketFan.
Initialization with super()
A child class often needs to initialize two sets of variables: the variables belonging to the parent and new variables belonging to the child. The child class can define its own __init__ method and use super().__init__() inside it to call the parent's __init__ method.
Call super().__init__() before setting up child-specific variables. This ensures that the parent class completes its required initialization first, after which the child can add its own setup.
What do you think happens?
When a child object's __init__ method uses super().__init__(), which initialization happens first?
Reveal answer
The parent class's initialization
super().__init__() calls the parent class's __init__ method. The child then sets up its own variables after the parent setup is complete.
A Combined Instance
Imagine creating an instance j of CricketFan. During initialization, the parent setup provides the inherited name variable. The child setup provides the points variable. The resulting instance has access to both variables, as well as the inherited party() method and the child-defined six() method.
Extending the Parent
A child class extends a parent class in two directions. First, it receives the parent's existing variables and methods through inheritance. Second, it contributes new variables and methods that make the child more specialized. In the source example, CricketFan keeps name and party() from PartyAnimal and adds points and six().
Common Initialization Mistakes
Common mistake Defining the child class without naming the parent in parentheses
What it looks likeUsing a class definition that does not follow the pattern class ChildClass(ParentClass):
Why it failsThe parent-child inheritance relationship is communicated by placing the parent class name in parentheses after the child class name.
Do this insteadUse the inheritance syntax class ChildClass(ParentClass):.
Common mistake Initializing only the child-specific variables
What it looks likeThe child __init__ method sets up points but does not call super().__init__().
Why it failsThe parent class's required setup is not completed.
Do this insteadCall super().__init__() from the child __init__ method before setting up child-specific variables.
Common mistake Assuming the parent receives features from the child
What it looks likeExpecting PartyAnimal to gain CricketFan's points variable or six() method.
Why it failsInheritance is one-way from parent to child.
Do this insteadRemember that CricketFan inherits from PartyAnimal; PartyAnimal does not inherit from CricketFan.
Common mistake Forgetting that inherited methods remain available
What it looks likeTreating a CricketFan instance as if it can use only six() and not party().
Why it failsA child instance has access to the parent's inherited methods as well as its own methods.
Do this insteadInclude both inherited and child-defined functionality when identifying what the instance can access.
Practice the Trace
A parent class provides the variable title and the method describe(). A child class extends it by adding the variable level and the method advance(). Trace the child object's initialization: identify which initialization must happen through super().__init__(), then list the inherited and child-defined features available on the finished object.
Hints
- The parent variables and methods are inherited by the child.
- The child should call
super().__init__() before setting up level. - Separate inherited features from features defined directly in the child.
- 1To solve the trace, begin with the parent initialization through
super().__init__(). Then add the child variable level. The finished child object has title anddescribe()from the parent, plus level andadvance()from the child.
Key Takeaways
- Define inheritance with the pattern class
ChildClass(ParentClass):. - A child class inherits the parent's variables and methods automatically.
- Use
super().__init__() inside the child__init__method to complete parent initialization. - Call
super().__init__() before setting child-specific variables. - A child instance combines inherited features with the new variables and methods defined by the child.