File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how can we say compile time polymorphism in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can we say compile time polymorphism in java ?" Watch "how can we say compile time polymorphism in java ?" New topic
Author

how can we say compile time polymorphism in java ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi,

We know method overloading is the compile time ploymorphism. Means compiler is able to match the name of the methods at the compile time only.

But how can we prove this with any example that this is happening in compile time only..

any practical example instead of the theoretical concept.

can we prove this practically with some exmaple.


Regards,
Prabhat
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

Prabhat Ranjan wrote:But how can we prove this with nay example that this is happening in compile time only..

What example exactly do you expect? Overloaded methods are resolved at compile time, that's just how Java works, I don't see how you could prove that it always works like this with an example.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
yes that right but can we give some justification and while compilation can we say this is actually worked out for method overloading.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

Ok, here's a simple example.

In line 13, the version of the method that takes an int is called, and in line 14, the version that takes a String is called. To decide which of the two methods to call, Java looks at the number and type of the arguments that you are passing to it. Obviously, if you call it with String, only the version that takes a String applies. It would be strange if it would work differenly. What more justification do you need for this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40


I can take a shot at this. Let's modify Jesper's example.



Notice that at line 16, it is a compile error, even though the actual instance has the method that takes a string. The reason is that the signature checking is only done at compile time.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
First i agree with Jasper, it is correct the matching methods
say if you have sum(int), sum(String) two methods then you can overload only two methods of the same name

like sum(5); and sum("prabhat");

here if you pass sum(12.5); this line will give you compile time error only.

I will give input on henry conclusion.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
My reply on henry example.

Example e = new ExampleWithString();
e.method(23);
e.method("Hello"); // Error. Even though instance has method.

in line no 16 it is giving Error because methods are overloaded into the same class only , am i right ?

e is the instance of ExampleWithString ,but compile time polymorphism works on the ref type not Object Type(instance) so compiler is trying to find and match the method with name method(with String type) only but it is defined in class Example.

So compiler gives the compile time method no matching method found.

is my understanding is right ! Henry it is ok.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can we say compile time polymorphism in java ?