Compile Time Polymorphism in Java

In Java polymorphism is the technique which we come across every day. Polymorphism is one of the major pillars of OOP. Using polymorphism we can achieve great things. Today in this article we are going to discuss polymorphism. Let’s understand what polymorphism is.

What is Polymorphism?

Polymorphism is the combination of two words “poly” means “many” and “morph” means “form”. So, polymorphism is the ability of someone to take many forms. In terms of java, polymorphism is the ability of a method to take many forms.

Let’s take a real-life example of polymorphism. In real life, a person can be an example of polymorphism. A person in the house will be husband/wife/mother/father the same person at the office will be an employee.

In java, any object that can implement more than one IS-A relation can be considered as polymorphic. However, in Java, all objects are polymorphic as they have IS-A relation with themselves and class Object. So, conclusions from the above example:

            1.      Person IS-A human.

            2.      Person IS-A husband.

            3.      Person IS-A employee.

A person is polymorphic.

The best example of polymorphism in java is the add method. Using the add method we can perform a single operation i.e. adding of numbers in different manners like:

            1.      add(int,int)

            2.      add(int,int,int)

            3.      add(long,log)

In the above example, we can see that one single method (add) is performing different tasks.

The mechanism of polymorphism should be implemented keeping the below rules in mind:

            1.      Name of the methods should be the same.

            2.      Both methods should have a different number or different types of arguments.

 

Types of Polymorphism

Polymorphism mechanism can be broadly classified into two parts:

            1.      Compile-time polymorphism.

            2.      Run-time polymorphism.

 

Compile-time polymorphism:

Compile-time polymorphism is also known as static polymorphism. In compile-time polymorphism, the method will get resolved at compile-time rather than at run-time. If the methods have the same name but have different argument list then these methods are said to be implemented compile-time polymorphism and such methods are known as overloaded methods.

Example of Compile-time polymorphism:

Why method overloading is not possible with different data types

If two methods which have different data types in the argument list are overloaded, there may occur ambiguity exception. Understand with this example:

Using null to overload methods in java

Consider this example:

Output:

Temp called with str

Explanation:

In the above example, all methods are eligible to be called but, JVM will choose the most specific method.

Where the JVM gets null as an argument for an overloaded method,  the most specific type method will be chosen, so in this case: the string is chosen instead of the most tolerant: object.

The choice for the compiler between object/string/int is obvious: the reason being int cannot be null and therefore its associated method is not callable in this case.

But if we change int to Integer, JVM will get confused and will give an error.


Related Articles:

    1.      Core java interview questions.

    2.      Java 8 interview questions.

    3.      Abstraction in Java.

    4.    Encapsulation in Java.

    5.       Internal working of Hashmap in java.

    6.    Runtime Polymorphism in java.

Post a Comment

0 Comments