When modeling a problem domain, you often run across objects that are similar but not entirely the same. We use words like
- type or
to describe their relationship. For example, a truck is a type of vehicle, a plane is a kind of vehicle. If all the properties of truck and plane were the same, then they could be instances of the same class. But they share only some properties. In this case, you can generalize the shared properties, that is, you can place them in their own separate class called “vehicle” and inherit them. The unique properties of trucks and planes go into their own classes. To follow through with the example, you would need to combine all the properties of the vehicle class and the truck class to create an instance of a truck. The following series of images below describes an instance of generalization:
Use composition when you have a class that has a set of other objects, in any quantity. Use generalization when you have a class that shares
common properties with a set of objects, but can also have different properties or behavior. For example, a Car has components like engine,
wheels, seats, etc. This is a composition relationship. But a Vehicle is a generalization of a Car, because you can have other types of
Vehicles with different properties, like a Truck. Car and Truck are derived classes of Vehicle.
has two main objectives:
- to build highly cohesive classes and
- to maintain loose coupling between those classes.
High-cohesion means well-structured classes and loose coupling means more flexible, extensible software."
Cohesive means that a certain class performs a set of closely related actions. A lack of cohesion, on the other hand, means that a class is performing several unrelated tasks. Though lack of cohesion may never have an impact on the overall functionality of a particular class or of the application itself the application software will eventually become unmanageable as more and more behaviours become scattered and end up in wrong places.
The term generalization is also used another way. A generalization is the structure, that is, a class hierarchy, that you get when you organize
the class definitions using the processes of generalization and specialization.
To generalize means to identify the properties that a group of objects have in common.
To specialize means to identify the properties that distinguish similar objects from one another. Generalization supports the logical grouping of similar objects that we often use when we think and talk about objects. It allows the introduction of new objects without redefining the properties it shares with related objects already in a model. Generalization is classified as an association. However, be careful to recognize that generalization is unlike every other type of association. Every other type of association may be instantiated as a link between two objects. In contrast, a generalization connects two class definitions that must be used together to instantiate a single object.