Commit 3117171
committed
feat: Demonstrate multiple interface implementation with SmartPhone extending phone and using polymorphic references
WHAT the code does:
- Defines class phone:
- Provides basic methods call() and sms().
- Declares interface ICamera with methods click() and record().
- Declares interface IMusicPlayer with methods play() and stop().
- Defines class SmartPhone:
- Extends phone (inherits call() and sms()).
- Implements ICamera and IMusicPlayer, providing concrete implementations for click(), record(), play(), and stop().
- Adds its own method videoCall().
- In InterfacesIMP.main():
- Creates interface references IMusicPlayer and ICamera pointing to SmartPhone objects.
- Demonstrates polymorphism by calling only the methods declared in the interfaces (play/stop for IMusicPlayer, click/record for ICamera).
WHY this matters:
- Illustrates Java’s approach to multiple inheritance:
- A class can extend one superclass and implement multiple interfaces, gaining combined behavior.
- Demonstrates runtime polymorphism:
- A SmartPhone object can be treated as an IMusicPlayer, ICamera, or phone, depending on the reference type.
- Reinforces the principle of programming to an interface, not an implementation, which improves flexibility and decoupling.
HOW it works:
1. SmartPhone inherits call() and sms() from phone.
2. SmartPhone provides definitions for all abstract methods from ICamera and IMusicPlayer.
3. In main(), `IMusicPlayer M = new SmartPhone();` creates a SmartPhone but restricts access to only play() and stop().
4. Similarly, `ICamera C = new SmartPhone();` restricts access to camera-related methods.
5. The actual object (SmartPhone) supports all capabilities, but the interface reference dictates which subset of behavior is visible.
Tips and gotchas:
- Interfaces define contracts; implementation classes must fulfill them.
- An object can be referenced through multiple interfaces, enabling flexible design, but only the interface’s methods are visible without casting.
- Avoid naming classes with lowercase (phone → Phone) to follow Java naming conventions.
- For real-world extensibility, interfaces can evolve with default methods (introduced in Java 8).
- If you need access to both ICamera and IMusicPlayer methods from the same object, you can downcast or hold multiple interface references.
Use-cases / analogies:
- Smartphones in reality: act as phones, cameras, and music players — same physical object, multiple functional roles.
- Vehicles: a modern car could implement Drivable, MusicSystem, GPSDevice, all accessed separately.
- Software plugins: an object may implement multiple interfaces like Saveable, Printable, Shareable depending on context.
Short key: java-interfaces multiple-inheritance polymorphism smartphone demo contract-implementation.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7f34ab0 commit 3117171
1 file changed
+10
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | | - | |
8 | | - | |
| 6 | + | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | | - | |
13 | | - | |
| 10 | + | |
| 11 | + | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | | - | |
18 | | - | |
| 15 | + | |
| 16 | + | |
19 | 17 | | |
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
25 | 23 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
31 | 28 | | |
32 | 29 | | |
33 | 30 | | |
| |||
36 | 33 | | |
37 | 34 | | |
38 | 35 | | |
39 | | - | |
| 36 | + | |
0 commit comments