Polymorphism in Java


Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means many forms.

There are two types of polymorphism in java:

1.Compile time polymorphism

2.Runtime polymorphism.

We can perform polymorphism in java by method overloading and method overriding.If you overload static method in java, it is the example of compile time polymorphism.

1.Compile time polymorphism:

Compile time polymorphism is also known as   Static Binding Polymorphism.This is used to write the program in such a way, that flow of control is decided in compile time itself. It is achieved using method overloading.Compile time polymorphism is nothing but the method overloading in java.

In method overloading, an object can have two or more methods with same name, BUT, with their method parameters different. These parameters may be different on two bases:

1) Parameter type: Type of method parameters can be different.

The actual method to be called is decided on compile time based on parameters passed to function in program.

class abc

{

public void sum(int a)

{

System.out.println(“int value=”+a);

}

public void sum(float b)

{

System.out.println(“float value=”+b);

}

public void sum(String c)

{

System.out.println(“String value=”+c);

}

}

class xyz

{

public static void main(String arg[])

{

abc a1=new abc();

a1.sum(10);

a1.sum(2.4);

a1.sum(“Ram”);

}

}

output:

sum=10

sum=2.7

sum=Ram

2) Parameter count: Functions accepting different number of parameters.e.g.

Lets see the below example to understand it better-

class abc

{

public void sum(int a)

{

System.out.println(“sum=”+a);

}

public void sum(int b,int c)

{

System.out.println(“sum=”+b+”,”+c);

}

public void sum(int d,int e,int f)

{

System.out.println(“sum=”+d+”,”+e+”,”+f);

}

}

class xyz

{

public static void main(String arg[])

{

abc a1=new abc();

a1.sum(10);

a1.sum(10,20);

a1.sum(10,20,30);

}

}

output:

sum=10

sum=10,20

sum=10,20,30

2. Runtime Polymorphism in Java:

Runtime polymorphism is also known as  Dynamic Binding Polymorphism.It is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.

Runtime polymorphism with overriding the method:

The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime, not at the compile time.

Let’s take a look at the following example:

class animal{

public void whoru()

{

System.out.println(“I am a animal.”);

}

}

class dog extends animal

{public void whoru()

{

System.out.println(“I am a dog.”);

}

}

class goat extends animal

{

public void whoru( )

{

System.out.println(“I am a goat.”);

}

}

class cat extends animal{public void whoru()

{

System.out.println(“I am a cat.”);

}

}

class RunPoly

{

public static void main(String aa[])

{

animal ref1 = new animal();

animal ref2 = new dog();

animal ref3 = new goat();

animal ref4 = new cat();

ref1.whoru();

ref2.whoru();

ref3.whoru();

ref4.whoru();

}

}

output :

I am a animal.

I am a dog.

I am a goat.

I am a  cat.

In the example, there are four variables of type animal (e.g., ref1, ref2, ref3, and ref4). Only ref1 refers to an instance of animal class, all others refer to an instance of the subclasses of animal. From the output results, you can confirm that version of a method is invoked based on the actually object’s type.

The program is able to resolve the correct method related to the subclass object at runtime. This is called the runtime polymorphism in Java. This provides the ability to override functionality already available in the class hierarchy tree. At runtime, which version of the method will be invoked is based on the type of actual object stored in that reference variable and not on the type of the reference variable.

Runtime polymorphism with  super class methods

Super class methods can be overridden in the sub class. If the super class method has to be called from the sub class method that is overridden, then it can be achieved using super.methodname. For example, in the above Student class, the printStudent method is used to print the name and studentId. If the printStudent method is overridden in the GraduateStudent class to print noOfYears_Course along with name and studentId, then printStudent method of super class can be called within this overridden method using super. This scenario is demonstrated below:

class Student

{

int yearCourse;

public Student(String name, String stuId, int yearCourse)//constructor of the class

{

     super(name, stutId);

this.yearCourse = yearCourse;

}

public void printStudent()

{

super.printStudent();

System.out.println(“The no of years of the course is:” + Course );

}

}

class school

{

public static void main(String aa[])

{

student s1=new student(“Ram”,101,2009);

//when object is created the constructor is automatically called

s1.printStudent();

}

}

In above example:

The printStudent() underlined above is equivalent to:

 

public void printStudent()

{

System.out.println(“Name of the person is:” + name);

System.out.println(“Id of the Student is:”+ stuId);

System.out.println(“The student id is:” + stuId);

}

Comments are closed.