Subclasses should be specializations

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m
Line 1: Line 1:
Johnson and Foote defined that subclass should be considered as a specialized entity of the superclass. Hence subclass does not redefine any of the method which is already defined in the superclass but only adding new methods.
+
Johnson and Foote defined that subclass should be considered as a specialized entity of the superclass. Hence subclasses should not redefine any of method which is already defined in the superclass.  Subclasses should only add new methods.
  
However, the author also noticed there are some cases which might be good to be an exception from the rule defined above. One example is the case of an abstract superclass. In such a case, subclass have also to fill in the method which required by the superclass. Secondly, they suggests some times subclass doesn’t need to be a specialization of the superclass. Subclass can be used as a way for fixing improper design in legacy code by adding subclass to the base class. As a reault, subclass could be more general than its superclass, or may not have much relationship to the super class but just for the purpose of code reuse. A further reorganization of the class hierarchy is necessary if we fall into this kind of situation.
+
However, the authors also recognized that there may be some exceptions to the rule defined above. One example is the case of an abstract superclass. In such a case, subclasses must implement all methods declared in the abstract superclass. Secondly, they suggest there may be some cases where a subclass may not be a specialization of the super class. For example, a subclass may be used to wrap the super class, possibly to encapsulate legacy code. In this case, the subclass may be more general than its superclass, or may have very little relationship with the super class.  This would appear to be [[Inheritance for implementation]], where inheritance is being used purely for the purpose of code reuse. A further reorganization of the class hierarchy is necessary if we fall into this kind of situation.
  
 
Riel's heuristic [[Inheritance for specialization]] is derived from this idea.
 
Riel's heuristic [[Inheritance for specialization]] is derived from this idea.

Revision as of 09:20, 8 August 2009

Johnson and Foote defined that subclass should be considered as a specialized entity of the superclass. Hence subclasses should not redefine any of method which is already defined in the superclass. Subclasses should only add new methods.

However, the authors also recognized that there may be some exceptions to the rule defined above. One example is the case of an abstract superclass. In such a case, subclasses must implement all methods declared in the abstract superclass. Secondly, they suggest there may be some cases where a subclass may not be a specialization of the super class. For example, a subclass may be used to wrap the super class, possibly to encapsulate legacy code. In this case, the subclass may be more general than its superclass, or may have very little relationship with the super class. This would appear to be Inheritance for implementation, where inheritance is being used purely for the purpose of code reuse. A further reorganization of the class hierarchy is necessary if we fall into this kind of situation.

Riel's heuristic Inheritance for specialization is derived from this idea.

See also

Personal tools