forked from slgobinath/Java-Helps-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipleInheritance.java
More file actions
54 lines (46 loc) · 1019 Bytes
/
MultipleInheritance.java
File metadata and controls
54 lines (46 loc) · 1019 Bytes
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
/**
* OOP supports multiple inheritance, but Java supports through interfaces only.
* This code is an example of multiple inheritance.
*
* @author L.Gobinath
*/
public class MultipleInheritance {
public static void main(String[] args) {
Bat bat = new Bat();
doFly(bat);
doFeed(bat);
}
public static void doFly(WingedAnimal ani) {
ani.flap();
}
public static void doFeed(Mammal ani) {
ani.breastFeed();
}
}
interface Animal {
void eat();
}
interface Mammal extends Animal {
void breastFeed();
}
interface WingedAnimal extends Animal {
void flap();
}
/**
* Multiple inheritance:
* Bat implements both Mammal and WingedAnimal.
*/
class Bat implements Mammal, WingedAnimal {
@Override
public void eat() {
System.out.println("Eating");
}
@Override
public void breastFeed() {
System.out.println("Feeding");
}
@Override
public void flap() {
System.out.println("Flying");
}
}