Book new_novel=new Book(); <\/code><\/pre>\n\n\n\nYou have to create another class that extends the abstract class. Then you can create an instance of the new class.<\/p>\n\n\n\n
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.<\/p>\n\n\n\n
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.<\/p>\n\n\n\n
Your class mustn’t be public.<\/p>\n\n\n\n
<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n A tale of two cities<\/code><\/pre>\n\n\n\n<\/span>Sample Output<\/strong><\/span><\/h4>\n\n\n\n The title is: A tale of two cities<\/code><\/pre>\n\n\n\n<\/span>Solution – Java Abstract Class<\/strong><\/span><\/h2>\n\n\n\nimport java.util.*;\nabstract class Book{\n\tString title;\n\tabstract void setTitle(String s);\n\tString getTitle(){\n\t\treturn title;\n\t}\n}\n\n\/\/Write MyBook class here\nclass MyBook extends Book {\n @Override\n void setTitle(String s) {\n title = s;\n }\n}\n\npublic class Main{\n\t\n\tpublic static void main(String []args){\n\t\t\/\/Book new_novel=new Book(); This line prHMain.java:25: error: Book is abstract; cannot be instantiated\n\t\tScanner sc=new Scanner(System.in);\n\t\tString title=sc.nextLine();\n\t\tMyBook new_novel=new MyBook();\n\t\tnew_novel.setTitle(title);\n\t\tSystem.out.println(\"The title is: \"+new_novel.getTitle());\n \tsc.close();\n\t\t\n\t}\n}<\/pre>\n\n\n\n