Polymorphism in Java

Polymorphism is the ability of an object to take on many forms. In Java, polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. In short, to see how polymorphism works we have created the above classes that extend the same class and override its methods:

  • Shape class has two methods, drawShape() and eraseShape().
  • Classes CircleSquare and Triangle extend Shape and override its two methods, each one in a different way.
  • RandomShapeGenerator class has a method, Shape next() that randomly creates one of the above classes, CircleSquare and Triangle.
  • We create a new RandomShapeGenerator instance and a new Shape array.
  • We fill the Shape array, by creating random shapes, using the next() method of RandomShapeGenerator.
  • Then we call drawShape() method, so each time the method of the object created randomly is called.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.Random;

class Shape {

    void drawShape() {
    }

    void eraseShape() {
    }
}

class Circle extends Shape {

    @Override
    void drawShape() {

    System.out.println("Circle.draw()");
    }

    @Override
    void eraseShape() {

    System.out.println("Circle.erase()");
    }
}

class Square extends Shape {

    @Override
    void drawShape() {

  System.out.println("Square.draw()");
    }

    @Override
    void eraseShape() {

    System.out.println("Square.erase()");
    }
}

class Triangle extends Shape {

    @Override
    void drawShape() {

    System.out.println("Triangle.draw()");
    }

    @Override
    void eraseShape() {

  System.out.println("Triangle.erase()");
    }
}

// A "factory" that randomly creates shapes:
class RandomShapeGenerator {

    private Random rand = new Random();
     
    //Choose randomly a circle, a squere or a triangle
    public Shape next() {

       switch (rand.nextInt(3)) {

       default:
         case 0:
             return new Circle();
         case 1:
             return new Square();
         case 2:
             return new Triangle();
       }
    }
}

public class Polymorphism {
   
    private static RandomShapeGenerator gen = new RandomShapeGenerator();
    public static void main(String[] args) {
  
       //This is an array of references of the superclass Shape
       Shape[] s = new Shape[9];
  
       // Fill up the array with random shapes shapes:

       for (int i = 0; i < s.length; i++) {
          s[i] = gen.next();
       }
  
       //s[x].drawShape will call the specific drawShape function of whichever 
       //shape is s[x] and not the drawShape function of class Shape
       //That's polymorphism.
       for (int i = 0; i < s.length; i++) {
          s[i].drawShape();
       }
    }
}

Output:


Square.draw()
Square.draw()
Circle.draw()
Triangle.draw()
Triangle.draw()
Square.draw()
Circle.draw()
Circle.draw()
Square.draw()

This was an example of polymorphism in Java.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s