This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
"A" has a signature that differs from one in the super class only by return type. That's not overloading or overriding ... it's just illegal. The following method should work whether you call it with an instance of super or an instance of p2. Think about why the compiler won't allow the signature in p2.
"B" has a signature that differs by argument type. That's overloading whether in the same class or another. The compiler and runtime can clearly tell what code to call and what should be returned.
Did that help?
BTW: Get in the habit of starting classnames with a capital letter, like Super and P2. Other folks who read the code will immediately spot the caps and realize you mean a class. [ November 01, 2006: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anyways, If you are defining the same method in same class then it's Overloading.
And if you are defining the method in another class then that is called as Overriding.
This is wrong!
It is true, that methods can be overridden only in a subclass. But you can overload methods in the same or in subclasses. And the difference has something to do with return types and parameter list.
Here's an example of an overload in a subclass:
Overriding: in a subclass method must have the same name and the same parameter list. method must have the same return type (or see covariant returns for Java5) + some additionals regarding visibility, exception throwing...
Overloading: in the same class or a subclass method must have the same name. method must have a different parameter list. method may have a different return type
The difference between overriding and overloading is extremely important. For example different behaviour in late binding. You cannot get it by posting to or reading in a newsgroup - not even the Javaranch.
Study the Sun Tutorials. Or perhaps have a first idea with wikipedia.