Consider a situation where I have an abstract class in Java;
public abstract class Foo { public abstract int myOperation(); }
Now some of its subclasses can override myOperation as follows:
class A extends Foo { public int myOperation() {
But if one subclass instead wants to return another data type, for example:
class A extends Foo { public Object myOperation() {
I want the method name to be the same, to keep the design intact, so that clients do not have to choose which method to call. Is there a workaround for this, other than individual methods, when one of them is an empty implementation or uses Object as the return type? Or is this a bad example of OO design?
I heard about Covariant return types in C ++ and wonder if Java has a different mechanism for this.
I can also use the interface here.
source share