• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance or not

 
Dani Atrei
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic