Day 13: Abstract Classes

  • Instructions:
    • Given a Book class and a Solution class, write a MyBook class that does the following:
      • Inherits from Book
      • Has a parameterized constructor taking 3 parameters:
        • 2 strings title & author
        • int price
      • Implements the Book class’ abstract display() method so it prints 3 lines:
        • Title: instance title
        • Author: instance author
        • Price: instance price
    Excercise
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    from abc import ABCMeta, abstractmethod
    class Book(object, metaclass=ABCMeta):
        def __init__(self,title,author):
            self.title=title
            self.author=author   
        @abstractmethod
        def display(): pass
    
    #Write MyBook class
    
    title=input()
    author=input()
    price=int(input())
    new_novel=MyBook(title,author,price)
    new_novel.display()
    
    Solution
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    from abc import ABCMeta, abstractmethod
    class Book(object, metaclass=ABCMeta):
        def __init__(self,title,author):
            self.title=title
            self.author=author   
        @abstractmethod
        def display(): pass
    
    #Write MyBook class
    class MyBook(Book):
        def __init__(self,title,author,price):
            super().__init__(title,author)
            self.price = price
            
        def display(self):
            print("Title: {}".format(self.title))
            print("Author: {}".format(self.author))
            print("Price: {}".format(self.price))
    
    title=input()
    author=input()
    price=int(input())
    new_novel=MyBook(title,author,price)
    new_novel.display()