• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Query on the topic overloading

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I have a doubt in overridding topic...

what will be the output for above code???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 77291
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 77291
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic