Hierarchical Inheritance Example in Python

Here, we are going to learn about the Hierarchical Inheritance and going to explain it by writing a Python program to demonstrate the Hierarchical Inheritance works.
Submitted by Shivang Yadav, on February 15, 2021

Problem Description: We will create a class named Media which is inherited by two classes Magazine and Channel. Then we have used the get method to get input of information from the user. And then print the details using the print method.

hierarchical inheritance

Program to illustrate Hierarchical Inheritance in Python

class Media:
    def getMediaInfo(self):
        self.__title=input("Enter Title:")
        self.__price=input("Enter Price:")
    def printMediaInfo(self):
        print(self.__title,self.__price)
class Magazine(Media):
    def getMagazineInfo(self):
        self.getMediaInfo()
        self.__pages=input("Enter Pages:")
    def printMagazineInfo(self):
        print("Magazine information : ")
        self.printMediaInfo()
        print("Pages:",self.__pages)
class Channel(Media):
    def GetChannelInfo(self):
        self.getMediaInfo()
        self.__freq=input("Enter Frequency:")
    def printChannelInfo(self):
        print("Channel information : ")
        self.printMediaInfo()
        print("Frequency:",self.__freq)

print("Enter Magazine information.")
magzineInfo = Magazine()
magzineInfo.getMagazineInfo()
magzineInfo.printMagazineInfo()

print("Enter Channel information.")
channelInfo = Channel()
channelInfo.GetChannelInfo()
channelInfo.printChannelInfo()

Output:

Enter Magazine information.
Enter Title:The times
Enter Price:120
Enter Pages:500
Magazine information :
The times 120
Pages: 500
Enter Channel information.
Enter Title:Star
Enter Price:100
Enter Frequency:250
Channel information :
Star 100
Frequency: 250

Python class & object programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.