Why Re-implement Interface

Discussion in 'Java' started by Tony, Oct 5, 2007.

  1. Tony

    Tony Guest

    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?
     
    Tony, Oct 5, 2007
    #1
    1. Advertising

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


    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.)
     
    Mike Schilling, Oct 5, 2007
    #2
    1. Advertising

  3. Tony

    Lew Guest

    Mike Schilling wrote:
    > 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>();
    >
    > 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.


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

    --
    Lew
     
    Lew, Oct 5, 2007
    #3
  4. Lew wrote:
    > Mike Schilling wrote:
    >> 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>();
    >>
    >> 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.

    >
    > 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.
     
    Mike Schilling, Oct 5, 2007
    #4
  5. Tony

    Roedy Green Guest

    On Thu, 04 Oct 2007 19:58:20 -0700, Tony <> wrote,
    quoted or indirectly quoted someone who said :

    >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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 5, 2007
    #5
    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. frank

    Implement IList Interface

    frank, Jul 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    569
    frank
    Jul 11, 2003
  2. Jan Schulze
    Replies:
    1
    Views:
    571
    Esmond Pitt
    Mar 26, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    894
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,948
    Smokey Grindel
    Dec 2, 2006
  5. Joe Gottman
    Replies:
    28
    Views:
    3,320
    tennenrishin
    Jul 3, 2011
Loading...

Share This Page