How to implement an override method with a different return type?

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() { // Do stuff } } 

But if one subclass instead wants to return another data type, for example:

 class A extends Foo { public Object myOperation() { // Do stuff } } 

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.

+4
source share
6 answers

You can not. Neither using inheritance nor using an interface for this. This will be a compiler error, so you cannot run your program at all.

You can return java.lang.Object and return any desired object. If you need to return a primitive, you can return its wrapper of the object.

+8
source

"is a very bad example of OO design." Yes. You cannot do this.

+7
source

Java has options for a joint return option, but the return type should be more specific, no less specific. If the method of the parent class returns int , then all its subtypes must also do, otherwise the calling object of such a method does not really know what type it gets.

 Foo foo = new A() int i = foo.method(); 

If method () returns anything other than int , this statement no longer makes sense.

If you can do this, if the method in the superclass return Object and the class dervice return Number , then a subclass of both can return Integer

+5
source

You can have different data types when you use the return type as derivatives ie, Co-varients. when you use a primitive data type, you cannot change it.

If the Super class method is any derived data type, while redundant retrieval in the return type of the subclass can be the same derived data type or derived types of the subclass.

+3
source

This is not possible because the method declared in the abstract class defines the contract: it returns an int. This contract must be respected by all implementations. What happens if you do this:

 Foo foo = new A(); int i = foo.myOperation(); 

This will break because myOperation will not return an int . That is why it is not allowed.

However, the following is allowed:

 public abstract class Foo { public abstract Object myOperation(); } public class A extends Foo { @Override public String myOperation() { return "some string"; } } 

In this case, the contract is respected. The method should return an object, it returns a string, and a string - an object.

+2
source

You cannot do this because it will violate the Foo contract.

 A a = new A(); someUtilityMethod(a); ... void someUtilityMethod(Foo foo){ int i = foo.myOperation(); // This would break } 

This principle is called the Liskov signature principle and it is the basis on which OO is built.

+2
source

All Articles