How the concept of Inheritance is implemented in Python

In this Python tutorial, we will learn how to implement the concept of inheritance in Python programming language. First, what is inheritance? Inheritance is one of the object-oriented concepts which can be defined as deriving new classes from the existing classes. Some terminologies for understanding are as follows:-

Inheritance in Python

 

Super Class in Python – This class is also known as parent class or base class in which all the methods and attributes are written in this class.

Learn also,



Sub Class in Python – This class is also known as child class or derived class in which all the methods and attributes will get inherited from the superclass or base class or base class.

The main advantage of inheritance in Python

The one main advantage of inheritance is reuse of the existing code.That means the derived class has the similar properties of a parent class. This concept is also called as reusability. By using this concept the development time of software gets reduced.

 

                Implementation of Inheritance in Python

 

First, create a parent class or super class where all the methods will be written.

class Person:  
     
    name = ""  
    age = 0  
  
     
    def __init__(self, personName, personAge):  
        self.name = personName  
        self.age = personAge  
  
    
    def showName(self):  
        print(self.name)  
  
    def showAge(self):  
        print(self.age)

The above parent class consists of two attributes name and age.To invoke the class variables a default constructor is used. After invoking the variables two methods are defined in this class.Those are showName() and showAge().Super class is created.

 

Now, create a child class called student in which the methods will get inherited from parent class.

class Student(Person): 
    studentId = ""  
  
    def __init__(self, studentName, studentAge, studentId):  #Calling super class constructor
        Person.__init__(self, studentName, studentAge)  
        self.studentId = studentId  
  
    def getId(self):  
        return self.studentId

 

In the above child class which is derived from parent class person has only one extra attribute called studentId.Here we will call the super class constructor to send the attributes or values.

Source Code:-

class Person:  # Creating a super class
    # declare the class variables
    name = ""  
    age = 0  
  
    #create the constructor and invoke the class variables
    def __init__(self, personName, personAge):  
        self.name = personName  
        self.age = personAge  
  
    #create the class methods  
    def showName(self):  
        print(self.name)  
  
    def showAge(self):  
        print(self.age)  # end of super class
  
    
  

class Student(Person): # Create a subclass Student which is derived from Parent class Person
    studentId = ""  #Attribute of child class Student
  
    def __init__(self, studentName, studentAge, studentId):  
        Person.__init__(self, studentName, studentAge)  # Invoking the super class (Person) constructor to send values
        self.studentId = studentId  # Invoking the child class attribute
  
    def getId(self):  # Methods of child class
        return self.studentId  # end of sub class


class Teacher(Student): # Create a subclass Teacher which is derived from Parent class Student
    salary=0  # Attribute of child class Teacher
    
    def __init__(self,teacherName,teacherAge,teacherId,salary):
        Student.__init__(self, teacherName, teacherAge,teacherId)  # Invoking the super class (Student) constructor to send values
        self.salary=salary
    
    def getSalary(self):
        return self.salary
    
  
  

print('This is Super Class Person')
person1 = Person("Abhilash", 20)  
#call member methods of the objects  

print('Age of the Person :-')
person1.showAge()
print('Name of the Person :-')
person1.showName()
# Create an object of the subclass  
print('This is Child Class Student derived from Parent Class Person ')
student1 = Student("Avi", 20, "AB123")  #Line: 39

print('Student ID:-',student1.getId())
student1.getId()
print('Name of the Student:-')
student1.showName()
print('Age of the Student:-')
student1.showAge()
  
print('This is Child Class Teacher derived from Parent Class Student')
teacher1 = Teacher("Anil",50,"Cse008",600000)
print('Teacher ID:-',teacher1.getId())
print('Name of the Teacher')
teacher1.showName()
print('Age of the Teacher:-')
teacher1.showAge()
print('Salary of the Teacher:-',teacher1.getSalary())

Output:-

This is Super Class Person
Age of the Person :-
20
Name of the Person :-
Abhilash
This is Child Class Student derived from Parent Class Person 
Student ID:- AB123
Name of the Student:-
Avi
Age of the Student:-
20
This is Child Class Teacher derived from Parent Class Student
Teacher ID:- Cse008
Name of the Teacher
Anil
Age of the Teacher:-
50
Salary of the Teacher:- 600000

You can also read,

How to implement Stack in Python

Mutable and Immutable objects in Python

 

Leave a Reply

Your email address will not be published. Required fields are marked *