Re: extending enum
- From: Chris Smith <cdsmith@xxxxxxx>
- Date: Sun, 31 Jul 2005 02:04:24 -0600
Tim Tyler <tim@xxxxxxxxxxx> wrote:
> > If you think about it, inheritance goes the wrong direction for adding
> > enum constants. A subclass needs to meet the requirements for a
> > superclass, plus add additional capabilities or constraints. The most
> > important facet of an enum type is that it is limited to a finite set of
> > values. A subtype of an enumeration would need to keep that constraint
> > plus add more... [...]
>
> IMO, that's a curious perspective on what inheritance is about.
>
> In biology, inherintance is a process of making modified derivatives.
>
> In computer science too - as far as I can tell - inherited classes
> can violate existing constraints as well as impose new ones.
That perspective is implicit in seeing API elements as representing
contracts. The client of an object may be unaware whether it has an
instance of a specific subclass or a more general superclass, but it
expects that the API contracts will be fulfilled in either case. The
use of an enum return value, for instance, implies a contract that only
one of a specific, well-known, finite set of values will be included.
Returning some other value is basically violating a postcondition.
Perhaps a different way of explaining would be better.
Generally speaking, casting is required when a conversion may fail
because the value is not compatible with the target type. If an enum B
"extends" enum A in the proposed feature, then it is safe to convert
from A to B. The set of possible values of B is a superset of the
possible values of A, so the A to B conversion is widening and should
not require a cast. Conversely, converting from B to A is narrowing and
should require a cast.
This is backwards from the type relationship normally implied by the
"extends" keyword. If class B extends class A, then the A to B
conversion is narrowing, and the B to A conversion is widening.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
.
- Follow-Ups:
- Re: extending enum
- From: Roedy Green
- Re: extending enum
- From: Tim Tyler
- Re: extending enum
- References:
- extending enum
- From: Roedy Green
- Re: extending enum
- From: Chris Smith
- Re: extending enum
- From: Tim Tyler
- extending enum
- Prev by Date: Cryptographic Service Provider supporting Via's Padlock (AES-encryption)
- Next by Date: Re: new FileWriter hangs
- Previous by thread: Re: extending enum
- Next by thread: Re: extending enum
- Index(es):
Relevant Pages
|