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
472 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
ID: 18831299
in which perspective you are viewing?
Try in java perpective.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18831330
>> 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
ID: 18834192
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
ID: 18837067
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:
Mayank S earned 500 total points
ID: 19046581
>> 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

778 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