-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.java
More file actions
54 lines (53 loc) · 1.15 KB
/
interfaces.java
File metadata and controls
54 lines (53 loc) · 1.15 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
public class interfaces {
public static void main(String[]args)
{
//interface = like abs and multiple inheritence(can have multiple parents)
//uses implements keyword
// can have multiple interfaces
rabbit r= new rabbit();
hawk h= new hawk();
fish f= new fish();
r.flee();
h.hunt();
f.flee();
f.hunt();
}
}
interface prey
{
// makes flee implicitily public
void flee();
}
interface predator
{
void hunt();
}
class rabbit implements prey {
//makes flee package private so public keyword used
@Override
public void flee() {
System.out.println("The Rabbit is running away");
}
}
//can implement multiple interfaces unlike extend
class fish implements prey,predator
{
//fish can both be a prey and a predator
@Override
public void flee() {
System.out.println("The fish is swimming away");
}
@Override
public void hunt()
{
System.out.println("The fish is hunting");
}
}
class hawk implements predator
{
@Override
public void hunt()
{
System.out.println("The hawk is hunting");
}
}