Interface with Multiple Implementations
This program demonstrates multiple classes implementing same interface.
Problem Statement
Write a Java program where Circle and Rectangle both implement Drawable interface.
Source Code
| 1 | interface Drawable { |
| 2 | void draw(); |
| 3 | } |
| 4 | class Circle implements Drawable { |
| 5 | public void draw(){ System.out.println("Drawing Circle"); } |
| 6 | } |
| 7 | class Rectangle implements Drawable { |
| 8 | public void draw(){ System.out.println("Drawing Rectangle"); } |
| 9 | } |
| 10 | public class InterfaceMultiple { |
| 11 | public static void main(String[] args) { |
| 12 | System.out.println("Eduinq Interface Multiple Implementations"); |
| 13 | Drawable d1=new Circle(); |
| 14 | Drawable d2=new Rectangle(); |
| 15 | d1.draw(); d2.draw(); |
| 16 | } |
| 17 | } |
Program Output
Eduinq Interface Multiple Implementations Drawing Circle Drawing Rectangle
Explanation
Multiple classes implement same interface. - Each provides its own version of draw(). - This demonstrates polymorphism through interfaces. Conclusion: Shows how multiple classes can implement same interface.