Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Inheritance in Python

Like a child inherits characteristics from his parents, a class in Python can also inherit properties from other classes. This is called inheritance. A class can inherit data members as well as functions from other classes. This promotes the reusability of code.

Points to remember while inheriting a class

The class which is inherited by some other class is called the SuperClass or the BaseClass and the class which inherits the SuperClass is called the SubClass or the DerivedClass.
Points to remember while inheriting a class -
  • Private methods and variables of the BaseClass cannot be inherited by the DerivedClass. If we try to do so, it gives an error.
  • To use the properties of the SuperClass, we must invoke its __init__() method. If a class inherits from a single BaseClass then we can call the super().__init__() method to invoke the SuperClass's init method. Otherwise, if a class inherits from multiple BaseClasses, then we must invoke the __init__() method of each BaseClass by using BaseClass1.__init__() and BaseClass2.__init__() etc.
  • You can override any method of the BaseClass and can provide your own implementation in the DerivedClass.

How to inherit a class ?

The syntax for inheriting a class is as follows -
class SuperClass:
    # Statements

class SubClass(SuperClass):
    # Statements
For example -

class Person:

    def __init__(self,name,age,phone_number) -> None:

        self.name=name
        self.age=age

        # Private variable
        self.__phone_number=phone_number

    # Private method
    def __changePhoneNumber(self,newPhoneNumber):

        self.phone_number=newPhoneNumber

class Student(Person):

    def __init__(self,name,age,rollno,grade) -> None:
     
        self.rollno=rollno
        self.grade=grade

        # Invoking the constructor method of the base class.
        super().__init__(name,age,phone_number)

s1=Student("ravi",15,1012,'B')
print(s1.rollno)
print(s1.phone_number)     # Generates an AttributeError
    
Output
1012
Traceback (most recent call last):
  File "c:\Users\HP\OneDrive\Desktop\aditi jain\Rapid Coders Articles\python.py", line 16, in 
    print(s1.phone_number)
AttributeError: 'Student' object has no attribute 'phone_number'
        

Overriding method of Base Class

Overriding means redefining a method of the BaseClass by a DerivedClass. On overriding a method, we can provide a different implementation of the method than the BaseClass. When a method is called by an object, then the Python Interpreter first looks for it in the self-class. If it is not found, then it goes to the SuperClass. Hence, if we override a method of the BaseClass in the DerivedClass, then the method provided by the DerivedClass will get called.

    class Person:

    def __init__(self,name,age,phone_number) -> None:

        self.name=name
        self.age=age

        # Private variable
        self.__phone_number=phone_number

    # Public Method

    def introduce_yourself(self):

        return f"My name is {self.name}, age is {self.age} and phone number is {self.phone_number}."

class Student(Person):

    def __init__(self,name,age,phone_number,rollno,grade) -> None:

        self.rollno=rollno
        self.grade=grade

        # Invoking the constructor method of the base class.
        super().__init__(name,age,phone_number)
    
    # Overriding this method

    def introduce_yourself(self):
    
        return f"My name is {self.name}, age is {self.age}. I am a student with rollno {self.rollno} and my grade is {self.grade}."

s1=Student("ravi",15,98765,1012,'B')
print(s1.introduce_yourself())
    
Output
My name is ravi, age is 15. I am a student with rollno 1012 and my grade is B.

Types of inheritance

There are 5 types of inheritance in Python -
1. Single-level Inheritance - In this type of inheritance, a single class inherits from a single BaseClass. For Example -

class Person:
        # Statements

class Employee(Person):         # Employee inherits from Person
        # Statements
2. Multiple Inheritance - In this type of inheritance, a single class inherits from two or more BaseClasses. For Example -

class Person:
    # Statements

class Employee:
    # Statements

class SalesPerson(Person,Employee):         # SalesPerson inherits from both Person and Employee
    # Statements
    
3. Multilevel Inheritance - In this type of inheritance, class C inherits from class B which also inherits the class A. For Example -

class Person:
    # Statements
    
class Employee(Person):         # Employee inherits from Person
    # Statements
    
class SalesPerson(Employee):         # SalesPerson inherits from Employee
    # Statements
4. Hierarchical Inheritance - In this type of inheritance, two or more classes inherit from the same BaseClass. For Example -

class Person:
    # Statements

class Student(Person):         # Student inherits from Person
    # Statements
    
class Employee(Person):         # Employee also inherits from Person
    # Statements
5. Hybrid Inheritance - This inheritance is a combination of two or more types of inheritance. For example -

class Person:
    # Statements

class Student(Person):         # Student inherits from Person
    # Statements
    
class Employee(Person):         # Employee also inherits from Person
    # Statements

class SalesPerson(Employee):         # SalesPerson inherits from Employee
    # Statements