Foro sobre Java SE > Duda ejercicio Generics - Libro practice exams BB && KS
Hola a todos!
Estoy tratando de entender un ejercicio pero no me es claro el por qué de la respuesta que está en el libro, el ejercicio es el siguiente:
public class Organic<E> { void react(E e) {} public static void main(String[] args) { Organic<? extends Organic> compound = new Aliphatic<Organic>(); //Organic<? super Aliphatic> compound = new Aliphatic<Organic>(); compound.react(new Organic()); compound.react(new Aliphatic()); compound.react(new Hexane()); } }
class Aliphatic<F> extends Organic<F> {} class Hexane<E> extends Aliphatic<E> {}
Las opciones de respuesta son:
Which, taken independently, are true? (Choose all that apply.) A. If line 5 is uncommented, compilation fails due to an error at line 7. B. If line 5 is uncommented, compilation fails due to an error at line 8. C. If line 5 is uncommented, compilation fails due to an error at line 9. D. If line 6 is uncommented, compilation fails due to an error at line 7. E. If line 6 is uncommented, compilation fails due to an error at line 8. F. If line 6 is uncommented, compilation fails due to an error at line 9.
Las respuesta es: A, B, C, and D are correct.
Pero no me queda del todo claro porque al quitar el comentario a:
Organic<? extends Organic> compound = new Aliphatic<Organic>();
Ninguna de las 3 llamadas al método react funciona.
Hola a todos!
Estoy tratando de entender un ejercicio pero no me es claro el por qué de la respuesta que está en el libro, el ejercicio es el siguiente:
public class Organic<E> {
void react(E e) {}
public static void main(String[] args) {
Organic<? extends Organic> compound = new Aliphatic<Organic>();
//Organic<? super Aliphatic> compound = new Aliphatic<Organic>();
compound.react(new Organic());
compound.react(new Aliphatic());
compound.react(new Hexane());
}
}
class Aliphatic<F> extends Organic<F> {}
class Hexane<E> extends Aliphatic<E> {}
Las opciones de respuesta son:
Which, taken independently, are true? (Choose all that apply.)
A. If line 5 is uncommented, compilation fails due to an error at line 7.
B. If line 5 is uncommented, compilation fails due to an error at line 8.
C. If line 5 is uncommented, compilation fails due to an error at line 9.
D. If line 6 is uncommented, compilation fails due to an error at line 7.
E. If line 6 is uncommented, compilation fails due to an error at line 8.
F. If line 6 is uncommented, compilation fails due to an error at line 9.
Las respuesta es: A, B, C, and D are correct.
Pero no me queda del todo claro porque al quitar el comentario a:
Organic<? extends Organic> compound = new Aliphatic<Organic>();
Ninguna de las 3 llamadas al método react funciona.
Muchas gracias de antemano!!