Today we will see the HackerRank Day 13 Solution in Python. The problem is named Abstract Classes which is part of 30 Days of code on HackerRank. Let’s get started!
Day 13: Abstract Classes Problem statement
We are given a Book class and a Solution class, our task is to write a MyBook class that inherits from the Book class. It should have a parameterized constructor and implements the abstract display() method of the Book class
Sample Input
The Alchemist
Paulo Coelho
248
Sample Output
Title: The Alchemist
Author: Paulo Coelho
Price: 248
You can solve the problem here.
HackerRank Day 13 Solution in Python
from abc import ABCMeta, abstractmethod #Base class class Book(object, metaclass=ABCMeta): #Constructor of Book class def __init__(self,title,author): self.title=title self.author=author #Display method defined as Abstract @abstractmethod def display(): pass #MyBook class inherited from Book class class MyBook(Book): def __init__(self, title, author, price): super().__init__(title, author) self.price = price #Display method to print the book details def display(obj): print("Title:", obj.title) print("Author:", obj.author) print("Price:", obj.price) #Getting book details as user inputs title=input() author=input() price=int(input()) #Object of MyBook class new_novel=MyBook(title,author,price) #Calling display method using object of MyBook class new_novel.display()
Code Explanation:
- We created a MyBook class which is inherited from the Book class.
- Then we defined the constructor with parameters title, author, price
- After that, the method display is created which prints the details of the book.
Also Read:
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- Find Peak Element LeetCode 162
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- Explained: Allocate minimum number of pages
- HackerRank Day 15 Solution in Python: Linked List
- Search a 2D matrix: leetcode 74
- Maximum Subarray Sum: Kadane’s Algorithm
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope