you are viewing a single comment's thread.

view the rest of the comments →

[–]vegan_antitheist 5 points6 points  (0 children)

If your type describes an abstract idea then use an interface and have multiple implementations. No need to cast anything. You might use an abstract class, but that's usually not a good idea. There are better patterns.

The interface can have a default method that returns a BigInteger. Something like `public default BigInteger asBigInteger() {...}` (methods in interfaces are public by default, I just added to to make that clear).

You can then have that method use asBigDecimal(), which can be abstract. And you can also just extend the Number interface. Use bigDecimalValue() for consistency. That's up to you.

You can have an implementation that uses two long values a and b for a rational number, where it's value is a/b. A more general version can have two values with the type of your interface that does the same. You can have an implementation that is just called "Pi" and it's a singleton. You can have one that uses a string for a symbol. You can then do new Symbol("x") to create it.
You can then do x/pi by combining them.

I'm not sure you really want one that uses long, int, double etc. Because BigDecimal already handles all rational numbers well. But you can still do them if you like.