Solved

Why is it possible to cast List<? extends Shape> to be a list of anything at all without any sort of warning?

Posted on 2007-03-31
5
467 Views
Last Modified: 2013-12-15
Why does this compile?

    public class Shape {};
    public class Square extends Shape {};
   
    public void doStuff() {
        List<? extends Shape> shapes = new LinkedList();
        List<Square> squares = (List<Square>)shapes;
        List<Integer> shapeInts = (List<Integer>)shapes;
    }

Obviously, the cast to List<Square> makes sense because it could possibly be true that the list of Shape objects actually contains only Square objects. But why would the List<Integer> compile? Yes, it's not impossible that a list of <? extends Shape> could contain an Integer, but that would only be true if you compiled without generics compile time type checking.

I'm aware that there is no access to generics types at runtime (or at least it's not validated specially), but this seems like the sort of thing that can and should be handled at compile time.

Is there an option in Eclipse that would at least give a warning if the above is attempted?
0
Comment
Question by:HappyEngineer
5 Comments
 
LVL 11

Expert Comment

by:Manish
Comment Utility
in which perspective you are viewing?
Try in java perpective.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> But why would the List<Integer> compile?

The check made at compile time is merely to assure that you are casting the right hand side with the left hand side (to avoid class cast exceptions). However, if you are deliberately casting something wrong on the RHS with something else on the LHS, then Java will proceed with compilation (because it assumes you know what you are casting with - it did its job of warning you with an error when there was no casting as the types were not matching). It will give you a class cast exception later on run time.

For example:

Object o = new Object () ;
String s = o ; // compile error - Java is smart enough to warn you with an error here

Object o = new Object () ;
String s = ( String ) o ;
// Java sees that you are specifying explicitly that the RHS be cast to String which is same as LHS, so it assumes you know what you are deliberately doing

However, when you run it, you wil get a ClassCastException
0
 

Author Comment

by:HappyEngineer
Comment Utility
Yes, I'm viewing it in the java view. Karanw, if you do that and see a warning then let me know and I'll look harder through the list of warnings.

Mayankeagle, in the cast of (String)o, it's possible that's correct because you're casting an object. If I do
  Integer i = null;
  Float f = (Float)i;
then that correctly gives me a compile time exception because that's never valid. This isn't like C++ where you can cast anything to anything.
0
 
LVL 3

Expert Comment

by:cavey_79
Comment Utility
Is Shape by any chance an interface? In that case, you will get less aggressive warning as in the case of normal casts since you could have a subclass of Integer (not really possible since Integer is final) implement shape.

If Shape was an abstract class from a completely different hierarchy than Integer, then it would be obvious you can't have a class subclass both of them. But if any of the two class names are an interface, everything is possible. I agree that they could check for final classes and figure some more stuff out though.

Warning in Eclipse should show up for risky operations, unless you use @SupressWarnings
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 500 total points
Comment Utility
>> gives me a compile time exception because that's never valid

Yes there has to be an inheritance relationship between the 2 classes.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now