In this HackerRank Day 13: Abstract Classes problem solution You are Given a Book class and a Solution class, write a MyBook class that does the following:

  1. Inherits from Book
  2. Has a parameterized constructor taking these 3 parameters:
  3. string title
  4. string author
  5. int price
  6. Implements the Book class' abstract display() method so it prints these 3 lines:
  7. Title:, a space, and then the current instance's title.
  8. Author:, a space, and then the current instance's author.
  9. Price:, a space, and then the current instance's price.

HackerRank Day 13: Abstract Classes problem solution


Problem solution in Python programming.

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



Problem solution in Java programming.

//Write MyBook Class

class MyBook extends Book{
    private int price;
    public MyBook(String title, String author, int price){
        super(title, author);
        this.price = price;
    }
    
    void display(){
        System.out.println("Title: "+title);
        System.out.println("Author: "+author);
        System.out.println("Price: "+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) {
        this->price = price; 
    }
    
    void display() {
        cout << "Title: " << title << "\n";
        cout << "Author: " << author << "\n";
        cout << "Price: " << price << "\n";
    }
};



Problem solution in Javascript programming.

class MyBook extends Book {
  constructor(t, a, p) {
    super(t, a);
    this.t = t;
    this.a = a;
    this.p = p;
  }

  display() {
    console.log(`Title: ${this.t}`);
    console.log(`Author: ${this.a}`);
    console.log(`Price: ${this.p}`);
  }
}