JDK 1.5 Compiler not enforcing parameterized type in interface?

Discussion in 'Java' started by googlegroups-nospam4me@sneakemail.com, Aug 24, 2006.

  1. 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
     
    , Aug 24, 2006
    #1
    1. Advertising

  2. wrote:
    > 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.


    As far as the JVM is concerned, both are methods called doSomething that
    take a single Collection argument and return void. The signature is the
    same.

    The advantage of erasure is that if the base class had been updated to
    use generics, the old derived class still works.

    > 2. String can't be added to collection of Integer.


    The static type is Collection, not Collection<Integer>.

    When you compile the code, you should get a warning. Listen to compiler
    warnings.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 24, 2006
    #2
    1. Advertising

  3. Soren Kuula Guest

    Thomas Hawtin wrote:
    > wrote:


    As Thomas said, because of backwards compatibility issues, you will only
    get a warning if you implement with a class that does not use generics.

    If, on the other hand, you do

    >> public class MyClass implements MyInterface {
    >> public void doSomething(Collection <Object> collection) {
    >> collection.add(new String());
    >> }
    >> }

    you should get an error.
    Søren
     
    Soren Kuula, Aug 24, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Babar
    Replies:
    1
    Views:
    439
    Chris Smith
    May 20, 2004
  2. Thomas G. Marshall
    Replies:
    5
    Views:
    812
    Thomas G. Marshall
    Aug 6, 2004
  3. Ulf Meinhardt
    Replies:
    0
    Views:
    6,435
    Ulf Meinhardt
    Aug 10, 2006
  4. Jaggu
    Replies:
    3
    Views:
    996
    Nigel Wade
    Jan 8, 2007
  5. Pep

    JDK 1.5 or JDK 1.6

    Pep, Jul 11, 2007, in forum: Java
    Replies:
    19
    Views:
    1,731
    ~kurt
    Jul 15, 2007
Loading...

Share This Page