-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_60_PracticeSet_ch11.java
154 lines (130 loc) · 3.73 KB
/
cwh_60_PracticeSet_ch11.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package company;
// Q1:
abstract class Pen {
abstract void write();
abstract void refill();
}
// Q2:
class FountainPen extends Pen {
void changeNib() {
System.out.println("Changing nib of the fountain pen.");
}
public void write() {
System.out.println("Writing with the pen.");
}
public void refill() {
System.out.println("Refilling the pen.");
}
}
// Q3:
interface BasicAnimal {
void eat();
void sleep();
}
class Monkey {
void jump() {
System.out.println("Jumping monkey.");
}
void bite() {
System.out.println("Biting monkey.");
}
}
class Human extends Monkey implements BasicAnimal {
// Method of Human:
void speak() {
System.out.println("Speaking human.");
}
@Override
public void eat() {
System.out.println("Eating animal.");
}
@Override
public void sleep() {
System.out.println("Sleeping animal.");
}
}
// Q4:
abstract class Telephone {
abstract void ring();
abstract void lift();
abstract void disconnect();
}
class SmartTelephone extends Telephone {
// Method ofSmartTelephone:
void st() {
System.out.println("Method of SmartTelephone.");
}
@Override
public void ring() {
System.out.println("Ringing...");
}
@Override
public void lift() {
System.out.println("Lifting...");
}
@Override
public void disconnect() {
System.out.println("Disconnecting...");
}
}
// Q6:
interface SmartTVRemote {
void getNetflix();
}
interface TVRemote extends SmartTVRemote {
void changeChannel();
}
// Q7:
class Tv implements TVRemote {
@Override
public void changeChannel() {
System.out.println("Changing channel on TV.");
}
// we need to write getNetflix() because TVRemote inherits SmartTVRemote:
@Override
public void getNetflix() {
System.out.println("Getting Netflix on SmartTV.");
}
}
public class cwh_60_PracticeSet_ch11 {
public static void main(String[] args) {
// Question - 1 --> Create an abstract method Pen with methods write() and refill() as abstract methods.
// Question - 2 --> Use the Pen class from Q1 to create a concrete class FountainPen with additional method changeNib().
FountainPen fp = new FountainPen();
fp.write();
fp.refill();
fp.changeNib();
System.out.println();
/* Question - 3 --> Create a class Monkey with jump() & bite() methods. Create a class Human which inherits this Monkey class and implements
BasicAnimal interface with eat() & sleep() methods. */
Human h = new Human();
h.jump();
h.bite();
h.eat();
h.sleep();
System.out.println();
/* Question - 4 --> Create a class Telephone with ring(), lift(), & disconnect() methods as abstract methods.
Create another class SmartTelephone and demonstrate polymorphism. */
Telephone t = new SmartTelephone();
// t.st(); --> not allowed, because st() is method of SmartTelephone and 't' is an object of SmartTelephone & referred as Telephone. Thus POLYMORPHISM.
System.out.println("Using polymorphism:");
t.ring();
t.lift();
t.disconnect();
System.out.println();
// Question - 5 --> Demonstrate polymorphism using Monkey class from Q3.
Monkey m = new Human();
// m.speak(); // not allowed, because speak() is a method of Human and 'm' is an object of Human & referred as Monkey. Thus POLYMORPHISM.
// m.eat(); // not allowed.
// m.sleep(); // not allowed.
System.out.println("Using polymorphism:");
m.jump();
m.bite();
System.out.println();
// Question - 6 --> Create an interface TVRemote and use it to inherit another interface SmartTVRemote.
// Question - 7 --> Create a class Tv which implements TVRemote interface from Q6.
Tv tv = new Tv();
tv.changeChannel();
tv.getNetflix();
}
}