-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
86 lines (73 loc) · 1.76 KB
/
Copy pathBook.java
File metadata and controls
86 lines (73 loc) · 1.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.Scanner;
class Book{
String author;
Book(){
this.author="xyz";
}
Book(String author){
this.author=author;
}
void display(){
System.out.println("Author name: "+this.author);
}
}
class BookPublication extends Book{
String title;
BookPublication(){
super();
this.title="abc";
}
BookPublication(String author, String title){
super(author);
this.title=title;
}
void display(){
super.display();
System.out.println("Book Title: "+this.title);
}
}
class PaperPublication extends Book{
String title;
PaperPublication(){
super();
this.title="pqr";
}
PaperPublication(String author,String title){
super(author);
this.title=title;
}
void display(){
super.display();
System.out.println("Paper Title: "+this.title);
}
}
class Main{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.println("How many books : ");
int n=sc.nextInt();
Book b[]=new Book[n];
for(int i=0;i<n; i++){
String author=sc.nextLine(); // to get data from buffer
System.out.println("Enter author name : ");
author=sc.nextLine();
System.out.println("Enter type of Publication (book / paper) : ");
String type=sc.nextLine();
System.out.println("Enter Title : ");
String title=sc.nextLine();
if (type.equals("book"))
b[i] = new BookPublication(author, title);
else
if (type.equals("paper"))
b[i]=new PaperPublication(author,title);
else
{
System.out.println("Invalid publication type ");
i--;
}
}//end of for
System.out.println("Pulication Details: ");
for(int i=0; i<n; i++)
b[i].display();//Dynamic method dispatch -> display of BookPublication or PaperPublication
}
}