-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path41_Interface
More file actions
62 lines (54 loc) · 1.31 KB
/
41_Interface
File metadata and controls
62 lines (54 loc) · 1.31 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
// interface = a template that can be applied to a class,
// similar to inheritance, but specifies what a class has/must do.
// classes can apply more than one interface, inheritance is limited to 1 super class
<Main.java>
public class Main{
public static void main(String[] args) {
Rabbit rabbit = new Rabbit();
rabbit.flee();
Hawk hawk = new Hawk();
hawk.hunt();
Fish fish = new Fish();
fish.flee();
fish.hunt();
}
}
<Prey.java>
public interface Prey {
void flee();
}
<Predator.java>
public interface Predator {
void hunt();
}
<Rabbit.java>
public class Rabbit implements Prey{
@Override
public void flee(){
System.out.println("*The rabbit is fleeing*");
}
}
<Hawk.java>
public class Hawk implements Predator{
@Override
public void hunt(){
System.out.println("*The hawk is hunting*");
}
}
<Fish.java>
public class Fish implements Prey, Predator {
@Override
public void flee() {
System.out.println("*This fish is fleeing from a larger fish*");
//TODO Auto-generated method stub
}
@Override
public void hunt(){
System.out.println("*This fish is hunting smaller fish*");
}
}
>>
*The rabbit is fleeing*
*The hawk is hunting*
*This fish is fleeing from a larger fish*
*This fish is hunting smaller fish*