Skip to content

Commit 3117171

Browse files
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

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
class phone
2-
{
1+
class phone {
32
public void call(){System.out.println("Phone call");}
43
public void sms(){System.out.println("Phone sms");}
54
}
65

7-
interface ICamera
8-
{
6+
interface ICamera {
97
void click();
108
void record();
119
}
12-
interface IMusicPlayer
13-
{
10+
11+
interface IMusicPlayer {
1412
void play();
1513
void stop();
1614
}
17-
class SmartPhone extends phone implements ICamera, IMusicPlayer
18-
{
15+
16+
class SmartPhone extends phone implements ICamera, IMusicPlayer {
1917
public void videoCall() { System.out.println("Smart Phone video calling"); }
2018

2119
public void click() { System.out.println("Smart Phone Clicking Photo"); }
2220
public void record() { System.out.println("Smart Phone recording video"); }
2321
public void play() { System.out.println("Smart Phone playing music"); }
2422
public void stop() { System.out.println("Smart Phone stopped playing music"); }
2523
}
26-
public class InterfacesIMP {
27-
public static void main(String[] args)
28-
{
29-
//We can create reference of interfaces but cant crete object of it.
3024

25+
public class InterfacesIMP {
26+
public static void main(String[] args) {
27+
//We can create a reference of interfaces but cant crete an object of it.
3128
IMusicPlayer M=new SmartPhone();
3229
M.play();
3330
M.stop();
@@ -36,4 +33,4 @@ public static void main(String[] args)
3633
C.click();
3734
C.record();
3835
}
39-
}
36+
}

0 commit comments

Comments
 (0)