• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch statement vs Polymorphism

 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused with this statement.



will you explain it in code?

it was taken from this url. http://www.javaranch.com/drive/switch.html
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what polymorphism is? (There are different forms of polymorphism, but usually people mean subtype polymorphism).

Suppose you want to model animals in a program. You could write code like this:

The design of this code is very poor, from an object oriented programming point of view. The makeSound method in class Demo has a chain of if ... else if ... statements to determine with which animal we are dealing. Suppose that you'd add a new animal class, then you'd have to change the makeSound method also to add the sound of the new animal.

In a well-designed program, you would put everything that is necessary to know about a specific animal in the class for that animal. Note that in the program above, the knowledge about what sound to make is outside the classes Dog and Cat.

It looks innocent in that little example, but when you are writing a larger and more complex program, maintainability and extensibility become more and more important. If you need to modify code in a dozen places to add a new animal, then the program is hard to extend.

It's much better to do something like this:

In this example, we make use of subtype polymorphism. Note that an Animal can be either a Dog or a Cat (or any other animal, if you add more classes that extend Animal). In the main method, you don't even need to know what kind of animal you call makeSound on - it will automatically go to the method in the appropriate class.

If you would want to add another animal in the above code, you'd just have to add a class that extends Animal. All the functionality of the animal, such as what sound it makes, is contained in that class, and not scattered around in a dozen places. So, a program with this design is much easier to extend.
 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's how it is.. i was confused on the statement in using objects in switches..first thing that comes into my mind was the factory pattern. thanks sir Jesper. It's well explained.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that the values after the case keyword in a switch block must be compile-time constants, and they must be ints (or can be cast to int) or enum members (Java5+) or Strings (Java7 only). So that restricts the range of values permitted after case.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic