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): 				# Student inherits from Person superclass
    studentClass = ""  

    def __init__(self, studentName, studentClass):  
        Person.__init__(self, studentName)		# superclass constructor
        self.studentClass = studentClass  		# Student class specific
  
    def getStudentClass(self):  
        return self.studentClass  
  
  
person1 = Person("Dave")
person1.showName()                  # Dave
student1 = Student("Mary", "Maths")
print(student1.getStudentClass())   # Maths
student1.showName()                 # Mary

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()