wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading " Watch "Overloading " New topic
Author

Overloading

Debasis behera
Greenhorn

Joined: Oct 26, 2009
Posts: 14
method overloading is compiletime or runtime polymorphism
method binding in java at compile time or runtime
Actually in Thinking in java it is written that in java every thing is at runtime polymorphism except static method and final and private method
so

the question is show method is overloaded
if it is run time so it must invoke the method show(10);
it gives error,method not found
please give brief description



java
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Debasis please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it.

Coming to your question, at compile time, the compiler knows that the type of reference variable a is Animal and since Animal class doesn't have a show method which takes an int parameter, so you get an error.
Actually in Thinking in java it is written that in java every thing is at runtime polymorphism except static method and final and private method

This line can be explained with this example

So the thing is, method call to non-static methods at runtime are polymorphic but not for static methods.

HTH


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Debasis behera
Greenhorn

Joined: Oct 26, 2009
Posts: 14
In java actually non static method binding is compile time or run time
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Sorry but I'm unable to understand your statement. For non-static methods, the actual method call at runtime will be resolved on the basis of the actual Object on which it is called. At compile time, the compiler will see the type of the Reference used to call the method (Note there is a difference between reference and object here)...
Debasis behera
Greenhorn

Joined: Oct 26, 2009
Posts: 14
please
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Method overloading is Compile time Polymorphism...

Overriding is Runtime.

Go through K.Seirra its very clear!!!

Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Debasis:-As per Your example where you are trying to overload show()


Here you have created a reference of Animal, Now the compiler sees that Animal doesn't have an overloaded method of show. Hence it gives an exception at compile time itself.

Where as in case of overriding example by Ankit:-

Here we have created a reference of Animal (and since show() is present Animal so no compile time error), But at the runtime the instance of DOG is created so it executes Dog.show() and prints Dog.

You should be clear about reference and instance , and if the case is of Overloading Or Overriding.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

VivekSingh SinghVivek wrote:Method overloading is Compile time Polymorphism...


There is no compile time polymorphism in java. Search in java forum for more details . this topic is discussed many times there.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Debasis behera wrote:please


It wont help you. please provide enough information to get Answare
Debasis behera
Greenhorn

Joined: Oct 26, 2009
Posts: 14
thanks a lot.

Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Well yes Because Polymorphic method invocations apply only to instance methods. So that will be runtime...

But what i wanted to convey is For Overloading:-
"Reference type determines which overloaded version (based on declared argument types) is selected. Happens at compile time. The actual method that's invoked is still a virtual method invocation that happens at runtime, but the compiler will already know the signature of the method to be invoked. So at runtime, the argument match will already have been nailed down, just not the class in which the method lives."
Where as for Overriding:-
"Object type (in other words, the type of the actual instance on the heap) determines which method is selected. Happens at runtime. "
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi VivekSingh,

http://www.coderanch.com/t/380051/Java-General/java/many-types-polymorphisms#1660079 and welcome to javaranch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Debasis behera wrote:thanks a lot.


You are Welcome
Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
Just remember, runtime polymorphism is applicable for overriding and not for overloading. It is the compile time association that is applicable for overloading.


Java Certification Exam Mock Tests: SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 (Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading
 
Similar Threads
ClassCast Exception
Reference Variable Casting?
Reference variable
Why can only "real" dog bark?
polymorphism help...