siliconsenthil.in

If Else or Switch Case to Polymorphism

If you try to express your logic in if-else way like this,

  private static String getSoundIfElseWay(String animal) {
       if (animal.equalsIgnoreCase("Dog"))
            return "Bark";
       else if (animal.equalsIgnoreCase("Cat"))
            return "Mew";
       else if (animal.equalsIgnoreCase("Lion"))
            return "Roar";
       return "";
  }

then polymorphic way would be,