Certification Objective Legal Return Types (Exam Objective 1.5)


Certification Objective —Legal Return Types (Exam Objective 1.5)

1.5 Given a code example, determine if a method is correctly overriding or overloading another method, and identify legal return values (including covariant returns), for the method.

This objective covers two aspects of return types: what you can declare as a return type, and what you can actually return as a value. What you can and cannot declare is pretty straightforward, but it all depends on whether you're overriding an inherited method or simply declaring a new method (which includes overloaded methods). We'll take just a quick look at the difference between return type rules for overloaded and overriding methods, because we've already covered that in this chapter. We'll cover a small bit of new ground, though, when we look at polymorphic return types and the rules for what is and is not legal to actually return.

Return Type Declarations

This section looks at what you're allowed to declare as a return type, which depends primarily on whether you are overriding, overloading, or declaring a new method.

Return Types on Overloaded Methods

Remember that method overloading is not much more than name reuse. The overloaded method is a completely different method from any other method of the same name. So if you inherit a method but overload it in a subclass, you're not subject to the restrictions of overriding, which means you can declare any return type you like. What you can't do is change only the return type. To overload a method, remember, you must change the argument list. The following code shows an overloaded method:

 public class Foo{    void go() { } } public class Bar extends Foo {    String go(int x) {       return null;    } } 

Notice that the Bar version of the method uses a different return type. That's perfectly fine. As long as you've changed the argument list, you're overloading the method, so the return type doesn't have to match that of the superclass version. What you're NOT allowed to do is this:

 public class Foo{    void go() { } } public class Bar extends Foo {    String go() { // Not legal! Can't change only the return type       return null;    } } 

Overriding and Return Types, and Covariant Returns

When a subclass wants to change the method implementation of an inherited method (an override), the subclass must define a method that matches the inherited version exactly. Or, as of Java 5, you're allowed to change the return type in the overriding method as long as the new return type is a subtype of the declared return type of the overridden (superclass) method.

Let's look at a covariant return in action:

 class Alpha {   Alpha doStuff(char c) {     return new Alpha();   } } class Beta extends Alpha {    Beta doStuff(char c) {     // legal override in Java 1.5      return new Beta();   } } 

As of Java 5, this code will compile. If you were to attempt to compile this code with a 1.4 compiler or with the source flag as follows:

 javac -source 1.4 Beta.java 

you would get a compiler error something like this:

 attempting to use incompatible return type 

(We'11 talk more about compiler flags in Chapter 10.)

Other rules apply to overriding, including those for access modifiers and declared exceptions, but those rules aren't relevant to the return type discussion.

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 covariant returns. Just that knowledge alone will help you through a wide range of exam questions.

Returning a Value

You have to remember only six rules for returning a value:

  1. You can return null in a method with an object reference return type.

     public Button doStuff() {   return null; } 

  2. An array is a perfectly legal return type.

     public String[] go () {  return new String[] {"Fred", "Barney", "wilma"}; } 

  3. In a method with a primitive return type, you can return any value or variable that can be implicitly converted to the declared return type.

     public int foo () {   char c = 'c';   return c;  // char is compatible with int } 

  4. In a method with a primitive return type, you can return any value or variable that can be explicitly cast to the declared return type.

     public int foo () {   float f = 32.5f;   return (int) f; } 

  5. You must not return anything from a method with a void return type.

     public void bar() {   return "this is it";  // Not legal!! } 

  6. In a method with an object reference return type, you can return any object type that can be implicitly cast to the declared return type.

     public Animal getAnimal() {   return new Horse();  // Assume Horse extends Animal } public Object getObject() {   int[] nums = {1,2,3};   return nums;  // Return an int array,                 // which is still an object } public interface Chewable { } public class Gum implements Chewable { } public class TestChewable {     // Method with an interface return type     public Chewable getChewable() {       return new Gum();  // Return interface implementer     } } 

image from book
Exam Watch

Watch for methods that declare an abstract class or interface return type, and know that any object that passes the IS-A test (in other words, would test true using the instanceof operator) can be returned from that method— for example:

 public abstract class Animal { } public class Bear extends Animal { } public class Test {    public Animal go() {       return new Bear();  // OK, Bear "is-a" Animal    } } 

This code will compile, the return value is a subtype.

image from book



SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net