inheritance in java - simple question - Java

This is a discussion on inheritance in java - simple question - Java ; 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 ...

+ Reply to Thread
Results 1 to 7 of 7

inheritance in java - simple question

  1. Default inheritance in java - simple question

    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 ?



  2. Default Re: inheritance in java - simple question

    "Thomas" <arabel9@o2.pl> 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 { ... }


  3. Default Re: inheritance in java - simple question

    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

  4. Default Re: inheritance in java - simple question

    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

  5. Default Re: inheritance in java - simple question

    Flo 'Irian' Schaetz <iryan@gmx.de> 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


  6. Default Re: inheritance in java - simple question


    Uzytkownik "Patricia Shanahan" <pats@acm.org> napisal w wiadomosci
    news:3xtli.6930$Od7.4456@newsread1.news.pas.earthlink.net...
    > 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.



  7. Default Re: inheritance in java - simple question

    Thomas wrote:
    > Uzytkownik "Patricia Shanahan" <pats@acm.org> napisal w wiadomosci
    > news:3xtli.6930$Od7.4456@newsread1.news.pas.earthlink.net...
    >> 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

+ Reply to Thread

Similar Threads

  1. simple inheritance question
    By Application Development in forum c++
    Replies: 6
    Last Post: 11-28-2007, 03:28 PM
  2. REXML inheritance confusion (simple q?)
    By Application Development in forum RUBY
    Replies: 1
    Last Post: 11-28-2007, 01:22 PM
  3. A simple inheritance question
    By Application Development in forum CSharp
    Replies: 3
    Last Post: 10-25-2007, 12:29 PM
  4. Java Classpath - Simple Question
    By Application Development in forum Java
    Replies: 3
    Last Post: 08-03-2007, 01:56 PM
  5. Simple question about - Inheritance with Constructors in VB.Net
    By Application Development in forum basic.visual
    Replies: 1
    Last Post: 06-02-2004, 03:24 PM