Are there any guidelines someone could give me for sub-classing a Class that I don't have the source to... an opaque class. For example: I want to subclass the DecimalFormat Class - I dont know much about it's methods and internal workings. It has 3 const'r & about 30 methods, then it's parent class NumberFormat... It also interacts with the classes FieldPosition, ParsPosition & StringBuffer in ways that may appear obvious - but I don't know the exact rules - and this is what wories me. For my subclass to be well conceived and a 'good' class it needs to follow the same "rules" on the interaction (side-effects) with these classes it interacts with. Since there are no formal documented interactions between these classes - I'll be guessing - or is there a way to infer the knowledge I need? Maybe Sun/Javadoc could come up with a way to formalize what a 'extender' of a class would need to know - just dreaming. But are there any generalization one could make? Thanks, David
The interactions with a few known other classes just help illustrate how the methods are used. The real contract you must not break is the Liskov Substitution rule, that ANY class that works with the original must work with your class. If we had a full set of unit tests for the original, this would be a lot easier, but we don't, unless you want to make some What kind of extensions do you have in mind? If it's only new methods you are probably pretty safe. If it's overriding some methods to gives different answers, you may be in big trouble. A textbook example of potential trouble: In geometry, a square IS a rectangle. So in a drawing program you might be tempted to make Square a sublcass of Rectangle. The new rule for square is the sides are all the same length, so override setWidth() to also set height, and setHeight to also set Width. Is life good? According to Liskov, you should be able to pass a Square to the unit tests for Rectangle. How about:
And what do you do with the constructor new Rectangle( width, height )? Sometimes extending is just not the right answer.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James: The interactions with a few known other classes just help illustrate how the methods are used. The real contract you must not break is the Liskov Substitution rule, that ANY class that works with the original must work with your class. If we had a full set of unit tests for the original, this would be a lot easier, but we don't, unless you want to make some
That's actually what I would do if I cared: Write JUnit tests for the base class and then see that my derived class is passing them, too!
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Oct 08, 2003
Thanks for the input... maybe an example will help. Here the code for my extended DecimalFormat class. The Overpunch format is used in the health care industry EDI - looks like some old cobol programmers designed the spec... From the context of OO design - I'm thinking that it would be nice if the JavaDocs had some 'magic' way of creating a section that would lend a hand to the 'extender' of a class. It's taken me quite a while to extend DecimalFormat and I've yet to figure out if I've done a good job. Yea, one day real soon now I'm going to write those JUnit test...
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com