-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh11_59_Practice18.java
More file actions
132 lines (113 loc) · 3.8 KB
/
Ch11_59_Practice18.java
File metadata and controls
132 lines (113 loc) · 3.8 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
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
public class Ch11_59_Practice18 {
abstract static class Pen{
abstract void write(); // All methods of an abstract class are also abstract
abstract void refill();
}
static class FountainPen extends Pen{ // All methods must be overridden of an Abstract class
void changeNib(){
System.out.println("Time to change Nib");
}
@Override
void write(){
System.out.println("Writing now...");
}
@Override
void refill(){
System.out.println("Time to refill ink...");
}
}
abstract static class Monkey{
abstract void jump();
abstract void bite();
}
interface basicAnimal{
void eat();
void sleep();
}
static class Human extends Monkey implements basicAnimal{
@Override
void jump(){
System.out.println("Jumping");
}
@Override
void bite(){
System.out.println("Biting");
}
public void eat(){ // Interface methods are always public
System.out.println("Eating");
}
public void sleep(){
System.out.println("Sleeping");
}
}
abstract static class Telephone{
abstract void ring(String contact);
abstract void lift(String incoming);
abstract void disconnect();
}
static class Smartphone80 extends Telephone{
void openApp(String appName){
System.out.println("Opening: " + appName);
}
void ring(String contact){
System.out.println("Ringing " + contact);
}
void lift(String incoming){
System.out.println("Bzzz... Incoming call: " + incoming);
}
void disconnect(){
System.out.println("Diconnecting the call...");
}
}
interface TvRemote{
void powerOn();
void channelUp();
void channelDown();
}
interface SmartTvRemote extends TvRemote{
void Netflix();
void Youtube();
void voiceSearch();
}
static class Tv implements TvRemote{
public void powerOn(){
System.out.println("TV is powering on...");
}
public void channelUp(){
System.out.println("Channel +");
}
public void channelDown(){
System.out.println("Channel -");
}
}
public static void main(String[] args) {
// Q1. Creating abstract class pen with methods write and refill.
// Q2. Using class Pen to create a concrete class Fountain
System.out.println("Q1 & Q2");
FountainPen pen = new FountainPen();
pen.changeNib();
pen.write();
System.out.println();
// Q3. Create an abstract class monkey inherited my Human having methods of Interface BasicAnimal
System.out.println("Q3");
Human action = new Human();
System.out.print("Human is now ");
action.sleep();
System.out.println();
// Q4. Create an abstract class Telephone and create a class Smartphone then show Polymorphism
System.out.println("Q4");
Telephone phn = new Smartphone80(); // Polymorphism
phn.ring("Bigg Boss");
System.out.println();
// Q5. Demonstrate polymorphism in Q3
System.out.println("Q5");
Monkey mon = new Human();
System.out.print("Look! at the tree... There's a Monkey which is "); mon.jump();
System.out.println();
// Q6. Inheriting two interfaces (TvRemote and SmartTvRemote)
// Q7. create Class which implement TvRemote
System.out.println("Q6 & Q7");
Tv tv = new Tv();
tv.powerOn();
}
}