Skip to content

Commit 0c11f82

Browse files
authored
Add files via upload
1 parent a9b9f5f commit 0c11f82

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
3+
4+
Following is an example of abstract class:
5+
6+
abstract class Book{
7+
String title;
8+
abstract void setTitle(String s);
9+
String getTitle(){
10+
return title;
11+
}
12+
}
13+
If you try to create an instance of this class like the following line you will get an error:
14+
15+
Book new_novel=new Book();
16+
You have to create another class that extends the abstract class. Then you can create an instance of the new class.
17+
18+
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.
19+
20+
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.
21+
22+
Your class mustn't be public.
23+
24+
Sample Input
25+
26+
A tale of two cities
27+
Sample Output
28+
29+
The title is: A tale of two cities
30+
31+
*/
32+
33+
34+
35+
import java.util.*;
36+
abstract class Book{
37+
String title;
38+
abstract void setTitle(String s);
39+
String getTitle(){
40+
return title;
41+
}
42+
}
43+
44+
class MyBook extends Book
45+
{
46+
47+
void setTitle(String s){
48+
title = s;
49+
}
50+
}
51+
52+
53+
54+
55+
public class Main{
56+
57+
public static void main(String []args){
58+
//Book new_novel=new Book(); This line prHMain.java:25: error: Book is abstract; cannot be instantiated
59+
Scanner sc=new Scanner(System.in);
60+
String title=sc.nextLine();
61+
MyBook new_novel=new MyBook();
62+
new_novel.setTitle(title);
63+
System.out.println("The title is: "+new_novel.getTitle());
64+
sc.close();
65+
66+
}
67+
}

0 commit comments

Comments
 (0)