Should two classes that depend on each other but have different functionality have a common superclass?

Tag: oop , design , inheritance Author: huangshuangnan Date: 2012-07-06

I have a class A that depends on class B. They have very different functions but share similar methods. I though of splitting these classes and subclassing them from a common superclass, but they do not relate.

Should I subclass them from a common superclass, or should I create another class that contains components of the class A and class B?

Hard to tell, because it's too broad question without details ... Both ways are doable and it depends what do you mean with "similar methods" and what they do. But anyway, do it in a way which suits your needs. There's no one perfect answer to question like this one.
Is class A a more specific type of B, does A implement features of B, or does A just use B's methods? It's unclear what the relationship between A and B is specifically in order to tell you how you should structure them.
Class A is a CCLayer that adds some sprite from a plist. Class B is a CCLayer that add some additional sprite depending on Class A.
The method I'm debating around is: loadSpriteFromPlist and loadNextSprite. The former load the NSString from a plist and loadNextString call loadSpritePlist for the next sprite. Both Class A and Class B have this, but they don't really relate except for the fact that Class B depends more on class A for some information.
@EmbodiedDarkness Does class B need to know internal state of class A? If so B should inherit A. Otherwise you should probably move the shared functionality implementation details outside of both A and B and call it remotely. Though, depending on the details of your classes other approaches may be better -- it's hard to tell based on your descriptions

Other Answer1

Very broad scenario. From what I believe, if two completely unrelated classes have some operations in common, that totally represents some package-level or global utility operations.

You might consider extracting those operations into a utility class or regular class depending on the specific scenario.