AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Subclassing EnumSet to add an interface?

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Language Programming
View previous topic :: View next topic  
Author Message
Eric Smith
Guest





PostPosted: Sat May 12, 2007 7:12 am    Post subject: Subclassing EnumSet to add an interface? Reply with quote



I'd like to create a subclass of EnumSet to implement the Comparable
interface (using my own arbitrary ordering, so that I can use the
subclass as a key in a dictionary), but I can't seem to figure
out how to do it.

I tried:

import java.util.EnumSet;

public abstract class Foo<E extends Enum<E>> extends EnumSet<E>
implements Comparable<Foo>
{
public int compareTo (Foo o)
{
return 1; // dummy value for now
}
}


The compiler says:

Foo.java:3: cannot find symbol
symbol : constructor EnumSet()
location: class java.util.EnumSet<E>
public abstract class Foo<E extends Enum<E>> extends EnumSet<E>
^
1 error


I don't understand why it thinks there should be an EnumSet()
constructor, since I'm subclassing it as an abstract class.
Any hints or suggestions?

Thanks!
Eric
Back to top
Eric Smith
Guest





PostPosted: Sun May 13, 2007 12:33 am    Post subject: Re: Subclassing EnumSet to add an interface? Reply with quote



Lew <lew (AT) nospam (DOT) lewscanon.com> writes:
Quote:
As Joshua Bloch advised in /Effective Java/, "prefer composition to inheritance."

Even when you only want to add one simple method?
Back to top
Eric Smith
Guest





PostPosted: Sun May 13, 2007 12:54 am    Post subject: Re: Subclassing EnumSet to add an interface? Reply with quote



I wrote:
Quote:
I never cease to be amazed at how often the standard Java
classes do 95% of what I want, but *cannot* be coerced into
letting me implement that last 5%.

Tom Hawtin wrote:
Quote:
You just have to use it correctly.

I originally learned object-oriented programming in Smalltalk.
Perhaps Smalltalk taught me to do things incorrectly, though
at the time I didn't seem to have trouble with it.
Back to top
Eric Smith
Guest





PostPosted: Mon May 14, 2007 4:15 am    Post subject: Re: Subclassing EnumSet to add an interface? Reply with quote

Lew <lew (AT) nospam (DOT) lewscanon.com> writes:
Quote:
If your analysis says "B /is-a/n A",
then the modeled B inherits from the modeled A. If it doesn't, then B
does not inherit from A. Simple. Number of methods not a factor.

The analysis did say "B is an A". Specifically, it said "B is an A
that also does one extra thing."

Quote:
Correctness is not simply a matter of counting. Think carefully about
your model.

I have thought carefully about it. Condescenion is not helpful.
Back to top
Eric Smith
Guest





PostPosted: Mon May 14, 2007 4:19 am    Post subject: Re: Subclassing EnumSet to add an interface? Reply with quote

Lew wrote:
Quote:
Apples and oranges. Completely misses the point.

The comment was about using the Java classes correctly, not about
using "object-oriented programming" correctly.

So what I've learned from you in this thread is:

1) I should use subclassing only when "A is a B" (as it was in my example)
2) I'm using Java wrong

You haven't explained how to reconcile those two points, given
that my complaint was in fact about Java not letting me subclass
a provided cass.
Back to top
Eric Smith
Guest





PostPosted: Mon May 14, 2007 4:29 am    Post subject: Re: Subclassing EnumSet to add an interface? Reply with quote

Lew wrote:
Quote:
The decision isn't based on whether you're only going "to add one
simple method". First off, the complexity of the method is completely
immaterial. The decision is based on your object model. Your object
model is based on your analysis. If your analysis says "B /is-a/n A",
then the modeled B inherits from the modeled A. If it doesn't, then B
does not inherit from A. Simple. Number of methods not a factor.

If A has a bunch of methods, and you need a B that has those methods
plus one more, there is a high probability that "B is an A".
Number of methods may not directly be a factor, but it's also not
completely irrelevant.
Back to top
Tom Hawtin
Guest





PostPosted: Mon May 14, 2007 6:14 am    Post subject: Re: Subclassing EnumSet to add an interface? Reply with quote

Eric Smith wrote:
Quote:

1) I should use subclassing only when "A is a B" (as it was in my example)

Your analysis was poor. Does Foo need to be an EnumSet? Given that an
instance of EnumSet does practically nothing that an AbstractSet does,
it appears not. Then there is Comparable. Must that be implemented by
the same class as that which contains the Set? It seems they can easily
be kept separate (and therefore probably should).

Quote:
2) I'm using Java wrong

Clearly.

Tom Hawtin
Back to top
Eric Smith
Guest





PostPosted: Thu May 17, 2007 7:11 am    Post subject: Re: Subclassing EnumSet to add an interface? Reply with quote

Tom Hawtin wrote:
Quote:
Your analysis was poor. Does Foo need to be an EnumSet? Given that an
instance of EnumSet does practically nothing that an AbstractSet does,
it appears not.

I wrote:
Quote:
How did you determine that?

Tom Hawtin wrote:
Quote:
I read the API docs.

You're confusing analysis of requirements with implementation.
I determine what object behavior I required; the API docs were
neither necessary nor sufficient to do so.

Whether my requirements can be met by an implmentation using
EnumSet is a different matter, and for reasons that have been
explained in this thread, they cannot.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Language Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.