• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Overriding vs Overloading

 
hired gun
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it safe to say that the method called when overloading is always determined by the type of reference and when overriding it is always determined by the type of the object?
A a = new D();
In this case
Type of Object is D
Type of Reference is A
Is the above statement always true?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Hembree:
Is it safe to say that the method called when overloading is always determined by the type of reference and when overriding it is always determined by the type of the object?


I'm not really sure what you're trying to get at, John. Overriding and overloading are two very different things.
For one thing, you can overload methods without introducing any sort of inheritence into the picture. Overloading a method is simply defining multiple methods with the same name that have different signatures.
Overriding methods, on the other hand, allows a child to behave differently than a parent when a given method is invoked. In this case, the signature must be identical in order for the overriding to function.
I don't know if that answers your question, John, but if you still have questions, let me know.
Corey
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,


Is it safe to say that the method called when overloading is always determined by the type of reference and when overriding it is always determined by the type of the object?
A a = new D();
In this case
Type of Object is D
Type of Reference is A
Is the above statement always true?


No it not true. While OverLoading doesn't look at the actual object/referrence. It looks at the method that
has same number and type of parameters no matter the method is in subclass or super class.
class A{
void m1();
}
class B extends A{
void m1(int x){}
}
In the above mentioned example the following code
A a= new B();
a.m1() always calls A.m1().
While a static overridden method is called the reference is considered to resolve the method and a non-static overridden method call is resolved using the actual object.
[ March 12, 2003: Message edited by: Sarma Lolla ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sarma Lolla:
While a static overridden method is called the reference is considered to resolve the method and a non-static overridden method call is resolved using the actual object.


A bit of a nitpick, but you can't override a static method - that doesn't make any sense. Instead, if you declare an identical static method in a child as in a parent, the method is said to "hide" the parent class' static method. Be sure to check out the JLS §8.4.6 Inheritance, Overriding, and Hiding for all sorts of good info on this topic.
Corey
[ March 12, 2003: Message edited by: Corey McGlone ]
reply
    Bookmark Topic Watch Topic
  • New Topic