In this blog, we will see what is Polymorphism? And how can we achieve it in Java.

What is polymorphism?

The general definition of polymorphism is single task can be performed in different ways. Example is Arithmetic operations. Arithmetic operations are general operations, it consists of addition, subtraction, multiplication and division. And you can perform addition the two integer numbers or floating point numbers similarly others.

Types of polymorphism?

Polymorphism Java

There are two types of polymorphism.

  • Compilation time polymorphism
  • Runtime polymorphism

In java we can achieve polymorphism in two ways.

  • Method overloading – Compilation time polymorphism
  • Method overriding – Runtime polymorphism

Method overloading

In short, we can define method overloading is same name and different parameters. It means, method name in java class should be same, but the number of arguments will be different. Method overloading is the best example of compiling time polymorphism, and it is possible in same class.

Package COM.samplecoder.polymorphism;

import java.time.LocalDate;

public class ParentClass {
	public void method(String msg) {

	// compile time polymorphism
	public void method(String msg, LocalDate dob) {
		System.out.println(msg+"   "+dob);

Method overriding

We can define, in short method overriding is same name and same arguments but behavior is different. It means, In Java class method name and arguments should be same, but functionality or behavior of the method is different. Method overriding is the best example for runtime polymorphism. We can override the method in child class only.

package com.samplecoder.polymorphism;

public class ChildClass extends ParentClass{

	// Runtime polymorphism
	public void method(String msg) {
		// TODO Auto-generated method stub
		super.method("Hi!  "+msg);