Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am developing a range of post filters for a wordpress site, and I have built the first 4 with a single class.

The final two are different enough in scope to only share a single function (the function for producing the final links) in their classes.

Is it reasonable, either in this instance or in any number of similar hypothetical instances, to extend the original class in order to have that functionality, or is there a better way?

share|improve this question
I don't understand this question. What do you mean by building things with a class? Are these classes that inherit from something? Wouldn't the class at the top of the hierarchy already have the common function? – David Thornley Oct 5 '11 at 14:31

No, if the two classes are so different that they only share one function, the chances are they are not interchangable - and therefore they would break the liskov substition principle.

Only derive from a base class if it's logical to do so. Sharing a single piece of code is rarely a justification for inheritence :)

In this particular example it sounds like you want to re-use that function via composition instead...

share|improve this answer
Prefer composition over inheritance! In most cases this will make you life simpler in the long run. – Travis Oct 5 '11 at 14:56
Composition FTW! – back2dos Oct 5 '11 at 15:07
Worth noting my answer is basically the same as @Sardathrion, I just linked to the relevent principles :) – MattDavey Oct 5 '11 at 15:08

If a class description has the word "and" in it, it should be broken down into two (or more) classes. By the looks of things, you could make that function a member of another class that is referenced by the ones you currently have.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.