Compile Time Polymorphism

In this class, We discuss Compile Time Polymorphism.

The reader should have prior knowledge of method overloading in Java. Click Here.

Polymorphism: Polymorphism means the same entity performing different operations in different scenarios.

Example:

class A

{

void m1()

{

System.out.println(“method with no parameters”);

}

void m1(int a)

{

System.out.println(“method with one parameter”);

}

}

class test

{

public static void main(String args[])

{

A ob = new A();

ob.m1();

ob.m1(2);

}

}

We have two methods with the same name, m1.

The logic in both methods is different.

The same method, m1, working with different logics is called polymorphism.

Method overloading is called compile-time polymorphism.

Compile time polymorphism means identifying which method to execute during compile time.

The method names are the same in method overloading, but the signature is different.

Identification is done by using the signature during compile time

When calling the method m1(2), we will use the second m1 method.

The parameters match the second m1 method.

In our next class, we discuss run-time polymorphism.