Learning objectives
By the end of this section you should be able to
- Implement a subclass that accesses inherited attributes from the superclass.
- Write a subclass's
__init__()
that inherits superclass instance attributes and creates new instance attributes.
Creating a simple subclass
Subclasses have access to the attributes inherited from the superclass. When the subclass's __init__()
isn't explicitly defined, the superclass's __init__()
method is called. Accessing both types of attributes uses the same syntax.
Concepts in Practice
Using simple subclasses
Using __init__() to create and inherit instance attributes
A programmer often wants a subclass to have new instance attributes as well as those inherited from the superclass. Explicitly defining a subclass's __init__()
involves defining instance attributes and assigning instance attributes inherited from the superclass.
Concepts in Practice
Accessing a subclass's attributes
Consider the Employee
and Developer
example code:
class Employee:
count = 0
def __init__(self):
Employee.count += 1
self.e_id = Employee.count
self.hire_year = 2023
def emp_display(self):
print(f"Employee {self.e_id} hired in {self.hire_year}")
class Developer(Employee):
def __init__(self):
Employee.count += 1
self.e_id = Employee.count
self.hire_year = 2023
self.lang_xp = ["Python", "C++", "Java"]
def dev_display(self):
print(f"Proficient in {self.lang_xp}")
emp_1 = Employee()
dev_1 = Developer()
Try It
Creating a subclass with an instance attribute
Given a class Dessert
, create a class, Cupcake
, inherited from Dessert
. Cupcake
class methods:
__init__(self)
: initializes inherited instance attribute ingredients with ["butter", "sugar", "eggs", "flour"], and initializes instance attribute frosting with "buttercream"display(self)
: prints a cupcake's ingredients and frosting
Then call the display()
method on a new Cupcake
object. The output should match:
Made with ["butter", "sugar", "eggs", "flour"] and topped with buttercream frosting