Section 1/4The is-a Relationship
The is-a Relationship
Use inheritance when the child class is a type of the parent class. A Dog is an Animal. A SavingsAccount is a BankAccount.
Inheritance
Inheritance lets a new class reuse the attributes and methods of an existing class. The existing class is called the parent (or superclass); the new class is called the child (or subclass).
The is-a Relationship
Use inheritance when the child class is a type of the parent class. A Dog is an Animal. A SavingsAccount is a BankAccount.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."
class Dog(Animal): # Dog inherits from Animal
def speak(self):
return "Woof"The syntax class Dog(Animal): means Dog inherits everything from Animal.