I never did proper class design, so bear with me.
Let’s say we have a Project class. Then let’s say we have a ProjectGroup class that has the same members as the Project class, plus a list of Projects.
Should ProjectGroup be the superclass of Project or is ProjectGroup a specialization of Project and the former should be a subclass of the latter?
I won’t bother you with theory, because you’re probably in a hurry to get a quick answer. So here it goes:
If your two classes are actually implying they should be related by inheritance then
ProjectGroupshould inherit fromProjectclass. This is how it would look like in C#:If they are not, but they use some common class members (that define their state and some functionality over that state), then I’d write an interface and implement it in both classes. C# code again:
Edit
If your classes are actually
ProjectandProjectGroupand they both have properties likeIDandNamein common (for instance), they still shouldn’t be inherited. They just happen to have properties with the same name, but they are basically different entities.They could both either
ICommonEntityinterface – use it when they have the same state+functionality but functionality behaves differently in each of themCommonEntityclass – use it when functionality is completely identical; this way you’ll follow the DRY (don’t repeat yourself) philosophySo your component may be an interface or a class (when using composite pattern).
Direct inheritance between two classes is more suitable where entities imply on being in relation to each other. Like
UserandPersonclasses. They can be inherited either way. Depending on the business scenario.This would be the case where you have an application with contacts. Some of them are also users of this very same application.
This would be a web site where you can register as a user. In case you fill up some personal details as well your user data becomes of type
Person.