However, there are occasions with polymorphism when these switch constructs reintroduce themselves. This tip focuses on single and multiple polymorphism, with examples that use Java source code. In my previous Java Tip , I discussed how you can get your compiler to check that constant values are valid. This new Java Tip also incorporates typesafe constants and a technique that can be used to remove the aforementioned condition checking.
Single polymorphism is achieved by method overriding.
Listing 1 provides an example of single polymorphism, demonstrating how classes that implement the Traceable interface simply write their current state to System.out . The code in Listing 1 works fine if you only want to trace your objects to System.out . A bad solution would be to define a set of interfaces, one for each type, as show in Listing 2. You have to implement a new interface each time your requirements change, resulting in a proliferation of interfaces and classes which can make a design difficult to understand and extend. Multiple polymorphism Multiple polymorphism is when an abstract class uses another abstract class.