Java: Declare Variable with ? extends class - java

i have the following Problem. I have a Interface
public interface A<T extends B>
{
T getObject();
}
And i have a class using this interface:
public void test()
{
A<B> a;
B b = a.getObject();
}
This causes an compile error cause the returned Object is B and not extending B.
Is there any way i can declare variable b with ?
Thanks,
Gertrude

Related

Call generic method with interface constraints on objects who may implement that interface

This is my code:
interface a {}
class b{}
class c extends b implements a{}
class d extends b{}
class e{
public void makeItWork(){
b[] bees = new b[] {new c(), new d()};
for (b bee: bees){
if (bee instanceof a) {
a beeA = (a) bee;
//how to call the method test if object bee conforms the the interface?
test(beeA.getClass(), beeA);
//this goes wrong
}
}
}
public <T extends a> void test(Class<T> classType, T concrete){
}
}
Besides maybe the bad design, I would like to know if it is possible to call the method test on objects who implements the interface a.
your test method doesn't need a generic type parameter.
You can define it as:
public void test(Class<? extends a> classType, a concrete) {
}
P.S. please use capitalized class names.
You can actually get away without using generics at all here:
public void test(a concrete) {
}

Generic Constructor call in inheritance

I have the following scenario :
public abstract class A{}
public class B extends A{}
public abstract class C<T extends A>{
private T t;
public C(){}
public C(T t){
this.t = t;
}
}
public class D<B> extends C<A>{
private B b;
public D(B b){
super(b);
this.b=b;
}
}
But i am getting an error in the constructor of class D. What am i missing? Java 1.7
In the code you gave, B (the generic parameter of D) doesn't have to extend A so the bound of generic parameter from C is not satisfied.
Change D<B> extends C to D extends C<B>.

Multiple Level of Inheritance

I have the following Java class with multiple level of inheritance with certain type parameters. I want to use the type parameter T in class B.
class B extends C {
}
class C<T extends D> {
}
class D {
}
However, he following doesn't compile:
class B extends C {
T t;
}
class C<T extends D> {
}
class D {
}
Although I can define the variable t in class C, but it is not a good coding practice. How can I define the following (This doesn't compile as well)?
class B extends C<T extends D> {
}
Thanks!
Type parameters are not inherited!
If you want to have your class B generic, you should specify its own type parameter:
class B<T extends D> extends C<T> {
T t;
...
}
Note that you must again constrain the type parameter T to have it extending D because it is constrained this way in class C.
It should be :
class B<T extends D> extends C<T> {
}

Guice, extending interfaces and constructor return type

Interface A and its implementation:
public interface A<K, E> {
public void foo();
}
public abstract class AImpl<K, E> implements A<K, E> {
public void foo(){};
}
Interface B, which extends interface A, and its implementation:
public interface B extends A<Integer, String> {
public void bar();
}
public class BImpl extends AImpl<Integer, String> implements B {
public void bar(){};
}
An abstract class C, which gets A injected:
public abstract class C<K, E> {
A<K, E> a;
#Inject
public setA(A<K, E> a){
this.a = a;
}
public A<K, E> getA(){
return a;
}
}
With Guice:
bind(new TypeLiteral<A<Integer, Book>>(){}).to(BImpl.class);
And the last class, which extends class C:
public class D extends C<Integer, String> {
public void fooBar(){
this.getA().bar(); //Gets BImpl injected by Guice, and call bar(): Not working - error
((B) this.getA()).bar(); //Working
}
}
Like you can see from inline comments, BImpl gets properly injected and can be used, if it has no additional methods, that extends A (interface B is empty). If I add any new method in B, I can't call it in D without it casting to B. My main goal is, giving a user possibility to extend A and use this functionality in D.
If I add any new method in B, I can't call it in D without it casting to B. My main goal is, giving a user possibility to extend A and use this functionality in D.
If the user needs the functionality provided by B but not A, they should declare that they need a B. Class D should declare what it needs - not rely on casting to make sure it was correctly configured beyond what was declared.

Java generic type's inferring

I have class structure like this:
class A1,A2,..,An extends A;
class B1,B2,..,Bn extends B;
And class that converts Ai into Bi:
private B1 convert(A1 a1){}
...
private Bn convert(An an){}
How can I define single public method with signature like <? extends B> convert(<? extends A> a)?
Now I have only this approach:
B convert(A a){
if(A.getClass().equals(A1.class)){
return convert((A1)a);
}...
}
Can I use instanceof if perfomance is important and the method will be called frequently?
A more elegant solution will probably be to declare a method in A: [preferably abstract, if A is abstract]:
public abstract B toB();
Overriding classes (A1,A2,...) will have to override it and instantiate their own B object.
Code snap [the static modifier is used since I implemented it as an inner class, it is not needed and cannot be used if the classes are outer classes]:
public abstract static class A {
public abstract B toB();
}
public static class A1 extends A {
#Override
public B1 toB() {
return new B1();
}
}
public static class B {
}
public static class B1 extends B {
}
you could do something like:
public <AType extends A, BType extends B> BType convert(AType a) {...
But your could have converter interface like:
public interface Converter<AType extends A, BType extends B> {
AType convert(BType b);
BType convert(AType a);
}
Regarding the performance question, you could take a look here

Categories