Q1. Explain Type interface
Ans. This feature, known as type inference, allows you to invoke a generic method as you would an ordinary method, without specifying a type between angle brackets.
The complete syntax for invoking this method is:
Box.<Crayon>fillBoxes(red, crayonBoxes);
Here we've explicitly provided the type to be used as U, but more often than not, this can be left out and the compiler will infer the type that's needed:
Box.fillBoxes(red, crayonBoxes); // compiler infers that U is Crayon
Q2. Explain subtyping.
Ans. It is explained by generics. Now consider the following method:
Ans. This feature, known as type inference, allows you to invoke a generic method as you would an ordinary method, without specifying a type between angle brackets.
The complete syntax for invoking this method is:
Box.<Crayon>fillBoxes(red, crayonBoxes);
Here we've explicitly provided the type to be used as U, but more often than not, this can be left out and the compiler will infer the type that's needed:
Box.fillBoxes(red, crayonBoxes); // compiler infers that U is Crayon
Q2. Explain subtyping.
Ans. It is explained by generics. Now consider the following method:
public void boxTest(Box<Number> n) {
// method body omitted
}
Looking at its signature, we can see that it accepts a single argument whose type is Box<Number>. So, we think that it should accept Box<Integer> or Box<Double>. Surprisingly, the answer is "no", because Box <Integer> and Box<Double> are not subtypes of Box<Number>, but Integer and Double are subtypes of Number.
Q3. Can you limit the generic type <T> to few types or Explain bounded types.
Ans. Yes. It can be done using extends keyword.
Ex. using <T extends Number> expects the parameter to be passed of type Number or its sub-class. It can’t accept a String.
To specify additional interfaces to implement, use & character, as shown below:
<U extends Number & MyInterface>
Bounded type parameters limit the kinds of types that can be passed into a type parameter; they can specify an upper bound only.
Q4. Explain use of wildcards in generics
Ans. In contrast to bounded parameters, Wildcards represent unknown types, and they can specify an upper or lower bound.
Keyword extends gives upper bound, saying “an unknown type that is a subtype of Number or Number itself" and keyword super gives a lower bound, saying “an unknown type that is super-type of Number or is Number itself”.
Ex. <Box<? extends Number>
Box<Integer> and Box<Double> are not subtypes of Box<Number>, they are in fact subtypes of “<Box<? extends Number>”. Here ? is a wildcard character.
Ex. <? super Number> -> Class must be a super class of Number or Number itself. Thus, it can be Number or Object.
Q5. Explain type erasure.
Ans. During compilation, type erasure removes all generic information from a generic class or interface, leaving behind only its raw type. It is possible for generic code and legacy code to interact, but in many cases the compiler will emit a warning telling you to recompile with special flags for more details.
java.sun.com/docs/books/tutorial/java/generics/erasure.html
// method body omitted
}
Looking at its signature, we can see that it accepts a single argument whose type is Box<Number>. So, we think that it should accept Box<Integer> or Box<Double>. Surprisingly, the answer is "no", because Box <Integer> and Box<Double> are not subtypes of Box<Number>, but Integer and Double are subtypes of Number.
Q3. Can you limit the generic type <T> to few types or Explain bounded types.
Ans. Yes. It can be done using extends keyword.
Ex. using <T extends Number> expects the parameter to be passed of type Number or its sub-class. It can’t accept a String.
To specify additional interfaces to implement, use & character, as shown below:
<U extends Number & MyInterface>
Bounded type parameters limit the kinds of types that can be passed into a type parameter; they can specify an upper bound only.
Q4. Explain use of wildcards in generics
Ans. In contrast to bounded parameters, Wildcards represent unknown types, and they can specify an upper or lower bound.
Keyword extends gives upper bound, saying “an unknown type that is a subtype of Number or Number itself" and keyword super gives a lower bound, saying “an unknown type that is super-type of Number or is Number itself”.
Ex. <Box<? extends Number>
Box<Integer> and Box<Double> are not subtypes of Box<Number>, they are in fact subtypes of “<Box<? extends Number>”. Here ? is a wildcard character.
Ex. <? super Number> -> Class must be a super class of Number or Number itself. Thus, it can be Number or Object.
Q5. Explain type erasure.
Ans. During compilation, type erasure removes all generic information from a generic class or interface, leaving behind only its raw type. It is possible for generic code and legacy code to interact, but in many cases the compiler will emit a warning telling you to recompile with special flags for more details.
java.sun.com/docs/books/tutorial/java/generics/erasure.html
No comments:
Post a Comment