forked from int28h/JavaTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_Abstract_Classes.java
More file actions
56 lines (47 loc) · 1.41 KB
/
13_Abstract_Classes.java
File metadata and controls
56 lines (47 loc) · 1.41 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
/**
* 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:
* - string title
* - string author
* - int price
*
* Implements the Book class' abstract display() method so it prints these lines:
* Title: a space, and then the current instance's title.
* Author: a space, and then the current instance's author.
* Price: a space, and then the current instance's price.
*/
import java.util.*;
abstract class Book {
String title;
String author;
Book(String title, String author) {
this.title = title;
this.author = author;
}
abstract void display();
}
class MyBook extends Book {
int price;
MyBook(String title, String author, int price){
super(title, author);
this.price = price;
}
public void display(){
System.out.println("Title: " + this.title);
System.out.println("Author: " + this.author);
System.out.println("Price: " + this.price);
}
}
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String title = scanner.nextLine();
String author = scanner.nextLine();
int price = scanner.nextInt();
scanner.close();
Book book = new MyBook(title, author, price);
book.display();
}
}