python inheritance function 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
class Parent:
BloodGroup = 'A'
Gender = 'Male'
Hobby = 'Chess'
class Child(Parent):
BloodGroup = 'A+'
Gender = 'Female
def print_data():
print(BloodGroup, Gender, Hobby)
child1 = Child()
child1.print_data()