Specializing Classes

Specializing Classes

You might hear some experts talk about specialization. Specialization is just the opposite of generalization. Instead of taking common features from subclasses and creating a generic superclass, you create specialized classes from a common superclass. In the archive we have “print media.” It turns out there are two kinds of print media – books and transcripts. So print media is a superclass. Books and transcripts are specializations and thus subclasses of print media. While we started with the idea of having a class called PrintMedia, we recognized there were special forms of PrintMedia in the archive.

 Remember   When you generalize, you start with some subclasses and develop a superclass. When you specialize, you start with a superclass and develop some subclass.

For specialization, we start with PrintMedia and use UML to show PrintMedia as a superclass and it’s “specialized” subclasses. Figure shows the inheritance hierarchy for PrintMedia. The Book class holds its own unique attributes of isbn, author, title, publisher, and publishDate. Transcript (on the other hand) has typist, editor, and transcribed attributes.

Click To expand
Figure: Print-media inheritance hierarchy.

We may have specialized from the class PrintMedia instead of generalized, but you notice that we still have an inheritance hierarchy with a superclass and a couple of subclasses. So, the Book and Transcript subclasses both inherit sheetWeight, paper and needsBinding from PrintMedia.

 Warning   Generalization and specialization are just two sides of the same coin. Whether you generalize or specialize, the UML diagram ends up having superclasses connected to subclasses—an inheritance hierarchy. A developer looking at your diagram might focus on the superclass and think of the subclasses as specializations. Another developer looking at your diagram might focus on the subclasses and think of the superclass as a generalization. You can look at inheritance diagrams either way no matter what technique (generalization or specialization) you used to create the diagram.

When doing practical development of systems, you’ll find that you’re doing a fluid dance with generalization and specialization. Sometimes you’ll be seeing how things look the same, comparing them and generalizing the results. Sometimes you’ll be seeing how things look different, contrasting them and specializing the results. If you work hard at it, and dance with everyone, you’ll find the order of your dancing doesn’t make that much difference to the final generalization hierarchy. You’ll have gathered together the commonalities, separated out the differences, and made a robust hierarchy.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows