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 / October 2007

Tip: Looking for answers? Try searching our database.

Why Re-implement Interface

Thread view: 
Tony - 05 Oct 2007 03:58 GMT
In Java API, we see such code:
public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

My question is, why Sub-class still declare to re-implement the same
inteface, since Super-class already implements it?
Mike Schilling - 05 Oct 2007 04:25 GMT
> In Java API, we see such code:
> public class ArrayList
[quoted text clipped - 3 lines]
> My question is, why Sub-class still declare to re-implement the same
> inteface, since Super-class already implements it?

It's useful as documentation.

In fact, in my ideal version of Java, it would be possible to declare that
the fact that ArrayList inherits from AbstractList is an implementation
detail invisible to its clients.   That is, while it's good for client code
to contain

   List<String> l = new ArrayList<String>();

it should never contain

   AbstractList<String>l = new ArrayList<String>();

The maintainer should be completely free to re-implement ArrayList in some
other fashion, if he feels like it, with no fear of breaking client code.
Currently, that's not the case.  (Strictly speaking, this would probably
also require making ArrayList final, since subclasses are often sensitive to
implementation issues.  See http://en.wikipedia.org/wiki/Fragile_base_class 
for a brief description of this problem.)
Lew - 05 Oct 2007 04:34 GMT
> In fact, in my ideal version of Java, it would be possible to declare that
> the fact that ArrayList inherits from AbstractList is an implementation
> detail invisible to its clients.   That is, while it's good for client code
> to contain

This is not a Java issue.  It's an API issue.

>     List<String> l = new ArrayList<String>();
>
[quoted text clipped - 5 lines]
> other fashion, if he feels like it, with no fear of breaking client code.
> Currently, that's not the case.  

Apparently the API designers didn't want that, so they implemented ArrayList
as a subclass of AbstractList.

Signature

Lew

Mike Schilling - 05 Oct 2007 05:39 GMT
>> In fact, in my ideal version of Java, it would be possible to
>> declare that the fact that ArrayList inherits from AbstractList is
[quoted text clipped - 15 lines]
> Apparently the API designers didn't want that, so they implemented
> ArrayList as a subclass of AbstractList.

My point is that in Java there's no way to inherit from a class without
making that fact as visible as the class is.
Roedy Green - 05 Oct 2007 06:27 GMT
>My question is, why Sub-class still declare to re-implement the same
>inteface, since Super-class already implements it?

I do that just to make it clear my new class can be used wherever that
interface is required. It makes it very clear. You don't have to chase
the inheritance tree to discover that fact.  It also makes sure nobody
"unimplements" the interface on the base class without me finding out
next time I compile.

In other words I consider implementing that interface an inherent part
of my class's abilities, not something it inherits
accidentally/incidentally.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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



©2008 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.