Java method returning type of generic type of generic used at class

TheTonsOfCode

This is simple structure of code i wanna realize:

public class B {}
public class A<X extends B> {
    X x;
    public A(X x) {this.x = x;}
    public X getX() {return x;}
}
public class C<A> {
    public A getA() {return null;}       
    public A<T> T getGenericXOfA() {return getA().getX();}
}

I wanna to my class C by using method 'getGenericXOfA' returns type used at generic parameter of class A. Is that even possible at Java? I wanna do something like that:

public class C<A<X>> {
    public A getA() {return null;}       
    public X getGenericXOfA() {return getA().getX();}
}

or like that?:

public class C<A> {
    public A getA() {return null;}       
    public A<T> T getGenericXOfA() {return getA().getX();}
}
Andrew Tobilko

You used a raw type of the A when you need to generalise it by X which extends B:

class C<X extends B> {

    public A<X> getA() { ... }

    public X getGenericXOfA() {
        return getA().getX();
    }

}

Actually, you are playing with names of generic parameters. They don't matter and can't refer to existing classes.

If you want the same generic type like another class has, you should repeat the same template.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

incompatible type wile returning generic method of statictype

Can the generic type of a generic Java method be used to enforce the type of arguments?

Java Generic Class - Determine Type

Passing a class with type parameter as type parameter for generic method in Java

Generic type as parameter in Java Method

Return type of generic method (Java)

Generic Type of a Generic Type in Java?

Type inference in generic method & generic class

Type Parameter Class with generic java

Generic method returning generic type

Distinguish between a class generic type parameter and method generic type parameter

Result type covariance - generic class with method returning both an interface type and a specific type

Delegate for generic class and method with generic return type

generic as a generic type in Java?

Java generic method type argument

how to write a java method with generic class type?

pass generic type class to method

Generic method without class type?

java generic method return type

Java Generic Type and Object Class

Generic type in interface method Java

Class type reference not pass in java generic class or method

Generic method with generic class type constraint

Method receiving and returning generic type - how to do it?

"Class type" of "type parameter" used by generic java function

Generic method type can't be used as generic type for generic class

Have a Generic Type of a Method Depend on the Generic Type Of the Class in TypeScript

mypy: How to declare the return type of a method returning self in a generic class?

Multiple generic type for a class java