• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch statement vs Polymorphism

 
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic