File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Switch statement vs Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Switch statement vs Polymorphism" Watch "Switch statement vs Polymorphism" New topic
Author

Switch statement vs Polymorphism

Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

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


OCPJP 6, OCEWCD Java EE 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

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

Joined: Oct 13, 2005
Posts: 39415
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch statement vs Polymorphism