Concepts / Methods in Classes

Methods in Classes

A child class extends a parent class using the syntax class ChildClass(ParentClass):, inheriting all parent variables and methods automatically.

  • Programming

One Object, Two Class Levels

Inheritance lets you create a child class that extends a parent class. The child receives the parent class's variables and methods, then can add variables and methods of its own. This means one child-class instance can combine inherited functionality with child-specific functionality.

inheritsPartyAnimalname, party()CricketFanpoints, six()
What does a child class receive when it extends a parent class?

The relationship is one-way: the child inherits from the parent, but the parent does not inherit from the child.

The Inheritance Declaration

To define a child class, place the parent class name in parentheses after the child class name. The pattern is class ChildClass(ParentClass):. In the source example, CricketFan is the child class and PartyAnimal is the parent class, so CricketFan extends PartyAnimal.

python
classstarts a class definitionChildClassnew class(opens parent listParentClassparent class):completes declaration
How does class ChildClass(ParentClass): express the parent-child relationship?

Inheritance is a one-way class relationship in which the child class receives functionality from the parent class.

Initialization Through super()

A child class may need to initialize both the parent class's variables and its own variables. The child class's __init__ method can call super().__init__() to run the parent class's __init__ method. Call super().__init__() before setting child-specific variables so the parent is initialized first.

python
callssuper().__init__()returns, then continueCricketFan instanceobject creationCricketFan.__init__child setup beginsPartyAnimal.__init__super().__init__()pointschild attribute setup
How does control move from the child initializer to the parent initializer during object creation?

Inherited and Added Functionality

A CricketFan Extends PartyAnimal

Track which variables and methods are available on an instance of CricketFan.

Parent functionality: PartyAnimal supplies the name variable and the party() method.

Child functionality: CricketFan adds the points variable and the six() method.

Combined instance: An instance of CricketFan has access to the inherited name and party(), as well as the child-specific points and six().

The child-class instance combines the parent class's variables and methods with the variables and methods defined by the child class.

python
combined on instancecombined on instancenameinherited variablepointsnew variableparty()inherited methodsix()new method
Which methods and attributes come from the parent, and which are added by the child?

Inheritance does not replace the child class's ability to define its own behavior. The child extends the parent by adding new variables and methods while retaining access to the inherited ones.

Finding an Inherited Method

A child-class object can use a method that the child class did not define itself because the method was inherited from the parent. For example, a CricketFan instance can use party(), which comes from PartyAnimal, while also using six(), which is defined in CricketFan.

looks in child classnot defined in childCricketFan instanceparty() requestedCricketFanmethod defined here?PartyAnimal.party()inherited method
When a child-class object calls a method it does not define itself, where does the method lookup go next?
has access tohas access tohas access tohas access tojCricketFan instancenamefrom PartyAnimalpointsfrom CricketFanparty()from PartyAnimalsix()from CricketFan
What attributes exist on a child-class object after parent and child initialization?

Initialization Checklist

  1. Declare the child class with the parent class in parentheses.
  2. Define the child class's __init__ method when the child needs its own initialization.
  3. Call super().__init__() from the child initializer.
  4. Set up the child-specific variables after the parent initializer returns.
  5. Add child-specific methods while retaining access to inherited methods.
  • Defining the child class without naming its parent in parentheses.

    This does not express that CricketFan extends PartyAnimal.

    Fix: Use the inheritance form class CricketFan(PartyAnimal):.

  • Setting child-specific variables without calling the parent initializer.

    The parent class's required initialization may not be completed.

    Fix: Call super().__init__() before setting child-specific variables.

  • Assuming the child has only its newly defined methods and variables.

    A child instance also has access to inherited parent functionality.

    Fix: Include both inherited members, such as name and party(), and child members, such as points and six().

  • Assuming inheritance works in both directions.

    The hierarchy is one-way from parent to child.

    Fix: Remember that CricketFan inherits from PartyAnimal; PartyAnimal does not inherit from CricketFan.

Practice the Extension Pattern

MEDIUM

Suppose PartyAnimal provides name and party(). Design a child class named CricketFan that calls the parent initializer, adds points, and defines six(). Then list which variables and methods a CricketFan instance can access.

Hints
  • Put PartyAnimal in parentheses after CricketFan.
  • Call super().__init__() inside the child initializer before setting points.
  • Separate inherited members from members introduced by CricketFan.

Checking the Completed Design

Explain the members available on an instance named j created from CricketFan.

Inherited variable: j has name from PartyAnimal.

Inherited method: j can use party() from PartyAnimal.

Child variable: j has points from CricketFan.

Child method: j can use six() from CricketFan.

j combines the parent functionality and the child functionality.

What to Remember

  1. Use class ChildClass(ParentClass): to define a child class that extends a parent class.
  2. A child class inherits the parent class's variables and methods.
  3. Use super().__init__() inside the child initializer to run the parent initializer.
  4. Call the parent initializer before setting child-specific variables.
  5. A child-class instance combines inherited functionality with new functionality defined by the child.

Key Takeaways

  • Inheritance creates a one-way parent-child relationship between classes.
  • The syntax class ChildClass(ParentClass): gives the child access to the parent's variables and methods.
  • super().__init__() runs the parent initializer from the child initializer.
  • A child class can add its own variables and methods while retaining inherited functionality.