Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
474 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 20
ejb mdb examples 1 8
jmss example java 2 23
Tagging and Merging on Branch 1 29
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

839 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