Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which type safety would have lost, had Generics supported the sub-typing? [duplicate]

Tags:

java

generics

Consider the snippet:

Number[] numbers = {1, 2.3, 4.5f, 6000000000000000000L}; 

It's perfectly okay to do the above, Number is an abstract class.

Going ahead,

List<Long> listLong = new ArrayList<Long>(); listLong.add(Long.valueOf(10));  List<Number> listNumbers = listLong; // compiler error    - LINE 3  listNumbers.add(Double.valueOf(1.23)); 

Had Line 3 was designed to be compiled successfully, we would end up with a List of Numbers, i.e,

for(Number num: listNumbers ){     System.out.println(num); }  // 10 // 1.23 

which are all numbers.

I came across this in a book,

Generics doesn’t support sub-typing because it will cause issues in achieving type safety. That’s why List<T> is not considered as a subtype of List<S> where S is the super-type of T

Which type safety would have lost in this specific case as discussed above, were the Line 3 was to be compile successfully?

like image 462
Farhan Shirgill Ansari Avatar asked Feb 28 '16 11:02

Farhan Shirgill Ansari


People also ask

Are generics used for type safety?

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.

How do I restrict a generic type in Java?

Java For Testers Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.

Which of the following options is raw type of generic box in Java?

Box rawBox = new Box(); Therefore, Box is the raw type of the generic type Box<T>. However, a non-generic class or interface type is not a raw type. The warning shows that raw types bypass generic type checks, deferring the catch of unsafe code to runtime.

What are generic methods in Java?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.


2 Answers

List<Long> listLong = new ArrayList<Long>(); List<Number> listNumbers = listLong; 

So, listNumbers and listLong would be two references to the same list, if that was possible, right?

listNumbers.add(Double.valueOf(1.23)); 

So, you would be able to add a Double to that list. listLong, of type List<Long>, would thus contain a Double. The type-safety would thus be broken.

like image 113
JB Nizet Avatar answered Sep 21 '22 01:09

JB Nizet


If that was the case, then we could add other different subtypes of Number into listNumbers, which must be forbidden.

Imagine you're now inserting objects of type Double and Long, and later you try to use Long#reverse. Your code will compile but of course will fail at runtime (bad) the first Double it'll come through.

like image 21
Maroun Avatar answered Sep 17 '22 01:09

Maroun