Java Day 13: Abstract Classes



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:
Implements the Book class' abstract display() method so it prints these lines:



  1. import java.util.*;
  2. abstract class Book {
  3. String title;
  4. String author;
  5. Book(String title, String author) {
  6. this.title = title;
  7. this.author = author;
  8. }
  9. abstract void display();
  10. }
  11. class MyBook extends Book {
  12. private int price;
  13. MyBook(String t, String a, int p) {
  14. super(t, a);
  15. price = p;
  16. }
  17. @Override
  18. void display() {
  19. System.out.println("Title: " + title + "\nAuthor: " + author + "\nPrice: " + price);
  20. }
  21. }
  22. // Declare your class here. Do not use the 'public' access modifier.
  23. // Declare the price instance variable
  24. /**
  25. * Class Constructor
  26. *
  27. * @param title The book's title.
  28. * @param author The book's author.
  29. * @param price The book's price.
  30. **/
  31. // Write your constructor here
  32. /**
  33. * Method Name: display
  34. *
  35. * Print the title, author, and price in the specified format.
  36. **/
  37. // Write your method here
  38. // End class
  39. public class Solution {
  40. public static void main(String[] args) {
  41. Scanner scanner = new Scanner(System.in);
  42. String title = scanner.nextLine();
  43. String author = scanner.nextLine();
  44. int price = scanner.nextInt();
  45. scanner.close();
  46. Book book = new MyBook(title, author, price);
  47. book.display();
  48. }
  49. }