Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

JDK 1.5 Compiler not enforcing parameterized type in interface?

Thread view: 
googlegroups-nospam4me@sneakemail.com - 24 Aug 2006 15:20 GMT
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
Thomas Hawtin - 24 Aug 2006 19:41 GMT
> public interface MyInterface {
>     public void doSomething(Collection<Integer> collection);
[quoted text clipped - 11 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Soren Kuula - 24 Aug 2006 20:41 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.