polymorphism means in java code example

Example 1: what is polymorphism

The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee.

Example 2: polymorphism in java

// Polymorphism in java example
class Shapes
{
   public void sides()
   {
      System.out.println("sides() method.");
   }
}
class Square extends Shapes
{
   public void sides()
   {
      System.out.println("square has four sides.");
   }
}
class Triangle extends Shapes
{
   public void sides()
   {
      System.out.println("triangle has three sides.");
   }
}
class PolymorphismExample
{
   public static void main(String[] args)
   {
      Shapes shape = new Shapes();
      Shapes square = new Square();
      Shapes triangle = new Triangle();
      shape.sides();
      square.sides();
      triangle.sides();
   }
}

Tags:

Misc Example