[ Pobierz całość w formacie PDF ]
to have so much fun in one chapter, so we re holding the rest of the discussion on
instantiating inner classes until Chapter 8.
CERTIFICATION OBJECTIVE
Legal Return Types (Exam Objective 1.4)
Identify legal return types for any method given the declarations of all related methods in
this or parent classes.
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 just the return type. To overload a method,
remember, you must change the argument list. The following code shows an overloaded
method:
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 5
Composite Default screen
Legal Return Types (Exam Objective 1.4)
37
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
When a subclass wants to change the method implementation of an inherited method,
the subclass must define a method that matches the inherited version exactly. As we
saw earlier in this chapter, an exact match means the arguments and return types
must be identical. 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 cannot. 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 returnnullin a method that has an object reference return type.
public Button doStuff() {
return null;
}
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 5
Composite Default screen
Chapter 5: Object Orientation, Overloading and Overriding, Constructors, and Return Types
38
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 avoidreturn 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
}
[ Pobierz całość w formacie PDF ]