#!/usr/bin/env python
class Person:
def __init__(self,name):
self.name = name
def getDetails(self):
return self.name
class Student(Person):
def __init__(self,name,branch,year):
Person.__init__(self,name)
self.branch = branch
self.year = year
def getDetails(self):
return (self.name, self.branch, self.year)
class Teacher(Person):
def __init__(self,name,papers):
Person.__init__(self,name)
self.papers = papers
def getDetails(self):
return (self.name, 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]$ ./student_teacher.py
Rahul
('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.