-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractClass.java
More file actions
66 lines (46 loc) · 1.06 KB
/
Copy pathAbstractClass.java
File metadata and controls
66 lines (46 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Question:
Create a class MyBook that extends
the abstract class Book.
Implement the abstract method:
setTitle(String s)
Store the given title and print it.
Example:
Input:
A tale of two cities
Output:
The title is: A tale of two cities
*/
import java.util.*;
// Abstract parent class
abstract class Book {
String title;
// Abstract method
abstract void setTitle(String s);
// Return book title
String getTitle() {
return title;
}
}
// Child class extending Book
class MyBook extends Book {
// Implement abstract method
void setTitle(String s) {
// Store title
title = s;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read book title
String title = sc.nextLine();
// Create MyBook object
MyBook new_novel = new MyBook();
// Set title
new_novel.setTitle(title);
// Print title
System.out.println("The title is: " + new_novel.getTitle());
sc.close();
}
}