30 Day code challenge-python

Ganesh Uthiravasagam
2 min readMar 20, 2021

--

Day 13–Abstract Classes

Task

Given a Book class and a Solution class, write a MyBook class that does the following:

  • Inherits from Book
  • Has a parameterized constructor taking these parameters:
  1. string title
  2. string author
  3. int price
  • Implements the Book class’ abstract display() method so it prints these 3 lines:
  1. Title, a space, and then the current instance’s title.
  2. Author, a space, and then the current instance’s author.
  3. Price, a space, and then the current instance’s price.
Sample input and output

Input Format

You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object.

Output Format

The void display() method should print and label the respective title, author, price and of the MyBook object’s instance (with each value on its own line).

Solution

Explanation

Line 15–17: Gets input from the user

Line 18: Calls the class MyBook which inherits the class Book.

Line 9–14: Prints the output in the required format where MyBook class inherits the value of class Book using super function.

Bonus Tip: Abstract class is imported from ABC (Abstract Base Class) module and it’s denoted as @abstractmethod. For an abstractmethod object can’t be created.

See you on day 14

--

--

No responses yet