Section9.6Step 0: Understand & Restate the Problem with Inheritance in Mind
When applying Step 0 (Understand & Restate), you typically paraphrase the problem, clarify inputs, outputs, and constraints, and document assumptions and edge cases. When planning a class hierarchy, this step should explicitly highlight overlapping or repeated requirements that suggest a superclass or an inheritance relationship.
"We need to implement a social media application that allows users to create different types of posts: text-only posts, photo posts with captions, and video posts with descriptions. All posts need to track the author, timestamp, number of likes, and comments. Users should be able to like any post type and add comments to any post. Additionally, photo and video posts need to track media size and resolution."
"We need classes to represent various post types in our social media application: TextPost, PhotoPost, and VideoPost. All posts share common attributes and behaviors: each has an author, timestamp, likeCount, and a collection of comments. All posts support operations like addComment() and like(). Media-based posts,specifically PhotoPost and VideoPost, share additional attributes related to the media itself, such as fileSize and resolution. Recognizing these similarities suggests we should introduce a general Post superclass for common fields and methods, and possibly an intermediate class like MediaPost to capture media-related properties."
Notice how clearly stating these overlaps early on gives you a roadmap for your class hierarchy even before defining any fields or writing any methods. In this step, you don’t yet commit to a final design. You simply identify potential inheritance relationships based on common functionality or data.
From this analysis, we can clearly see that the "Common Attributes" column identifies potential fields and methods for a Post superclass, while the "Specialized Attributes" belong in the respective subclasses.
While looking for superclasses, also consider potential interfaces for capabilities that cross hierarchies. For example, both posts and user profiles might be Likeable but belong to different class hierarchies. This might suggest an interface like Likeable with methods such as like() and getLikeCount().