I have a class tree like this:
master class abstract class Cell
AvCell extends Cell
FCell extends Cell
i have an abstract method getValue()
in Cell
Is it posibble to make the method getValue()
to return int
for AvCell
and String
for FCell
?
Can i use generics for 开发者_JAVA技巧int
String
?
Thanks!
You could use generics and declare a Cell<T>
.
Then just have getValue()
return T
abstract class Cell<T> {
abstract T getValue();
}
Now:
class AvCell extends Cell<Integer> {
}
and
class FCell extends Cell<String> {
}
No. You can, however, narrow the return type when subclassing. This is known as "covariant return types". If a method in a base class returns T, you are allow to override that method in a subclass and have it return a subclass of T.
You can do the following:
abstract class Cell<T> {
public abstract T getValue();
}
class AvCell extends Cell<Integer> {
public Integer getValue() {
return 0;
}
}
class FCell extends Cell<String> {
public String getValue() {
return "foo";
}
}
Nope, no overloading on return types.
You could declare the return type to be Object and return either an Integer or a String, if you really want to.
You cannot do that normally. You have to have return types declared and the value being returned should be of the declared type or a subtype. There are other ways to do that - declaring the method return type as Object and returning anything you want, but this is NOT the way to go.
First, a very simple question: Why do you want to return different types based on different input conditions? Generally this should be because you are doing two different things in the method based on the type you are passing. This is the place to look at. Have different methods for the different types and call one of them based on the type at hand. (And you can go further using factory patterns...)
If I understand the (changing) question correctly : No
From the Sun Java Tutorial:
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. "
You can't overload the return type of a method. The JVM would not know which method to use, because the signatures are the same.
But, you could return Object, with one filling out Integer and the other String.
Yes, JVM actually allows it, so it can be done if you can generate byte codes correctly. The catch is you have to use a different tool other than javac. Here is a good tutorial: http://today.java.net/pub/a/today/2008/07/31/return-type-based-method-overloading.html
Why is it important? We built a library but want to change the return type from void to something else. Unfortunately, it required all applications depending on it to recompile. By manipulating bytes codes, the old applications can run without recompiling.
精彩评论