Naming conventions....

I need to extend java.math.BigInteger and would like to call my version BigInteger (mainly because within the app I am developing I will be using my version as opposed to Sun's).Is this a good idea/bad idea? Why? How would you do it?
[248 byte] By [_bensmytha] at [2007-10-2 4:37:10]
# 1

Bad idea, generally, to extend Sun classes. Better to create your own class that takes either: a String, a long or BigInteger in its constructor. Have the actual BigInteger be an instance variable in your custom class that is initialized in the constructor. Finally, add public methods and delegate the calls you need to the nested BigInteger.

- Saish

Saisha at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...
# 2
This seems like considerable effort as opposed to extending?
_bensmytha at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...
# 3
Not really. A precept of "Effective Java" (Bloch, et. al.) is to prefer composition over inheritance whenever possible. What behavior are you adding to BigInteger that is not already there?- Saish
Saisha at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...
# 4

Bruce Eckel, author of [url=http://www.mindview.net/Books/TIJ/]Thinking In Java[/url], has this to say about composition vs. inheritance:

When deciding between inheritance and composition, ask if you need to upcast to the base type. If not, prefer composition (member objects) to inheritance. This can eliminate the perceived need for multiple base types. If you inherit, users will think they are supposed to upcast.

Choose composition first when creating new classes from existing classes. You should only used inheritance if it is required by your design. If you use inheritance where composition will work, your designs will become needlessly complicated.

Bill Venners: [url=http://www.artima.com/designtechniques/compoinh.html]Composition versus Inheritance[/url]

Use inheritance (of implementation) only when the class satisfies the following criteria:

1) "Is a special kind of," not "is a role played by a";

2) Never needs to transmute to be an object in some other class;

3) Extends rather than overrides or nullifies superclass;

4) Does not subclass what is merely a utility class (useful functionality you'd like to reuse); and

5) Within PD: expresses special kinds of roles, transactions, or things.

-- from [url=http://www.amazon.com/exec/obidos/tg/detail/-/0139111816/qid=1094662332/sr=1-5/ref=sr_1_5/002-7366647-8594415?v=glance&s=books]Java Design: Building Better Apps and Applets (2nd Edition)[/url], by Peter Coad and Mark Mayfield

yawmarka at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...
# 5
> Bruce Eckel, author of> [url= http://www.mindview.net/Books/TIJ/]Thinking In> Java[/url], has this to say about composition vs.> inheritance:> yes and if you still want to use inheritance refer to Liskov's principle
kilyasa at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...
# 6

> What behavior are you adding to

> BigInteger that is not already there?

BigInteger probableSafePrime(int bitLength, int certainty, Random rnd)

BigInteger generateQuadraticResidue(BigInteger n, SecureRandom rnd)

BigInteger generateQuadraticResidueGenerator(BigInteger n, BigInteger order, BigInteger[] primeFactor, SecureRandom rnd)

/** badly named.... Return a BigInteger[] representing the n high order bits of x and the n low order bits of x.*/

BigInteger[] split(BigInteger x, int n)

At the moment I have defined these methods as static in a class called MathUtil.

_bensmytha at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...
# 7
IMO, that is where they belong. - Saish
Saisha at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...
# 8
As supporting evidence, look at how java.lang.Math is structured.- Saish
Saisha at 2007-7-16 0:09:50 > top of Java-index,Java Essentials,New To Java...