Java Reference
In-Depth Information
Replace Conditional blocks with Polymorphism: In some scenarios, methods in a class
currently may be checking some values in order to decide the right action to perform. One
such example could be a class that draws a shape, which is defined by width and type
(circle or square). The code quickly becomes confusing as the same if or switch statements
are repeated throughout the class, i.e. in methods that calculate the area or perimeter of the
shape. By using polymorphism, the shape specific behavior can be offloaded to subclasses,
simplifying the code. This has the added benefit of allowing other subclasses, e.g. rectangle
or star, to be introduced without extensive code changes.
Search WWH ::




Custom Search