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