aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Inheritance or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Inheritance or not" Watch "Inheritance or not" New topic
Author

Inheritance or not

Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
Hi!
I'm doing a quiz program that asks questions to a user in the command prompt. I have 2 files, one containing the question, the other the answers. The question subset is taken randomly in the file while the answer file is there to check up with user answer. Now, the 2 txt file share a common implementation to ease the code part, like amrkers to indicate which question etc... Now it was all fine until i added this extra feature: the possibility to delete a question in the question file and answer file. To do that i need to shift by minus one the question numbers that go after the question to delete (to keep my random subset of questions working). So i did it and both classes (AnswerFile class and QuestionFile class) share a lot in common now, mainly all because of the new deleteQuestion() method and all the helper methods.
So I did the following: I made a new class called File.java and put the deleteQuestion(), loadFile() and all the other helper methods like reoderFile() etc... in the File.java and I made it be the super class. Now I've been wondering if it's the good thing to do, because although now my code is not duplicate, I feel the File.java class is doing all the job really as i needed not precise the deleteQuestion methodwhich I renamed deleteEntry(). Now deleteEntry() is called by the subclasses AnswerFile and QuestionFile. Hmm, so, should I inherit or do you think my code should be in the classes twice? Or again all what I said isn t clear and needs rephrasing ?


Si altas son las torres, el valor es alto - Alberti
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
As you question seems to be concerned with program design, I'm moving this to the OO, UML, Patterns and Refactoring forum, where they just love to talk about this stuff...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Daniel Curtmil Atrei:
So I did the following: I made a new class called File.java and put the deleteQuestion(), loadFile() and all the other helper methods like reoderFile() etc... in the File.java and I made it be the super class. Now I've been wondering if it's the good thing to do, because although now my code is not duplicate, I feel the File.java class is doing all the job really as i needed not precise the deleteQuestion methodwhich I renamed deleteEntry(). Now deleteEntry() is called by the subclasses AnswerFile and QuestionFile. Hmm, so, should I inherit or do you think my code should be in the classes twice? Or again all what I said isn t clear and needs rephrasing ?

As sure as fate, duplicating the code in two classes would be bad. It would violate the Single Choice Principle: If you want to change something, you should only have to change it at exactly one place. Having it to change at more then one place will not only lead to more work, but is also very likely to lead to inconsistencies - bugs not being fixed at all the necessary places etc.
So, extracting the similarities into one place was a good decision. There are alternatives to using inheritance for this, like delegation, and inheritance is the one which provides the strongest coupling. It's hard to tell wether inheritance is the best choice in this case without seeing some code, but I guess it will be ok (and you can refactor to a less coupled solution once the need becomes apparent).


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance or not
 
Similar Threads
Recursive problem - I need an idea.
Private Inner Class - TIJ
REWARD : pass a2a parameter, involves using getCodeBase()
Test 488 questions
Program design--to inner-class or not to inner-class?