Python Inheritance and Polymorphism

Software development is a team effort. Several programmers will work as a team to develop software. When a programmer develops a class, he will use its features by creating an instance to it. When another programmer wants to create another class which is similar to the class already created, then he need not create the class from the scratch. He can simply use the features of the existing class in creating his own class.

A programmer in the software development is creating Teacher class with setter() and getter() methods. Then he saved this code in a file ''.

When the programmer wants to use this Teacher class that is available in file, he can simply import this class into his program and use it as shown below:

creating module

The module is created and available in the project database

Now, another programmer in the same team wants to create a Student class. He is planning the Student class without considering the Teacher class as shown below

A Python program to create Student class and store it into module.

Now, the second programmer who created this Student class and saved it as can use it whenever he needs.

A Python program to use the Student class which is already available in

If we compare the Teacher class and the Student classes, we can understand that 75% of the code is same in both the classes. That means most of the code being planned by the second programmer in his Student class is already available in the Teacher class. Then why doesn't he use it for his advantage? Our idea is this: instead of creating a new class altogether, he can reuse the code which is already available.

A Python program to create Student class by deriving it from the Teacher class.

class Student(Teacher):

This means the Student class is derived from Teacher class. Once we write like this, all the members of Teacher class are available to the Student class. Hence we can use them without rewriting them in the Student class.

student class object