Home30 Days Of CodeHackerRank Day 13 : Abstract Classes 30 days of code solution

HackerRank Day 13 : Abstract Classes 30 days of code solution

Today we are going to solve HackerRank Day 13 : Abstract Classes 30 days of code solution in C++ , Java , Python & Javascript.

Objective
Today, we will extend what we learned yesterday about Inheritance to Abstract Classes. Because this is a very specific object oriented concept, submissions are limited to the few languages that use this construct

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 3 parameters:
    1. string title
    2. string author
    3. int price
  • Implements the Book class’ abstract display() method so it prints these  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.

Note: Because these classes are being written in the same file, you must not use an access modifier (e.g.:public ) when declaring MyBook or your code will not execute.

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 titleauthor, and price of the MyBook object’s instance (with each value on its own line) like so:

Title: $title
Author: $author
Price: $price

Note: The $ is prepended to variable names to indicate they are placeholders for variables.

Sample Input

The following input from stdin is handled by the locked stub code in your editor:

The Alchemist
Paulo Coelho
248

Sample Output

The following output is printed by your display() method:

Title: The Alchemist
Author: Paulo Coelho
Price: 248

HackerRank Day 13 : Abstract Classes 30 days of code solution

Abstract Classes HackerRank Solution in C ++

class MyBook : public Book {
    private:
        int price;
    
    public:
        MyBook(string title, string author, int price) : Book(title, author), price(price) {
            
        }
    
        void display(){
            cout << "Title: " << title << endl;
            cout << "Author: " << author << endl;
            cout << "Price: " << price << endl;
        }
};

Abstract Classes HackerRank Solution in Java

class MyBook extends Book{
    int price;
    
    MyBook(String title, String author, int price){
        super(title, author);
        this.price = price;
    }
    
    public void display(){
        System.out.println(
                "Title: " + this.title + "\n"
            +   "Author: " + this.author + "\n"
            +   "Price: " + this.price
        );
    }
}

Abstract Classes HackerRank Solution in Python 3

#Write MyBook class

class MyBook(Book):

    def __init__(self,title,author,price):
        self.title = title
        self.author = author
        self.price = price

    def display(self):
        print("Title:", title)
        print("Author:", author)
        print("Price:",price)

Abstract Classes HackerRank Solution in JavaScript

// Declare your class here.
class MyBook extends Book{

    /**   
    *   Class Constructor
    *   
    *   @param title The book's title.
    *   @param author The book's author.
    *   @param price The book's price.
    **/
    // Write your constructor here
    constructor(title, author, price) {
        super(title, author);
        this.price = price;
    }
    /**   
    *   Method Name: display
    *   
    *   Print the title, author, and price in the specified format.
    **/
    // Write your method here
    display() {
        console.log('Title: ' + this.title);
        console.log('Author: ' + this.author);
        console.log('Price: ' + this.price);
    }
    
// End class
}

NEXT : Hackerrank Day 14 : scope 30 days of code solution

30 Days of Code HackerRank Solutions List – Day 0 to Day 29


Read More –

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

- Advertisment -

Categories