• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overload, override

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I have a question.
1.

In this case b will see E.amethod, and call it.
-----------
2.

In this case b will NOT see E.amethod, and call Base.amethod.

I know that the difference is 1. is override, and 2. is overload, but I don't understand WHY it works like this.
Pls. help.
Thanx.
DD
Edited by Corey McGlone: Added CODE tags
[ June 23, 2003: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the answer lies not in overloading or overridding but in inhertance
As you know in the example when u call b.<anything> the scope of the compiler is only for the methods in Base as compiler see that the parameter is int which can be promoted to double (ref JLS). It will bind b.amethod(1) call to amethod(double) base method instead of int.
One important point remember since u overload the amethod(int) in subclass b.amethod() cannot see this new method at all (inhertance rule).
Hope this helps.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first scenario, the two methods have the exact same signature. This creates an overridden method and, hence, the JVM uses dynamic binding to determine which method should be invoked at runtime.
In the second scenario, the two methods have different signatures (one takes a double while the other takes an int) so we have what is known as method overloading (multiple methods with the same "name" but different signatures). In this case, the compiler can determine which method should be invoked at compile time.
Be sure to check out these sections of the JLS for more info:
§8.4.6 Inheritance, Overriding, and Hiding
§8.4.7 Overloading
I hope that helps,
Corey
[ June 23, 2003: Message edited by: Corey McGlone ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
In case 1 , the complier knows that amethod is overridden , so leaves the decision of which method to call until runtime.
In case 2 , as amethod is overloaded , the complier will try to match up the method based based on the parameters of the reference type.
A match is possible ( 1 can fit in a double ).
If this was not possible you'd get a compile time error.
I'm not sure why it doesn't use the actual object type at run-time.
Joey
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic