Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got the following exception in my code: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Comparable; cannot be cast to [LElement; at the following call:

Element<K,V>[] heap = (Element<K,V>[]) new Comparable[size]; 

where Element is defined as follows:

class Element<K, V> implements Comparable<Element<K, V>>{
    long timeStamp;
    K key;
    V val;
    @Override
    public int compareTo(Element<K, V> o) {
    return new Long(timeStamp).compareTo(o.timeStamp);
}
Element(long ts, K key, V val){
    this.timeStamp = ts;
    this.key = key;
    this.val = val;
    }

}

any help is greatly appreciated!

share|improve this question
    
Can you show where are you using that code. As such, that code doesn't make sense. The compiler should already be giving you an Unchecked Cast warning. Still, we need some surrounding code. –  Rohit Jain Aug 17 '13 at 18:59
    
I am trying to create an array of Element<K, V>. I know ArrayList works, but here I need something as simple as Element<K, V>[], which is not directly supported by Java. I also know sth like the following is allowed: heap = (K[]) new Comparable[capacity]; –  user2692465 Aug 17 '13 at 19:31
1  
Have you tried Element<K, V>[] heap = (Element<K, V>[]) new Element[size];? –  sbat Aug 17 '13 at 20:06
    
This works! thank you very much! –  user2692465 Aug 17 '13 at 20:12

2 Answers 2

That's not how polymorphism works. You cannot refer to a superclass (or interface) "object" through a subclass reference. You can, however, refer to any subclass object through the name of its implementing interface or any superclass.

Comparable c = new Element();

Or in general, you can remember that this is always legal:

Object c = new String();

But this is never OK:

AnyClass m = new Object();
share|improve this answer
    
I understand this. However, I was trying to cast the reference to a comparable/object array to some a subclass. I thought this is a typical trick people use to create an object/comparable array using generics E[] arr = E[] new Object[size]; E[] carr = E[] new Comparable[size]; –  user2692465 Aug 17 '13 at 19:39
    
@user2692465 In this case, the erasure of Element<K, V> is Element, which means that you could use new Element[size] (and get an unchecked cast warning). –  Jonathan Callen Aug 17 '13 at 22:32

Arrays cannot be cast the same polymorphic way that classes can. Consider this code:

Comparable[] foo = new Comparable[size];
foo[0] = Long.valueOf(123L);
Element<K,V>[] heap = (Element<K,V>[]) foo;

Element<K,V> thisFails = heap[0];    // this isn't safe!

Naturally, this code doesn't make sense; you would be putting a Long into your heap of Elements, and that's just not right. The counterintuitive thing there is that the reverse doesn't work either:

Element<K,V>[] heap = new Element<K,V>[];
Comparable[] foo = (Comparable[]) heap;
foo[0] = Long.valueOf(123L);
// ...which also sets heap[0], because they're two references to the same
// array object. Unlike C-style languages, arrays are objects in Java.

Element<K,V> thisFails = heap[0];    // this isn't safe!

The consequence of this is that arrays can't be cast in either direction. (Generics can, but with specific and arcane rules about extends and super; that's another matter.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.