aspose file tools*
The moose likes Java in General and the fly likes Query on the topic overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Query on the topic overloading" Watch "Query on the topic overloading" New topic
Author

Query on the topic overloading

shalini saranya
Greenhorn

Joined: Mar 09, 2011
Posts: 6
Hi all

I have a doubt in overridding topic...

what will be the output for above code???
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

Welcome to JavaRanch!

Please UseCodeTags when you post source code. Being a moderator, I've added the code tags in your post above, so that your code looks much nicer than plain text.

Have you tried compiling and running the code? Can you explain yourself why you get the output that you do?

Note that similar questions have been asked before on the forums here - if you search for "overloading", you'll most likely find many useful answers quickly.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
shalini saranya
Greenhorn

Joined: Mar 09, 2011
Posts: 6
Actually the decision about which method to be invoked in case of overloading is made at compile time..
So What happens if a method is invoked by a reference of its superclass which is assigned with object of its subclass

Note:The method which is invoked exists only in sub class and not in superclass
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

shalini saranya wrote:
So What happens if a method is invoked by a reference of its superclass which is assigned with object of its subclass
Note:The method which is invoked exists only in sub class and not in superclass

On a super class reference, you can only call methods defined in the super class. If not, the compiler shall stop you right away.

Now suppose we have a super class reference pointing to a sub class object, and we call a overridden method in it.
At the compile time, the compiler shall decide to call the super class's method,yes..... but at run-time if that method is overridden in the sub class, it shall be called instead : Run-time polymorphism.
The method overriding is an example of runtime polymorphism. Java virtual machine determines the proper method to call at the runtime.


OCPJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And be careful what you say in your thread titles; I have been looking for an overloaded method and not found one.

And also please post code which actually compiles; I get this error
javac UseAnimals.java
UseAnimals.java:17: doStuff(Animal) in UseAnimals cannot be applied to (UseAnimals)
ua.doStuff(use);
^
1 error

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I think what is happening is that you are in fact overloading the doStuff method by passing a supertype as its parameter. I tried with the @Override annotation on the subclass method and got this
javac UseAnimals.java
UseAnimals.java:8: method does not override or implement a method from a supertype
@Override public void doStuff(UseAnimals ua) {
^
So your thread title was correct after all. You are overloading this method when you think you are overriding it. The reference to the identifier is the supertype, and the overloaded method is only available in the subtype, so you get a compiler error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query on the topic overloading