Skip to main content

Change wrapper types inheritance

2 replies [Last post]
Joined: 2004-10-20

I would like to have the ability to cast, say, from Byte to Integer and so on. I would include the next inheritance hierarchy:

java.math.BigInteger extends java.lang.Number
java.lang.Integer extends java.math.BigInteger
java.lang.Short extends java.lang.Integer
java.lang.Byte extends java.lang.Short

java.math.BigDecimal extends java.lang.Number
java.lang.Double extends java.math.BigDecimal
java.lang.Float extends java.lang.Double

If we think OO, this satisfies Liskov substitution principle: an Short is an Integer and so on.

It may sound awkward that an Integer is a BigInteger but, if BigInteger is simply regarded as a GeneralizedInteger, it is seen that java Integer's are always GeneralizedInteger's. In a mathematical fashion, the set of the integers from -2147483648 to 2147483647 is a subset of the integers set (the set "Z" as is called in math).

Possible drawbacks:
1) You upcast a Byte to an Integer and then try to store a too large value --> It would never happen, since wrapper types are immutable.

2) Some wrapper classes could not be final. So the wrapped value should be made final, and so should be the methods that read the value (to avoid subclassing to some extent). Once this is solved, I cannot see any further reason why the class should be kept final.


Basically, the wrapper types mirror the hierarchy that wrapped types have. You can upcast in the same way, and, for fans of auto(un)boxing, operating is more straight (indeed, I am not especially fond of auto(un)boxing).

I have left out Character, because it is not strictly a number, however this would not let it be upcasted to Integer (as a char can be converted con an int). It should also be considered anyway.

Message was edited by: yod

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-10-14

> java.lang.Float extends java.lang.Short

You mean Float extends Double?


Joined: 2004-10-20

Ooops... yes, you're right, that's what I meant.

java.lang.Float extends java.lang.Double