forked from rahulXbarnwal/JavaTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearnInterfaces.java
More file actions
67 lines (47 loc) · 1.56 KB
/
LearnInterfaces.java
File metadata and controls
67 lines (47 loc) · 1.56 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
package oops4;
public class LearnInterfaces {
public static void main(String[] args) {
Monkey monkey = new Monkey();
monkey.eats();
monkey.drinks();
// Animal.LEGS = 5; // can't do, since its final
System.out.println(Animal.LEGS);
}
}
interface Pet {
void sings();
void drinks();
}
interface Animal {
// can define only those fields inside interface which are public, static and final
// public static final int LEGS=4;
int LEGS=4;
// Modifier 'abstract' is redundant for interface methods
// since methods are by default abstract for interfaces
// public is also redundant since it has to be accessible inside the inheriting class
// so keep it default
// public abstract void eats();
void eats();
void drinks();
// default implementation -> can be overridden
default void walk() {
System.out.println("Animal is walking");
}
}
// a class can implement 2 interfaces even if they have same methods
// because implementing class will have to provide the implementation of that function
// so it will not create a deadlock like which interface's function to call
// but this is not possible when extending 2 classes, i.e. multiple inheritance is not possible
class Monkey implements Animal, Pet {
@Override
public void eats() {
System.out.println("Monkey is eating");
}
@Override
public void sings() {
System.out.println("Monkey is singing");
}
public void drinks() {
System.out.println("Monkey is drinking");
}
}