In this HackerRank Day 13 Abstract Classes 30 days of code problem set, we have two classes Book and Solution. we need to make a new class MyBook that inherited from Book and can print the details.


Day 13 Abstract Classes 30 days of code solution hackerrank


Problem solution in Python 2 programming.

#Write MyBook class
class MyBook(Book):
    def __init__(self,title,author,price):
        Book.__init__(self,title,author)
        self.price=price
    def display(self):
        print "Title: " + self.title
        print "Author: " + self.author
        print "Price: " + str(self.price)


Problem solution in Python 3 programming.

#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)


Problem solution in java programming.

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
        );
    }
}


Problem solution in c++ programming.

//Write MyBook class
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;
        }
};


Problem solution in Javascript programming.

// 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
}