Commit 28482ff
committed
feat: Demonstrate multiple inheritance in Java using interfaces Walkable and Swimmable with Duck implementation
WHAT the code does:
- Declares interface Walkable with abstract method walk().
- Declares interface Swimmable with abstract method swim().
- Defines class Duck:
- Implements both Walkable and Swimmable.
- Provides concrete implementations of walk() and swim(), printing duck-specific behavior.
- In MultipleInheritanceUsingInterfaces.main():
- Creates a Duck object.
- Calls walk() and swim() to demonstrate the behavior.
WHY this matters:
- Java does not support multiple class inheritance (a class can extend only one class), but it does support multiple interface inheritance.
- This pattern allows a class to inherit multiple capabilities, promoting flexible and modular design.
- Interfaces represent “abilities” or “roles” (e.g., walk, swim), while the implementing class ties them together into a concrete entity.
- Encourages composition of behaviors without introducing ambiguity or the diamond problem found in multiple class inheritance.
HOW it works:
1. Interfaces Walkable and Swimmable define capabilities (walking and swimming).
2. Duck implements both, making it contractually obliged to define walk() and swim().
3. At runtime, Duck objects can be used wherever Walkable or Swimmable references are required, enabling polymorphism.
4. In main(), duck.walk() and duck.swim() trigger the Duck’s implementations.
Tips and gotchas:
- Always use interface names as abilities or roles (Walkable, Swimmable, Flyable) — it improves readability.
- A class can implement any number of interfaces but extend only one class.
- Interfaces define contracts, not state — fields should be avoided except for constants.
- Use polymorphism: Duck can be referenced as Walkable w = new Duck(); w.walk(); — this limits the reference to the walk() ability only.
Use-cases / analogies:
- Animals in biology: ducks can both walk and swim, dolphins swim but don’t walk, humans walk but can’t naturally swim well — different classes can combine interfaces differently.
- Vehicles: an AmphibiousCar could implement both Drivable and Swimmable.
- Game design: characters may implement multiple roles (Attackable, Healable, Movable) simultaneously.
Short key: java-interfaces multiple-inheritance duck walkable-swimmable polymorphism.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3117171 commit 28482ff
File tree
1 file changed
+13
-16
lines changed- Section14Interfaces/src
1 file changed
+13
-16
lines changedLines changed: 13 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
9 | | - | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | | - | |
14 | | - | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 20 | + | |
| 21 | + | |
23 | 22 | | |
24 | 23 | | |
25 | | - | |
26 | | - | |
| 24 | + | |
| 25 | + | |
27 | 26 | | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 31 | + | |
| 32 | + | |
36 | 33 | | |
37 | 34 | | |
38 | 35 | | |
39 | 36 | | |
40 | | - | |
| 37 | + | |
0 commit comments