inheritnce 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
class Robot:
def __init__(self, name):
self.name = name
def say_hi(self):
print("Hi, I am " + self.name)
class PhysicianRobot(Robot):
pass
x = Robot("Marvin")
y = PhysicianRobot("James")
print(x, type(x))
print(y, type(y))
y.say_hi()