You are here: Home / Topics / Polymorphism with interface example in Java

Polymorphism with interface example in Java

Filed under: Java on 2023-09-17 13:17:59

//  Polymorphism  with interface example

interface Shape
{
void draw();
}

class Rectangle implements Shape
{
public void draw()
{
 System.out.println("Drawing Rectangle.");
}
}

class Triangle implements Shape
{
public void draw()
{
 System.out.println("Drawing Triangle.");
}
}


class  PolymorphismTest8 
{
public static void main( String args[ ] )
{
 Shape s;
 
 Rectangle r = new Rectangle();
 s = r;
 s.draw();
 
 Triangle t = new Triangle();
 s = t;
 s.draw();
}
}


Output:

Drawing Rectangle.
Drawing Triangle.

About Author:
S
Shyam Dubey     View Profile
I am a good learner. I like posting new questions on this plateform to help other students.