inheritance in java - simple question

Discussion in 'Java' started by Thomas, Jul 12, 2007.

  1. Thomas

    Thomas Guest

    I have to implement a simple class hierarchy :


    List (two directions ;))
    / \
    / \
    Queue Stack

    The thing is List has more methods than Q and S. Should I make the methods,
    which aren't common private or use the interface mechanism ? Are there other
    ways two do that model inheritence in java ?
     
    Thomas, Jul 12, 2007
    #1
    1. Advertising

  2. Thomas

    Stefan Ram Guest

    "Thomas" <> writes:
    > List (two directions ;))
    > / \
    > / \
    > Queue Stack
    >The thing is List has more methods than Q and S.


    »Q« and »S« were not mentioned before.

    interface Stack { ... }
    interface Queue { ... }
    interface List extends Stack, Queue { ... }

    class ArrayList implements List { ... }
    class ArrayStack implements Stack { ... }
    class ArrayQueue implements Queue { ... }
     
    Stefan Ram, Jul 12, 2007
    #2
    1. Advertising

  3. And thus spoke Thomas...

    > The thing is List has more methods than Q and S. Should I make the methods,
    > which aren't common private or use the interface mechanism ? Are there other
    > ways two do that model inheritence in java ?


    Why is that so? What methods do you think List should have? Are we
    talking about the java.util.list interface here or do you want to
    implement something different?

    Flo
     
    Flo 'Irian' Schaetz, Jul 12, 2007
    #3
  4. Thomas wrote:
    > I have to implement a simple class hierarchy :
    >
    >
    > List (two directions ;))
    > / \
    > / \
    > Queue Stack
    >
    > The thing is List has more methods than Q and S. Should I make the methods,
    > which aren't common private or use the interface mechanism ? Are there other
    > ways two do that model inheritence in java ?
    >
    >


    You cannot reduce an inherited method from public to private, because
    that breaks the superclass contract.

    Why extend List at all?

    You could write a class Queue that has exactly the methods you think a
    queue should have, no more and no less. Have a List reference as an
    instance variable and implement the Queue methods by manipulating your List.

    Similarly, a Stack implementation could use a List reference to do most
    of the work, without extending List.

    Patricia
     
    Patricia Shanahan, Jul 12, 2007
    #4
  5. Thomas

    Stefan Ram Guest

    Flo 'Irian' Schaetz <> writes:
    >Are we talking about the java.util.list interface here


    public class Main
    { public static void main( final java.lang.String[] args )
    { java.lang.System.out.println( java.util.list.class ); }}

    javac Main.java
    Main.java:3: cannot find symbol
    symbol : class list
    location: package java.util
    { java.lang.System.out.println( java.util.list.class ); }}
    ^
    1 error
     
    Stefan Ram, Jul 12, 2007
    #5
  6. Thomas

    Thomas Guest

    Uzytkownik "Patricia Shanahan" <> napisal w wiadomosci
    news:3xtli.6930$...
    > Thomas wrote:
    > > I have to implement a simple class hierarchy :
    > >
    > >
    > > List (two directions ;))
    > > / \
    > > / \
    > > Queue Stack
    > >
    > > The thing is List has more methods than Q and S. Should I make the

    methods,
    > > which aren't common private or use the interface mechanism ? Are there

    other
    > > ways two do that model inheritence in java ?
    > >
    > >

    >
    > You cannot reduce an inherited method from public to private, because
    > that breaks the superclass contract.
    >
    > Why extend List at all?
    >
    > You could write a class Queue that has exactly the methods you think a
    > queue should have, no more and no less. Have a List reference as an
    > instance variable and implement the Queue methods by manipulating your

    List.
    >
    > Similarly, a Stack implementation could use a List reference to do most
    > of the work, without extending List.
    >
    > Patricia



    ok thx Yes, this might be a solution, but the constraints about the
    hierarchy I gave are still for that lab (I; m a student :). As i remember
    in c++ i had had to write the same. Then i just extended the classes as
    above, override the useless methods to empty method {} and C&P the others
    needed (since i dont have the code, i dont rembember I did the last one).
    Just wondering isn't any more elegant solution to do this.
     
    Thomas, Jul 12, 2007
    #6
  7. Thomas wrote:
    > Uzytkownik "Patricia Shanahan" <> napisal w wiadomosci
    > news:3xtli.6930$...
    >> Thomas wrote:
    >>> I have to implement a simple class hierarchy :
    >>>
    >>>
    >>> List (two directions ;))
    >>> / \
    >>> / \
    >>> Queue Stack
    >>>
    >>> The thing is List has more methods than Q and S. Should I make the

    > methods,
    >>> which aren't common private or use the interface mechanism ? Are there

    > other
    >>> ways two do that model inheritence in java ?
    >>>
    >>>

    >> You cannot reduce an inherited method from public to private, because
    >> that breaks the superclass contract.
    >>
    >> Why extend List at all?
    >>
    >> You could write a class Queue that has exactly the methods you think a
    >> queue should have, no more and no less. Have a List reference as an
    >> instance variable and implement the Queue methods by manipulating your

    > List.
    >> Similarly, a Stack implementation could use a List reference to do most
    >> of the work, without extending List.
    >>
    >> Patricia

    >
    >
    > ok thx Yes, this might be a solution, but the constraints about the
    > hierarchy I gave are still for that lab (I; m a student :). As i remember
    > in c++ i had had to write the same. Then i just extended the classes as
    > above, override the useless methods to empty method {} and C&P the others
    > needed (since i dont have the code, i dont rembember I did the last one).
    > Just wondering isn't any more elegant solution to do this.
    >
    >


    OK - you have to conform to lab exercise requirements.

    A couple of comments:

    There is a specific exception, java.lang.UnsupportedOperationException,
    that is thrown e.g. by java.util collection classes to indicate that an
    operation is defined in a superclass or interface, but not supported in
    this particular implementation. I would throw that from any method that
    you are implementing only because of the badly designed inheritance
    hierarchy.

    There should be no need to copy and paste - any method that is declared
    public in List will be available in both Stack and Queue.

    Patricia
     
    Patricia Shanahan, Jul 12, 2007
    #7
    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. maxw_cc
    Replies:
    1
    Views:
    3,197
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    853
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,067
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,956
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    794
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page