Interface Polymorphism

This program demonstrates polymorphism with interfaces.

Problem Statement

Write a Java program to show polymorphism using interface Playable.

Source Code

1 interface Playable {
2 void play();
3 }
4 class Guitar implements Playable {
5 public void play(){ System.out.println("Playing Guitar"); }
6 }
7 class Piano implements Playable {
8 public void play(){ System.out.println("Playing Piano"); }
9 }
10 public class InterfacePolymorphism {
11 public static void main(String[] args) {
12 System.out.println("Eduinq Interface Polymorphism");
13 Playable p1=new Guitar();
14 Playable p2=new Piano();
15 p1.play(); p2.play();
16 }
17 }

Program Output

Eduinq Interface Polymorphism
Playing Guitar
Playing Piano

Explanation

Interface defines contract method play(). - Different classes implement interface with their own behavior. - Polymorphism allows interface reference to point to different implementations. - This demonstrates how interfaces enable flexible design and runtime behavior switching. Conclusion: Shows how interfaces support polymorphism.

Quick Links to Explore