I'm struggling with these (similar) problems on the bounded quantification of generics in Java:
Write the header for a generic class named MyType. The class should have one type parameter. The type parameter’s upper bound should be the String class.
Write the header for a generic class named MyType. The class should have one type parameter. The type parameter’s lower bound should be the Integer class.
public class myType<T extends String>{}
and
public class myType<T extends Integer>{}
is this correct im really having a hard time grasping the concept of generics in java
You don't use extends to define both the upper bound and the lower bound.
According to Wikipedia's article on Generics in Java...
To specify the upper bound of a type wildcard, the extends keyword is used...
So to define the upper bound, use extends:
public class myType<T extends String>{}
Again, according to Wikipedia's article on Generics in Java...
To specify the lower bounding class of a type wildcard, the super keyword is used.
I think whoever originally put this question together needs to sort a few things out.
Some newer languages (such as Java) dispense with header files...
public class MyClass<T super Integer> {} will give a syntax error: > expected
So all together, the question reeks of nonsense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With