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 ?
Stefan Ram - 12 Jul 2007 18:19 GMT
> 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 { ... }
Flo 'Irian' Schaetz - 12 Jul 2007 18:20 GMT
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
Stefan Ram - 12 Jul 2007 18:32 GMT
>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
Patricia Shanahan - 12 Jul 2007 18:28 GMT
> I have to implement a simple class hierarchy :
>
[quoted text clipped - 6 lines]
> 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
Thomas - 12 Jul 2007 19:49 GMT
> > I have to implement a simple class hierarchy :
> >
[quoted text clipped - 20 lines]
>
> 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.
Patricia Shanahan - 12 Jul 2007 21:39 GMT
>>> I have to implement a simple class hierarchy :
>>>
[quoted text clipped - 29 lines]
> 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