Java Polymorphism

In this tutorial, we will learn about polymorphism and how polymorphism of the OOPs can be achieved in the java with working examples and use cases of it.

Java Polymorphism

Polymorphism is a concept of having same name but exists in different forms, consider human beings. Humans can be Male or Female and both, all will be under the same name and form of each human being dependents on ethnicity, gender, and genetic properties.

In computer language, polymorphism having the same name but will accept different forms.

Two types of polymorphism available and as follows.

  1. Compile-time polymorphism
  2. Run time polymorphism

Consider an example of addition of two numbers, as number can be decimal or integer type but operation is addition but in java integer belongs to int type and decimal can be float or double. This type of polymorphism called as compile time polymorphism.

Overriding a method of super class is a compile time polymorphism. Overriding a method will be helpful if you want add an extra feature or computation on parent method without changing it.

Compile-time polymorphism example

Consider the following example of polymorphism in Java for addition of two numbers with different types of numbers.

Example:

CopiedCopy Code
public class Addition {
	public static void main(String[] args) {
		int i1 = 2, i2 = 3;
		float f1 = 1.3F, f2 = 3.5F;
		double d1 = 3.5677, d2 = 4.6789;
		System.out.println(new Addition().add(i1, i2));
		System.out.println(new Addition().add(f1, f2));
		System.out.println(new Addition().add(d1, d2));
	}
	public Integer add(Integer x, Integer y) {
		return x + y;
	}
	public Double add(Double x, Double y) {
		return x + y;
	}
	public Float add(Float x, Float y) {
		return x + y;
	}
}

Output:

5
4.8
8.246599999999999

After running the example above output gets printed, explanation of the output and example as follows.

  1. Created a class with name as Addition
  2. Contains three methods with the name as "add" with three different types
  3. In the main method, we are just calling add method.
  4. Based on the passing parameter type for add appropriate method will be invoked.
  5. For two integers result is in integer
  6. For two float numbers result is in float
  7. For two double numbers result is in double.

Note: Compile time polymorphism can be achieved by method overriding.

Runtime Polymorphism example

Runtime polymorphism can be achieved by overriding super class method in the child class.

Example:

CopiedCopy Code
class X
{
	 private String property="X class method called";
	 public String getProperty()
	 {
		 return property;
	 }
	 public void setProperty(String property) {
		 this.property= property;
	 }
}
public class Y extends X {
	public static void main(String[] args) {
		X parent =new Y();
		System.out.println(parent.getProperty());
	}
	@Override
	public String getProperty()
	 {
		 return "Y class method called";
	 }
}

Output:

Y class method called

After running the example, above line gets printed. Explanation and output of example as follows.

  1. Created two classes with names X and Y.
  2. Y extends the class of X.
  3. Y overridden the method of X class, overridden method name is getProperty.
  4. In the main method X class is instantiated with Y class reference or Object.
  5. Invoked method getProperty
  6. It printed "Y class method called", because the reference passed to X is Y.
  7. During runtime Y reference method will be invoked as this method is overridden by Y.

The above one is classical example of Run time polymorphism.

Conclusion:

In this tutorial, we have learned about what is polymorphism and types of polymorphism with working examples.