Like any other OOP languages, Swift also supports the concept of class inheritance. Inheritance allows us to create a new class from an existing class.
The new class that is created is known as subclass (child or derived class) and the existing class from which the child class is derived is known as superclass (parent or base class).
Python Inheritance Syntax
Here’s the syntax of the inheritance in Python,
# define a superclass
class super_class:
# attributes and method definition
# inheritance
class sub_class(super_class):
# attributes and method of super_class
# attributes and method of sub_class
Here, we are inheriting the sub_class
class from the super_class
class.
Example: Python Inheritance
class Animal:
# attribute and method of the parent class
name = ""
def eat(self):
print("I can eat")
# inherit from Animal
class Dog(Animal):
# new method in subclass
def display(self):
# access name attribute of superclass using self
print("My name is ", self.name)
# create an object of the subclass
labrador = Dog()
# access superclass attribute and method
labrador.name = "Rohu"
labrador.eat()
# call subclass method
labrador.display()
Output
I can eat My name is Rohu
In the above example, we have derived a subclass Dog from a superclass Animal. Notice the statements,
labrador.name = "Rohu"
labrador.eat()
Here, we are using labrador (object of Dog) to access name and eat()
of the Animal class. This is possible because the subclass inherits all attributes and methods of the superclass.
Also, we have accessed the name attribute inside the method of the Dog class using self
.
is-a relationship
In Python, inheritance is an is-a relationship. That is, we use inheritance only if there exists an is-a relationship between two classes. For example,
- Car is a Vehicle
- Apple is a Fruit
- Cat is an Animal
Here, Car can inherit from Vehicle, Apple can inherit from Fruit, and so on.
Method Overriding in Python Inheritance
In the previous example, we see the object of the subclass can access the method of the superclass.
However, what if the same method is present in both the superclass and subclass?
In this case, the method in the subclass overrides the method in the superclass. This concept is known as method overriding in Python.
Example: Method Overriding
class Animal:
# attributes and method of the parent class
name = ""
def eat(self):
print("I can eat")
# inherit from Animal
class Dog(Animal):
# override eat() method
def eat(self):
print("I like to eat bones")
# create an object of the subclass
labrador = Dog()
# call the eat() method on the labrador object
labrador.eat()
Output
I like to eat bones
In the above example, the same method eat()
is present in both the Dog class and the Animal class.
Now, when we call the eat()
method using the object of the Dog subclass, the method of the Dog class is called.
This is because the eat()
method of the Dog subclass overrides the same method of the Animal superclass.
The super() Method in Python Inheritance
Previously we saw that the same method in the subclass overrides the method in the superclass.
However, if we need to access the superclass method from the subclass, we use the super()
method. For example,
class Animal:
name = ""
def eat(self):
print("I can eat")
# inherit from Animal
class Dog(Animal):
# override eat() method
def eat(self):
# call the eat() method of the superclass using super()
super().eat()
print("I like to eat bones")
# create an object of the subclass
labrador = Dog()
labrador.eat()
Output
I can eat I like to eat bones
In the above example, the eat()
method of the Dog subclass overrides the same method of the Animal superclass.
Inside the Dog class, we have used
# call method of superclass
super().eat()
to call the eat()
method of the Animal superclass from the Dog subclass.
So, when we call the eat()
method using the labrador object
# call the eat() method
labrador.eat()
Both the overridden and the superclass version of the eat()
method is executed.
Uses of Inheritance
- Since a child class can inherit all the functionalities of the parent’s class, this allows code reusability.
- Once a functionality is developed, you can simply inherit it. No need to reinvent the wheel. This allows for cleaner code and easier to maintain.
- Since you can also add your own functionalities in the child class, you can inherit only the useful functionalities and define other required features.
