This is a bit convoluted if you can follow along. I have this:
public interface Interface1<T> {
void method1(T t);
}
public interface Interface2 { }
public interface Interface3 extends Interface1<Interface2> { }
abstract public class BaseClass<P extends Interface1> {
P p;
void method2() {
p.method1(this);
}
}
public class Concrete extends BaseClass<Interface3> implements Interface2 {
}
Interface2 and Interface3 will change with different implementations of BaseClass. The problem is in BaseClass method2 because this
is not of type Interface2 which is what it expects in this specific case. It compiles and runs but it warns about an unchecked call.
I tried implementing the interface in the base class instead of the concrete class like this
abstract public class BaseClass<P extends Interface1, V> implements V
public class Concrete extends BaseClass<Interface3, Interface2>
but Java doesn't like it. Any ideas how to do this?
To solve
interface Interface2 { }
interface Interface1<T> {
void method1(T t);
}
interface Interface3 extends Interface1<Interface2> { }
abstract class BaseClass<P extends Interface1<Interface2>>
implements Interface2{
P p;
void method2() {
p.method1(this);
}
}
class Concrete extends BaseClass<Interface3> implements Interface2 { }
It compile without any warning, the only change I made is:
abstract class BaseClass<P extends Interface1<Interface2>> implements Interface2
If you don't want to BaseClass implements directly Interface2 you can create a "marker":
interface InterfaceFOO {} // <------------ Marker without methods
interface Interface2 extends InterfaceFOO{ } // <----- harmless extend
interface Interface1<T> {
void method1(T t);
}
interface Interface3 extends Interface1<InterfaceFOO> { }
abstract class BaseClass<P extends Interface1<InterfaceFOO>>
implements InterfaceFOO{
P p;
void method2() {
p.method1(this);
}
}
class Concrete extends BaseClass<Interface3> implements Interface2 { }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments