P
Patricia Shanahan
Mark said:A good point. Do you think it would be better to derive
AbstractNumber directly from Object?
I don't think it should be done with "extends" at all. I believe base
class extension was seriously over-used in the early days of Java, and
should be limited to situations in which there is inheritance of
implementation.
However, I am curious about whether it *can* be done using extends. The
relationship to Number is tricky, because Number has a strong flavor of
real number subset. Some, but not all, of the arithmetic classes do
represent subsets of the reals, and so should extend Number.
However, here's a guess how doubleValue() might be implemented for a
matrix:
All of the methods in java.lang.Number are identities. The
doubleValue() of a Byte with a value of 1 is still 1, insofar as a
double is able to represent the value of 1.
Double can represent 1 exactly. But it does not work the other way
round. The byteValue() of new Integer(1000) has very little connection
to 1000, and will not convert back to it.
> To me, this means that an Matrix which is implemented with, say,
> floats, should return a Matrix implemented with doubles when
> doubleValue() is called on it. Not all of these conversions make
> sense to me. I don't see the point of a Matrix which is created with
> byteValue(), for example.
The doubleValue() method in class Number returns a double, so any
concrete class that extends it, directly or indirectly, must declare or
inherit a public doubleValue() method that returns a double.
On the other hand, if AbstractNumber does not extend Number, there is no
reason for it to have a doubleValue method at all. Why couple arithmetic
operations with an arbitrary set of conversions?
Patricia