Generic Constructor call in inheritance - java

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>.

Related

Java: Declare Variable with ? extends class

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

Can I force the type of a variabile in a extended class to a subtype in Java?

Having this:
public class A{}
public class B extends A{}
public class C{
protected A x;
}
Is there any way to create a class D which extends C such that the type of x must be B in all D's instances? Note that C is a class I'm writing, so I could edit it on purpose to accomplish D needs.
Use generic type
public class C<T>{
protected T x;
}
public class D extends C<SomeConcreteType>{
//x will be SomeConcreteType
}

Two classes implementing interface with different type arguments

class SingleTon{
Data<A> a;
Data<B> b;
Data<C> c;
// ... etc
class Data<O extends I<O>>{
O o;
public void update(O o){
this.o.update(o);
}
}
}
interface I<T>{
void update(T t);
}
class A implements I<A>{
private String a;
#Override
public void update(A a) {
this.a = a.a;
}
}
class B extends A implements I<B>{
private String b;
#Override
public void update(B b) {
super.update(b);
this.b = b.b;
}
}
class C implements I<C> {public void update(C c){}}
This code cannot be compiled, because super and sub-classes trying to implements the same interface but with different type arguments.
Interface I cannot be inherited with different type arguments (A,B), anyone has a workaround to solve this?
No workaround are possible with such a hierarchy : in B you want to implement the i interface with two distinct generic types : A and B.
Generics are designed to bring more type safety and this possible ambiguity defeats that. From the client of the class, why update(B b) would be valid but update(A a) would be not ?
So the compiler will never accept it.
But with composition you could do :
class B implements i<B>{
private A a;
private String b;
#Override
public void update(B b) {
super.update(b);
this.b = b.b;
}
}
And now you can use the A a field if needed from the B instance.
With the inheritance model given below, you'll achieve a contact of i through B in A. You don't really need to implement i for class B<T>.
See the example below:
interface i<T> {
}
class A<T> implements i<T> {
}
class B<T> extends A<T> {
}
Hope it helps you to understand the structure!
I do not understand what is needed, but this code is compiled))
interface i<T extends i<T>> {
void update(T t);
}
class A<Q extends A<Q>> implements i<Q> {
private String a;
#Override
public void update(A a) {
this.a = a.a;
}
}
class B<X extends B<X>> extends A<X> implements i<X> {
String b;
#Override
public void update(X b) {
super.update(b);
this.b = b.b;
}
}

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