HackerRank Java- Abstract Class

You have to create another class that extends the abstract class. Then you can create an instance of the new class.

Notice that setTitle method is abstract too and has no body. That means you must implement the body of that method in the child class.

In the editor, we have provided the abstract Book class and a Main class. In the Main class, we created an instance of a class called MyBook. Your task is to write just the MyBook class.

Your class mustn't be public.

  1. import java.util.*;
  2. abstract class Book{
  3. String title;
  4. abstract void setTitle(String s);
  5. String getTitle(){
  6. return title;
  7. }
  8. }
  9. class MyBook extends Book {
  10. @Override
  11. void setTitle(String s) {
  12. title = s;
  13. }
  14. }
  15. public class Main{
  16. public static void main(String []args){
  17. //Book new_novel=new Book(); This line prHMain.java:25: error: Book is abstract; cannot be instantiated
  18. Scanner sc=new Scanner(System.in);
  19. String title=sc.nextLine();
  20. MyBook new_novel=new MyBook();
  21. new_novel.setTitle(title);
  22. System.out.println("The title is: "+new_novel.getTitle());
  23. sc.close();
  24. }
  25. }


Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube