Forums Register Login

how can we say compile time polymorphism in java ?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
yes that right but can we give some justification and while compilation can we say this is actually worked out for method overloading.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 7201 times.
Similar Threads
Final Class Problem
Atheism or Theism??
reading file in run time
pop-up frames display for a few seconds when i run my programs outside the IDE
serilalization process
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:35:56.