class and method inheritance in python code example
Example 1: python inheritance
class Person:
name = ""
def __init__(self, personName):
self.name = personName
def showName(self):
print(self.name)
class Student(Person):
studentClass = ""
def __init__(self, studentName, studentClass):
Person.__init__(self, studentName)
self.studentClass = studentClass
def getStudentClass(self):
return self.studentClass
person1 = Person("Dave")
person1.showName()
student1 = Student("Mary", "Maths")
print(student1.getStudentClass())
student1.showName()
Example 2: inheritance in python 3 example
<__main__.Robot object at 0x7fd0080b3ba8> <class '__main__.Robot'>
<__main__.PhysicianRobot object at 0x7fd0080b3b70> <class '__main__.PhysicianRobot'>
Hi, I am James