Compile time polymorphism

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In this article we are going to learn about compile time polymorphism in java. An example of polymorphism is method overloading. i.e. the same method name can be used for multiple purposes. compile time polymorphism can be achieved using method overloading. The rules are as follows.

  • Overloaded methods must have different argument list
  • May have different return types if argument list is different.
  • May throw different exceptions
  • May have different access modifiers

For understand how method overloading uses compile time polymorphism, Let us see an example. We have two classes

  • Greeting class : This contains two overloaded methods which are used to greet.
  • Greeter class : Instantiate Greeting class in Greeter main method and call overloaded methods.

Now, Greeter class instantiates Greeting and calls overloaded methods.

This produces output,

This is what happens in the program.

  • Greeting greeting = new Greeting();  instantiates Greeting class
  • When compiler sees the statement  greeting.greet();  then it checks whether there is a method with no arguments in the Greeting class.
  • The compiler checks the method in Greeting class because we are calling greet()  method on reference variable greeting which is of type Greeting .
  • Compiler finds the  greet()  method with no arguments is present in Greeting class. Thus it compiles fine.
  • This means that  greet()  method is bound at compile time only.This is called as compile time polymorphism.
  • Similarly  greet(String name)  is checked and bound at compile time.

Remember, The overloaded method to be called is decided at compile time based on the type of reference variable.



Share Button

5 comments for “Compile time polymorphism

  1. Sudhir Kumar
    November 22, 2013 at 9:36 pm

    you have written this line-
    May have different return types if argument list is different.
    is it true or, what about this line I am writing down-
    May have different return types if argument list Type is different.

    • Prasad Kharkar
      November 23, 2013 at 12:52 am

      What do you mean by argument list type is different? If method has different signature, then different return type is fine. Only constraint is that ONLY return type can’t be different for a method to be overloading method.

  2. Sanjay
    November 3, 2015 at 9:41 am

    I think java doesn’t support compile time polymorphism because In Java, all non-static methods are by default “virtual functions.” Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are not inherited, are non-virtual.

    In c++ virtual

    With “virtual” you get “late binding”. Which implementation of the method is used gets decided at run time based on the type of the pointed-to object – what it was originally constructed as. This is not necessarily what you’d think based on the type of the pointer that points to that object.

  3. November 13, 2017 at 11:34 am

    polymorphism can be defined as interface that can be used to perform related but different tasks.many thanks for sharing this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *