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.
Identify the parent: PartyAnimal is the parent class in the example.
Identify the child: CricketFan is the child class. Its inheritance syntax is class CricketFan(PartyAnimal):.
List inherited features: CricketFan inherits the name variable and the party() method from PartyAnimal.
List added features: CricketFan adds the points variable and the six() method.
Inspect the instance: An instance named j has access to name, party(), points, and six().
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
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
Defining the child class without naming the parent in parentheses
The parent-child inheritance relationship is communicated by placing the parent class name in parentheses after the child class name.
Fix:
Use the inheritance syntax class ChildClass(ParentClass):.Initializing only the child-specific variables
The parent class's required setup is not completed.
Fix:
Call super().__init__() from the child __init__ method before setting up child-specific variables.Assuming the parent receives features from the child
Inheritance is one-way from parent to child.
Fix:
Remember that CricketFan inherits from PartyAnimal; PartyAnimal does not inherit from CricketFan.Forgetting that inherited methods remain available
A child instance has access to the parent's inherited methods as well as its own methods.
Fix:
Include 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.
- To solve the trace, begin with the parent initialization through super().__init__(). Then add the child variable level. The finished child object has title and describe() from the parent, plus level and advance() 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.