Inheritance in Python

Python supports the concept of re-using existing classes. Python allows its programmers to create new classes that re-use the pre-written and tested classes. The technique of creating a new class from an existing class is called Inheritance.

The old or existing class is called the base class and the new class is known as the derived class or sub class. It has the following syntax:

class Derived-Class(Base-Class):
body of derived class

Example:


class Person:
def __init__(self, name, age):
self.name=name
self.age=age
def display(self):
print("Name : ", self.name)
print("Age : ", self.age)
class Teacher(Person):
def __init__(self, name, age, dept):
Person.__init__(self, name, age)
self.dept=dept
def displayData(self):
Person.display(self)
print("Department : ", self.dept)
class Student(Person):
def __init__(self, name, age, course):
Person.__init__(self, name, age)
self.course=course
def displayData(self):
Person.display(self)
print("Course : ", self.course)
print("*******Teacher*******")
T=Teacher("Ratna", 36, "Computer Science")
T.displayData()
print("*******Student*******")
S=Student("Sumit", 22, "B.Tech")
S.displayData()

Output:
Inheritance in Python