Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on the topic overloading

 
shalini saranya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have a doubt in overridding topic...

what will be the output for above code???
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shalini saranya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48396
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48396
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic