-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybridinheritance.java
169 lines (139 loc) · 3.21 KB
/
hybridinheritance.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Base interface for common behaviors
interface Animal {
void eat();
void breathe();
}
// Interface for swimming behavior
interface Swimmable {
void swim();
}
// Interface for walking behavior
interface Walkable {
void walk();
}
// Derived interface from Animal for fish-specific behaviors
interface Fish extends Animal, Swimmable {
}
// Derived interface from Animal for bird-specific behaviors
interface Bird extends Animal, Walkable {
}
// Derived interface from Animal for mammal-specific behaviors
interface Mammal extends Animal, Walkable {
}
// Classes implementing the interfaces
// Fish
class Shark implements Fish {
@Override
public void eat() {
System.out.println("Shark is eating");
}
@Override
public void breathe() {
System.out.println("Shark is breathing");
}
@Override
public void swim() {
System.out.println("Shark is swimming");
}
}
class Rohu implements Fish {
@Override
public void eat() {
System.out.println("Rohu is eating");
}
@Override
public void breathe() {
System.out.println("Rohu is breathing");
}
@Override
public void swim() {
System.out.println("Rohu is swimming");
}
}
// Bird
class Parrot implements Bird {
@Override
public void eat() {
System.out.println("Parrot is eating");
}
@Override
public void breathe() {
System.out.println("Parrot is breathing");
}
@Override
public void walk() {
System.out.println("Parrot is walking");
}
}
class Peacock implements Bird {
@Override
public void eat() {
System.out.println("Peacock is eating");
}
@Override
public void breathe() {
System.out.println("Peacock is breathing");
}
@Override
public void walk() {
System.out.println("Peacock is walking");
}
}
// Mammal
class Cat implements Mammal {
@Override
public void eat() {
System.out.println("Cat is eating");
}
@Override
public void breathe() {
System.out.println("Cat is breathing");
}
@Override
public void walk() {
System.out.println("Cat is walking");
}
}
class Human implements Mammal {
@Override
public void eat() {
System.out.println("Human is eating");
}
@Override
public void breathe() {
System.out.println("Human is breathing");
}
@Override
public void walk() {
System.out.println("Human is walking");
}
}
public class OOPS {
public static void main(String[] args) {
// Creating instances and calling methods
Shark shark = new Shark();
shark.eat();
shark.breathe();
shark.swim();
Rohu rohu = new Rohu();
rohu.eat();
rohu.breathe();
rohu.swim();
Parrot parrot = new Parrot();
parrot.eat();
parrot.breathe();
parrot.walk();
Peacock peacock = new Peacock();
peacock.eat();
peacock.breathe();
peacock.walk();
Cat cat = new Cat();
cat.eat();
cat.breathe();
cat.walk();
Human human = new Human();
human.eat();
human.breathe();
human.walk();
}
}