Software & Finance





Java - Polymorphism





If there are multiple classes (rectangle, circle) derived from common base class (shape), polymorphism provides the ability to call the methods in derived classes (rectangle, circle) using the base class (shape) pointers.

 

To understand the poymorphism, you need to know about the virtual functions and inheritance. Inheritance means classes can be derived from another class called base class. Derived classes will have all the features of the base class and it will have access to base class protected and public members. Derived classes will not have access to private members of the base class.

 

Regarding Virtual Functions - In C++, you need to use the virutal keyword in both base and derived classes. In C#, you need to use virual keyword in base case and override keyword in derived classes. In Java you do not need to use any keyword like virtual or override since by default all non static functions are considered as virual. You have to make it either private or use final keyword to remove the default virtual feature in each function in the Java classes.

 

When writing derived classes C++ and C# uses ":" between derived class and base class. Java uses extends keyword instead of :

 

There are 3 classes given on this example. Shape is a base class and Circle and Triangle are the classes derived from the base class Shape. The base class Shape* is used to access all two dervied class functions. It is called Polymorphism.

 

Useful links:

Polymorphism in C++

Polymorphism in C#

Polymorphism in Java

 

 

Source Code


import java.io.*;

 

 

class Shape

{

 

    public Shape()

    {

        System.out.println("Shape");

    }

 

    public void draw()

    {

        System.out.println("Drawing Shape");

    }

}

 

 

class Circle extends Shape

{

    int r;

 

    public Circle()

    {

        System.out.println("Circle");

    }

 

    public void draw()

    {

        System.out.println("Drawing Circle");

    }

}

 

 

 

class Triangle extends Shape

{

    public int a,b,c;

 

    public Triangle()

    {

        System.out.println("Triangle");

    }

 

    public void draw()

    {

        System.out.println("Drawing Triangle");

    }

}

 

class Polymorphism

{

    public static void main(String[] args)

    {

            Shape[] objects = { new Shape(),

                                    new Circle(),

                                    new Triangle()

                  };

 

        System.out.println("\n\nNow Drawing Objects\n");

 

            for(int i = 0; i < 3; i++)

                  objects[i].draw(); // // This line explains the concept of polymorphism

       

 

        System.out.println("\n");

    }

}

Output


D:\Program Files\Java\jdk1.6.0_23\bin>javac Polymorphism.java

 

D:\Program Files\Java\jdk1.6.0_23\bin>java Polymorphism

Shape

Shape

Circle

Shape

Triangle

 

 

Now Drawing Objects

 

Drawing Shape

Drawing Circle

Drawing Triangle