1) How to solve this problem in Java?
2) What if only the return types are different in the interfaces?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Essentially, the different “versions” of the method in the respective interfaces all bind to the same implementation method.
If that’s what you want, fine.
If you actually want a different version of the method for each interface then you are stuck. You can’t do that in Java.
If the return types are incompatible, you cannot write a class that implements all of the interfaces. Compilation error.
So when are return types compatible?
Prior to Java 5.0, the return type of an overriding or implementing method had to be the same as the return of the overridden or implemented method.
In Java 5.0, the language was modified to allow you to override / implement a method with a return type that is a subclass of the return type of the overridden / implemented method.
So … if you are using Java 5.0 or later … you need to implement using a return type that is the same type or a subclass of all of the return types of all versions of the method. If there is no such type, then the class is not implementable. And obviously, this doesn’t work for primitive return types, because no subclass relationship exists between primitive types.
(Note: in Java 5.0 +, we are talking about a subclass relationship between the declared return types … not the runtime types of the returned objects.)