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

Prasad Kharkar

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.

6 thoughts on “Compile time polymorphism

  • Pingback:runtime polymorphism in java | theJavaGeek

  • November 22, 2013 at 9:36 pm
    Permalink

    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.

    Reply
    • November 23, 2013 at 12:52 am
      Permalink

      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.

      Reply
  • November 3, 2015 at 9:41 am
    Permalink

    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.

    Reply
  • November 13, 2017 at 11:34 am
    Permalink

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

    Reply
  • August 1, 2018 at 1:14 pm
    Permalink

    Really it was an awesome article…very interesting to read..You have provided an nice article….Thanks for sharing..
    Are you looking for the best Java training institute in Chennai?
    Come and learn with Aorta, the best java training institute in Chennai offering the best platform to learn and get the depth of Java
    java training in chennai
    java course in chennai

    Reply

Leave a Reply

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