Organizing classes with UML diagrams
So far, our object-oriented solution includes nine classes with their fields and methods. However, if we take another look at these nine classes, we will notice that all of them have the same two methods: calculateArea
and calculatePerimeter
. The code for the methods in each class is different because each shape uses a special formula to calculate either the area or perimeter. However, the declarations, contracts, interfaces, or protocols for the methods are the same. Both methods have the same name, are always parameterless, and return a floating point value. Thus, all of them return the same type.
When we talked about the nine classes, we said we were talking about nine different geometrical 2D shapes or simply shapes. Thus, we can generalize the required behavior, protocol, or interface for these nine shapes. The nine shapes must define the calculateArea
and calculatePerimeter
methods with the previously explained declarations. We can create an interface...