Here's my problem. I've got an interface that looks something like this:
public interface A { public A getInterface(); }
And I need to create another interface, which extends the previous one and has the same method with the same parameters list, except the return value type:
public interface B extends A { public B getInstance(); }
I want the interface B to have the same getInstance method to return a link to interface B, which, in its turn, extends interface A. But I get compile-time error. As I understand I cannot have getInstance() overriden like this, even if B extends A.
> Hi all! > > Here's my problem. I've got an interface that looks > something like this: > > > public interface A > { > public A getInterface(); > } > > > And I need to create another interface, which extends the > previous one and has the same method with the same > parameters list, except the return value type: > > > public interface B extends A > { > public B getInstance(); > } > ... > > Is there any other way to achieve my goal? > > Thank you?
publicinterface B extends A{
//do nothing...
}
Because B is a sub-interface of A when you call getInstance from a class that implements B, it will return a type-B object by inheritance. You may actually check this by running a print statement when you call this method:
if(someObj.getInstance() instanceof B){
System.out.println("It is an instance of B");
}else{
System.out.println("It is not an instance of B");
}
> Because B is a sub-interface of A when you call > getInstance > from a class that implements B, it will return a type-B > object by inheritance. You may actually check this > by running a print statement when you call this method: >
> if(someObj.getInstance() instanceof B){
> System.out.println("It is an instance of B");
> }else{
> System.out.println("It is not an instance of B");
> }
>
Fine. I have already considered this solution. The way you proposed involves casting A to B. But this does not give me exactly what I need. I want to use the returned B-interface link further (that is, on the same line of code) to call other B-specific methods. The following code explains what I mean:
publicinterface A {
public A getInstance();
}
publicclass B extends A {
public B getInstance();
publicvoid specificMethod();
}
It will tell you what Java version you are running.
Java 5 is Java version 1.5
1.4 will probably also compile that code so you must be running a very old JDK.
Go to the Sun Website and update because from what I've noticed most of the responses on this site are usually with the assumption that you have already update to either 1.4 or 1.5
> Hi all! > > Here's my problem. I've got an interface that looks > something like this: > > > public interface A > { > public A getInterface(); > } > > > And I need to create another interface, which extends the > previous one and has the same method with the same > parameters list, except the return value type: > > > public interface B extends A > { > public B getInstance(); > } > > > I want the interface B to have the same getInstance method > to return a link to interface B, which, in its turn, > extends interface A. But I get compile-time error. As I > understand I cannot have getInstance() overriden like > this, even if B extends A. > > Is there any other way to achieve my goal? > > Thank you? > > I guess you meant getInstance() for both the interfaces or else you wouldn't have gotten the compile error. Java doesn't allow to override a method with same signature but different return type. For further details you may want to read the spec: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#227768