Inheritance
Subscribe to Tech with Tim
Inheritance
When we create classes we can inherit methods and attributes from other already existing classes. This allows us to create a variety of different sub-classes or child classes based off of what is known as a parent class or super class.
The following words are all equivalent and mean a class that inherits from another:
- child class
- subclass
- derived class
- concrete class
The following words are all equivalent and mean a class that is inherited from:
- parent class
- super class
- abstract class
When we inherit from a class all the methods and attributes of the parent class are passed down to the child class.
class Animal(): def __init__(self, name, age): self.name = name self.age = age def speak(self): print("I am", self.name, "and I am", self.age, "years old") class Dog(Animal): def __init__(self, name, age) self.name = name self.age = age self.type = "dog" # Since we inherit from the animal class we can use the method speak on Dog objects tim = Dog("Tim", 5) tim.speak() # This will print "I am Tim and I am 5 years old"
Calling a Super-Classes Constructor
Often times when we create sub-classes we want to initialize them in a similar same way as the parent class. To save ourselves from repeating code we can call the parents init function from inside of our childs class init like so:
class Animal(): def __init__(self, name, age): self.name = name self.age = age def speak(self): print("I am", self.name, "and I am", self.age, "years old") class Dog(Animal): def __init__(self, name, age) super().__init__(name, age) # This will call the Animal classes constructor method tim = Dog("Tim", 5) tim.speak() # This will print "I am Tim and I am 5 years old"
Overriding Methods
Sometimes when we inherit from a parent class we want to have methods or attributes that have the same name as a method in the parent class but that have a different functionality. If we create a method or attribute inside of our child class with the same name as one in the parent it will override the parent class.
class Animal(): def __init__(self, name, age): self.name = name self.age = age def speak(self): print("I am", self.name, "and I am", self.age, "years old") class Dog(Animal): def __init__(self, name, age) super.__init__(name, age) # This will call the Animal classes constructor method def speak(self): print("I am a Dog") tim = Dog("Tim", 5) tim.speak() # This will print "I am a Dog"
Example
Below is a realistic example of inheritance and where you may use it.
class Veichle: def __init__(self, price, color): self.color = color self.price = price self.gas = 0 def fillUpTank(self): self.gas = 100 def emptyTank(self): self.gas = 0 def gasLeft(self): return self.gas class Truck(Veichle): def __init__(self, price, color, tires): super().__init__(price, color) self.tires = tires def beep(self): print("Honk honk") class Car(Veichle): def __init__(self, price, color, speed): super().__init__(price, color) self.speed = speed def beep(self): print("Beep Beep")