• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading

 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java actually non static method binding is compile time or run time
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method overloading is Compile time Polymorphism...

Overriding is Runtime.

Go through K.Seirra its very clear!!!

 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debasis behera wrote:please


It wont help you. please provide enough information to get Answare
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot.

 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi VivekSingh,

http://www.coderanch.com/t/380051/Java-General/java/many-types-polymorphisms#1660079 and welcome to javaranch
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debasis behera wrote:thanks a lot.


You are Welcome
 
Atwal Usha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remember, runtime polymorphism is applicable for overriding and not for overloading. It is the compile time association that is applicable for overloading.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic