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 »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.