Product SiteDocumentation Site


#!/usr/bin/env python

class Person:
    def __init__(self,name): = name

    def getDetails(self):

class Student(Person):
    def __init__(self,name,branch,year):
        self.branch = branch
        self.year = year

    def getDetails(self):
        return (, self.branch, self.year)

class Teacher(Person):
    def __init__(self,name,papers):
        self.papers = papers

    def getDetails(self):
        return (, self.papers)

person1 = Person('Rahul')
student1 = Student('Kushal','CSE',2005)
teacher1 = Teacher('Prashad',['C','C++'])

print person1.getDetails()
print student1.getDetails()
print teacher1.getDetails()

The output:

[kdas@kdas code]$ ./
('Kushal', 'CSE', 2005)
('Prashad', ['C', 'C++'])

In this example you can see how we called the __init__ method of the class Person in both Studentা and Teacher classes' __init__ method. We also reimplemented getDetails() method of Person class in both Student and Teacher class. So, when we are calling getDetails() method on the teacher1 object it returns based on the object itself (which is of teacher class) and when we call getDetails() on the student1 or person1 object it returns based on getDetails() method implemented in it's own class.