• 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

4 definitions of polymorphism

 
Ranch Hand
Posts: 3404
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 4 kinds of polymorphism :
-----------------------------------
I was looking for the recent thread on polymorphism but couldn't find it.
Here are more references.(I thought the other thread didn't quite cover the whole area but was a good introduction to the beginner who asked the question. BTW what happened to it).

Reveal the magic behind subtype polymorphism


Object-Oriented Software Construction
=====================================
Author: Bertrand Meyer
Summary: Simply put, the most authoritative, scholarly reference on O-O concepts and architectures written to date.
Audience: Architects
Question:
---------
This question is at the heart of object-orientation:
14) A resulting benefit of using polymorphism is reduction of:
a) methods in the associated classes
b) subclasses needed to accomplish the same functionality
c) case statements and conditionals
d) coupling between classes in the system
Single Select - Please select the best answer (one and only one choice must be selected).
regards
 
buckaroo
Posts: 401
Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I pick
c) case statements and conditionals


Why not make action() a method of a common superclass? Then x.action()
(As I remember from somewhere in Core Java 2 Vol 1)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like your choice. I looked long at D as well. Your example "is type of x" has knowledge of all possible types, has to be modified to add a type. That's an ugly dependency, maybe not coupling. And one could argue that since inheritance is a particularly strong kind of coupling, polymorphism increases coupling!
Hey, and I really like the graphics in that article ... what you see and where it comes from in the type hierarchy. I'm gonna have to use that!
[ October 14, 2003: Message edited by: Stan James ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have thought the polymorphic solution (ie. no switch statement) has less coupling. In the switch statement solution the client is coupled to each type in the switch statement. In the polymorphic solution the client only has a dependency on the common interface to the classes.
Inheritance is a strong form of coupling but this is between classes in the inheritance hierarchy.
So polymorphism reduces coupling in client code...which is a good thing !
D.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test questions are so hard to interpret. Does the author mean the coupling we see in the code we're examining or the unseen coupling in the other classes that must be related to make polymorphism work? Pick one. Sheesh.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure wether this answers the question, but in my opinion replacing switch/if statements by polymorphism doesn't buy you anything *in general*, but only if coupling is reduced by doing so (which quite often is the case, of course). Does that compute?
 
HS Thomas
Ranch Hand
Posts: 3404
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is an excerpt taken from OOSC2 based around Bertrand Meyers Single Choice principle :
"The case instruction of Pascal and Ada comes in handy here; it is of course on purpose that its syntax mirrors the form of the declaration of a record type with variants. Fortran and C will emulate the effect through multi-target goto instructions (switch in C). In these and other languages a multi-branch conditional instruction (if - then - elseif - elseif - else - end) will also do the job."
"Aside from syntactic variants, the principal observation is that to perform such a discrimination every client must know the exact list of variants of the notion of publication supported by A. The consequence is easy to foresee. Sooner or later, you will realize the need for a new variant, such as technical reports of companies and universities. Then you will have to extend the definition of type PUBLICATION in module A to support the new case. Fair enough: you have modified the conceptual notion of publication, so you should update the corresponding type declaration. This change is logical and inevitable. Far harder to justify, however, is the other consequence: any client of A, such as B, will also require updating if it used a structure such as the above, relying on an explicit list of cases for p. This may, as we have seen, be the case for most clients."

Originally posted by Ilja Preuss :
but in my opinion replacing switch/if statements by polymorphism doesn't buy you anything *in general*


Did you mean that ,with polymorphism, there's still some adding of new types to do ?
But no updating of any additional clients is required ?
Which means Answer D is more correct.
regards
[ October 16, 2003: Message edited by: HS Thomas ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic