aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding and overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding and overloading" Watch "Overriding and overloading" New topic
Author

Overriding and overloading

ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
I was going through K&B chapter2: Object Orientation , when i came across this line : " For the exam , be sure you know that overloaded methods can change the return type but overriding methods can do so only within the bounds of co-varient returns" .

My doubt is :how "overriding methods can do so only within the bounds of covarient returns" .? What it actually means ?
jonad jons
Greenhorn

Joined: Mar 28, 2012
Posts: 1

it means that the overriding method return type can be one of his subclass return type.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Covariant just means the overridden method can return a subclass of the return type declared in the parent class (This was introduced in Java 5). Tip: Always use the @Override annotation when you override. It will save you tons of grief.

Original method:


OVERLOAD: "overloaded methods can change return type" (and the method signature must change. This means changing the number of arguments and / or the types)


OVERRIDE: "overridden methods must have the same return type OR covariant return type
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Tim McGuire wrote:
OVERLOAD: "overloaded methods can change return type"


importantly signature[method name and argument] must be different in case of overloading
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Seetharaman Venkatasamy wrote:
Tim McGuire wrote:
OVERLOAD: "overloaded methods can change return type"


importantly signature[method name and argument] must be different in case of overloading


Yow. I overlooked that. I'll edit the original.
 
 
subject: Overriding and overloading