G
Guest
I'm starting to use the features of 1.5 and wondering why the following
isn't enforced by the compiler: I haven't been able to find an answer,
or alternative way of doing it:
I'm jusing JDK 1.5.0_7
If I declare a that a collection is of type Integer in an interface
such as:
public interface MyInterface {
public void doSomething(Collection<Integer> collection);
}
Why doesn't it complain if it is excluded from the implementing class?
public class MyClass implements MyInterface {
public void doSomething(Collection collection) {
collection.add(new String());
}
}
I would expect one of 2 compiler errors:
1. MyClass doesn't implement the interface correctly since the method
signatures are different.
2. String can't be added to collection of Integer.
I suspect it has to do with the rewrite from 'erasure' to remove
generics,
but is this a compiler bug, or should I be doing it a different way?
Thanks,
-CEC
isn't enforced by the compiler: I haven't been able to find an answer,
or alternative way of doing it:
I'm jusing JDK 1.5.0_7
If I declare a that a collection is of type Integer in an interface
such as:
public interface MyInterface {
public void doSomething(Collection<Integer> collection);
}
Why doesn't it complain if it is excluded from the implementing class?
public class MyClass implements MyInterface {
public void doSomething(Collection collection) {
collection.add(new String());
}
}
I would expect one of 2 compiler errors:
1. MyClass doesn't implement the interface correctly since the method
signatures are different.
2. String can't be added to collection of Integer.
I suspect it has to do with the rewrite from 'erasure' to remove
generics,
but is this a compiler bug, or should I be doing it a different way?
Thanks,
-CEC