inheritance in java - simple question

T

Thomas

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 ?
 
S

Stefan Ram

Thomas said:
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 { ... }
 
F

Flo 'Irian' Schaetz

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
 
P

Patricia Shanahan

Thomas said:
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
 
S

Stefan Ram

Flo 'Irian' Schaetz said:
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
 
T

Thomas

Uzytkownik "Patricia Shanahan said:
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.
 
P

Patricia Shanahan

Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top