Q:

Hierarchical Inheritance Example in Python

belongs to collection: Python class & object programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Constructor Initialization Example in Python... >>
<< Searching of objects from an array of objects usin...