1

Is it possible to do this in java without problems? My IDE highlights my code as "Raw use of parameterized class 'Etat' " and "Unchecked call to 'add(E)' as a member of raw type 'java.util.ArrayList'" My code :

public abstract class Etat<T extends Transition> {}
public abstract class Transition<E extends Etat> {}
0

1 Answer 1

3

Might you be looking for:

class Etat<E extends Etat<E, T>, T extends Transition<E, T>>  {}
class Transition<E extends Etat<E, T>, T extends Transition<E, T>> {} 

Then, you can do:

class MonEtat extends Etat<MonEtat, MaTransition> {}
class MaTransition extends Transition<MonEtat, MaTransition> {}

allowing the two types to know each other through their type parameter. For instance, if you declare:

class Etat<E extends Etat<E, T>, T extends Transition<E, T>> {
    abstract E apply(T transition);
}

You can then be assured that

MonEtat e = ...;
e = e.apply(new MaTransition()); // compiles, and knows that MonEtat is returned
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, it is the solution to my problem. Just on your first code block the parameters of the class are not in the same order as the third and the second.
Oops. Could have sworn I had fixed that, but I apparently I failed to copy it over from eclipse. Fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.