Objects by Design Forums Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Calendar Find other members Frequently Asked Questions Search Home  
Objects by Design Forums : Powered by vBulletin version 2.3.5 Objects by Design Forums > Main Forums > UML Certification Review Questions > polymorphism
Pages (2): [1] 2 »   Last Thread   Next Thread
Author
Thread Post New Thread    Post A Reply
SZ
Administrator

Registered: Apr 2001
Location: New York
Posts: 492

polymorphism

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).

This is one of the better questions on the exam because it lacks ambiguity while still providing reasonable choices.

If you have read Bertrand Meyer's OOSC2 you will of course know that C is the best answer. If not, then see page 61, the section titled Single Choice.

If you get nothing else out of object-orientation, knowing how and when to use polymorphism would be enough!

Report this post to a moderator | IP: Logged

Old Post 08-07-2001 03:11 AM
SZ is offline Click Here to See the Profile for SZ Click here to Send SZ a Private Message Visit SZ's homepage! Find more posts by SZ Add SZ to your buddy list Edit/Delete Message Reply w/Quote
shreedev
Junior Member

Registered: May 2001
Location:
Posts: 24

Arrow polymorphism

Hi,
The answer b) matches the question "A resulting benefit of using polymorphism is reduction of: subclasses needed to accomplish the same functionality "
The reason is
As we override the super class method with different implementation, there is reduction of same functionality of superclass.

Thanks and Regards,
sridhar



__________________
sridhar

Report this post to a moderator | IP: Logged

Old Post 08-13-2001 10:23 AM
shreedev is offline Click Here to See the Profile for shreedev Click here to Send shreedev a Private Message Find more posts by shreedev Add shreedev to your buddy list Edit/Delete Message Reply w/Quote
SZ
Administrator

Registered: Apr 2001
Location: New York
Posts: 492

Hi Sridhar,
I don't understand your answer.

With polymorphism you can have as many subclasses as you want. In fact, polymorphism encourages subclassing. There is no difference if you have 1 or 100 subclasses. In fact you could say that the benefit of subclassing is greater as the number of subclasses increases because the client only sees the methods of the superclass and is spared all the details of the subclasses.

SZ

Report this post to a moderator | IP: Logged

Old Post 08-13-2001 01:17 PM
SZ is offline Click Here to See the Profile for SZ Click here to Send SZ a Private Message Visit SZ's homepage! Find more posts by SZ Add SZ to your buddy list Edit/Delete Message Reply w/Quote
shreedev
Junior Member

Registered: May 2001
Location:
Posts: 24

polymorphism

Hi SZ,
What you have mentioned is correct about the subclasses. What I mentioned is not the (reduction) number of subclasses. Polymorphism reduce the subclass inherited methods (ie) functionality because we are overriding the method with different implementation(functionality).
A resulting benefit of using polymorphism is reduction of subclasses needed to accomplish the same functionality

Thanks and Regards,
sridhar

__________________
sridhar

Report this post to a moderator | IP: Logged

Old Post 08-14-2001 09:03 AM
shreedev is offline Click Here to See the Profile for shreedev Click here to Send shreedev a Private Message Find more posts by shreedev Add shreedev to your buddy list Edit/Delete Message Reply w/Quote
SZ
Administrator

Registered: Apr 2001
Location: New York
Posts: 492

Sridhar, I highly recommend reading the section in Bertrand Meyer's OOSC2 referred to above. This will help you understand how polymorphism replaces the use of case statements and conditionals.

Report this post to a moderator | IP: Logged

Old Post 08-15-2001 03:10 AM
SZ is offline Click Here to See the Profile for SZ Click here to Send SZ a Private Message Visit SZ's homepage! Find more posts by SZ Add SZ to your buddy list Edit/Delete Message Reply w/Quote
kfoley
Junior Member

Registered: Aug 2001
Location:
Posts: 1

I really think D is the correct answer there. One cannot use polymorphism unless there is low coupling of the classes. So using polymorphism would result in the additional benefit of low coupling among classes.

Report this post to a moderator | IP: Logged

Old Post 08-16-2001 12:17 AM
kfoley is offline Click Here to See the Profile for kfoley Click here to Send kfoley a Private Message Find more posts by kfoley Add kfoley to your buddy list Edit/Delete Message Reply w/Quote
xenub
Junior Member

Registered: Aug 2001
Location:
Posts: 5

IMHO, using polymorphism would NOT result in the additional benefit of low coupling among classes.

Polymorphism/Inheritance makes systems more flexible and more easily extensible.

Encapsulation result in low coupling of classes.

And sometimes, Polymorphism even breaks encapsulation.

Report this post to a moderator | IP: Logged

Old Post 08-25-2001 06:15 PM
xenub is offline Click Here to See the Profile for xenub Click here to Send xenub a Private Message Find more posts by xenub Add xenub to your buddy list Edit/Delete Message Reply w/Quote
pfexa
Junior Member

Registered: Aug 2001
Location: Berlin / Germany
Posts: 16

I beleive that
c) case statements and conditionals
is the correct answer.

I may be wrong, but this formulation appears to me clear and obvious - in comparison with all others which I find very vague...

Report this post to a moderator | IP: Logged

Old Post 08-29-2001 01:38 PM
pfexa is offline Click Here to See the Profile for pfexa Click here to Send pfexa a Private Message Find more posts by pfexa Add pfexa to your buddy list Edit/Delete Message Reply w/Quote
ram
Junior Member

Registered: Oct 2001
Location:
Posts: 9


c) case statements and conditionals

b) subclasses needed to accomplish the same functionality

I will opt for C

b sounds good , but it is result of inheritance - not polymorphism. Is n't it?

Thanks ans Regards
Ram



Report this post to a moderator | IP: Logged

Old Post 10-31-2001 04:45 AM
ram is offline Click Here to See the Profile for ram Click here to Send ram a Private Message Find more posts by ram Add ram to your buddy list Edit/Delete Message Reply w/Quote
hsrivatsa
Junior Member

Registered: Jul 2001
Location:
Posts: 10

Let's first understand "polymorphism"- multiple methods, same name and different signature.

What is achieved through this? - all similar operations or functions are grouped under the same name, with different parameters.

Basically, the same operation is performed on different parameters.

Let's examine the options one by one.

a) Reduce methods in associated class -
i) Which do you call as associated class? The calling or the callee of the method
ii) Reduce Methods , is ridiculous!

b) hmmm... frankly I see no relation between polymorphism and subclasses, classes relate to objects and polymorphism relates to grouping of functions.

c) This is more of a hardcore Code oriented approach. If there were no polymorphism then probably the parameters type would have to be detemined within case structures.

d) Reduce coupling between classes this is true to some extent as the calling class now need not bother about which method to call but only the parameters.

Report this post to a moderator | IP: Logged

Old Post 10-31-2001 03:12 PM
hsrivatsa is offline Click Here to See the Profile for hsrivatsa Click here to Send hsrivatsa a Private Message Find more posts by hsrivatsa Add hsrivatsa to your buddy list Edit/Delete Message Reply w/Quote
xenub
Junior Member

Registered: Aug 2001
Location:
Posts: 5

Hello hsrivatsa, are you sure you're not mixing up operation Overloading with Polymorphism?

With Polymorphism, the signature of the polymorphic operations have to be the same to be Polymorphic, otherwise the operation is just overloaded with different parameters and return types.

Polymorphism Ex:
----------------------------------------------------------------
class SuperClassExample
{
// Polymorphic operation declaration (virtual)
virtual string HelloWorld(int aNumber)
{
// An implementation
return "Hello World " + aNumber;
}
}

class SubClassExample extends SuperClassExample
{
// Polymorphic operation overriding
override string HelloWorld(int aNumber)
{
// A different implementation
return "Hello World " + aNumber * 10;
}
}

Then a client can use the super class as :

SuperClassExample anInstance;
String aResult;

anInstance = new SuperClassExample();
aResult = anInstance.Helloworld(2);
// aResult = Hello world 2

anInstance = new SubClassExample();
aResult = anInstance.Helloworld(2);
// aResult = Hello world 20

----------------------------------------------------------------

The point of polymorphic operation is to be able to call an operation on a Superclass and execute the implementation of a subclass, without having to know or cast to the subclass explicitly.

On the other hand, operation overloading is simply to have two operations that have the same name and do the same thing (same enough to have the same operation name) but have different signatures and implementation.

Operation overloading can be implemented either in the superclass itself or in any of its subclasses (as long as you have the right visibility to the operation).

Polymorphic operations must be declared as such in the supertype, and can only provide different implementation in their subtypes as you can't have 2 operations with the same signature in the same class.

In other words, when you call an overloaded operation, the compiler decide which operation implementation to call based on the parameters you give to the operation call.

In the other hand, when you call a polymorphic operation, the compiler (or maybe its at runtime, not sure on this) decide which implementation to use based on the Type of the class instance you're calling the operation on.

Note that overloaded operations can also be polymorphic, so you can take advantages of both as needed.

Finally, as many pointed out, the answer to the question is 'C'. A good example of such use of polymorphism is the 'State/Strategy' pattern (Desing Patterns by Erich Gamma et al).

Hope it helped!

xenub

Last edited by xenub on 10-31-2001 at 05:46 PM

Report this post to a moderator | IP: Logged

Old Post 10-31-2001 05:40 PM
xenub is offline Click Here to See the Profile for xenub Click here to Send xenub a Private Message Find more posts by xenub Add xenub to your buddy list Edit/Delete Message Reply w/Quote
hsrivatsa
Junior Member

Registered: Jul 2001
Location:
Posts: 10

Hi Xenub,
Operation overloading and Polymorphism are the same. There are two types of Polymophism:

i) Static polymorphism - Polymorphism which can be resolved by the complier.
Eg:- Method overloading.
ii) Dynamic polymorphism - Polymorphism which has to be resolved during run time.
Eg:- Method Overriding.

Polymorphism is a logical grouping of similar operations. A classic example of polymorphism is operator overloading. Let's take the "+" operator, the operands may change but the basic operation i.e. addition remains the same.
This is polymorphism in it's complete and true sense.

I feel "d" is the correct option.

I would like SZ to throw some light on the article from Bertrand Meyer Pg 61

Report this post to a moderator | IP: Logged

Old Post 11-02-2001 09:51 AM
hsrivatsa is offline Click Here to See the Profile for hsrivatsa Click here to Send hsrivatsa a Private Message Find more posts by hsrivatsa Add hsrivatsa to your buddy list Edit/Delete Message Reply w/Quote
xenub
Junior Member

Registered: Aug 2001
Location:
Posts: 5

hsrivatsa,
You're right. There are 4 kinds of polymorphism :

AdHoc: Overloading and Coercion.
Universal: Parametric and Inclusion.

I'll quote Ian Graham, Object-Oriented Methods, Page 16 :

``Polymorphism represents the ability of an abstraction to share features. Inheritance is a special kind of Polymorphism... Other terms also encountered are Polymorphism, overloading and operator overloading, which refer to the ability to use the same symbol for different purposes when the context is clear.
...
Formally, Polymorphism - having many forms - means the ability of a variable of function to take different forms at runtime, or more specifically the ability to refer to instances of various classes. Overloading is the special case where two different operations merely share the same name.
...
AdHoc polymorphism refers to using the same symbol for semantically unrelated operations.

Operator overloading fits into this category if we use the symbo + for addition of integers and matrices.

Another kind of AdHoc polymorphism is called coercion, which allows operations to work on input of different type, as when adding an integer to a real.

Universal polymorphism splits into Parametric and Inclusion (Inheritance) polymorphism.

Parametric polymorphism refers to the ability to substitute arguments from a range of types into a function call.``

Thanks for your reply hsrivatsa. I`m sorry if I misleaded some of you with my previous post. The truth rules!

Report this post to a moderator | IP: Logged

Old Post 11-02-2001 01:50 PM
xenub is offline Click Here to See the Profile for xenub Click here to Send xenub a Private Message Find more posts by xenub Add xenub to your buddy list Edit/Delete Message Reply w/Quote
REF
Junior Member

Registered: Nov 2001
Location: Washington
Posts: 9

Wink

Xenub,

Right on! The truth rules! As I read this thread I was thinking the same thing, "Wait, aren't there 4 forms of polymorphism?" Also, great book reference -- though Graham's book isn't for the timid: it's pretty thick (it rivals OOSC2 easily).

Just to throw my two bits in: I think c) and d) are good answers. If pressed, I'd have to pick c).

Polymorphism allows us to move type and/or state dependant behavior variations into subclasses and avoid complicated conditional logic (answer c).

Polymorphism (specifically, sub-type polymorphism), used well, also allows instances to collaborate at higher levels of abstraction without regard to their underlying concrete type (thus creating a weaker form of coupling: answer d).

Here's a pretty good article on polymorphism :http://www.javaworld.com/javaworld/...-polymorph.html

Last edited by REF on 11-03-2001 at 03:36 PM

Report this post to a moderator | IP: Logged

Old Post 11-03-2001 09:33 AM
REF is offline Click Here to See the Profile for REF Click here to Send REF a Private Message Find more posts by REF Add REF to your buddy list Edit/Delete Message Reply w/Quote
SZ
Administrator

Registered: Apr 2001
Location: New York
Posts: 492

I think it is useful to emphasize here the original idea that Bertrand Meyer's Single Choice principle underscores the need for polymorphism: it is client code which is perniciously affected by the need to add new cases to a switch statement wherever discrimination is needed for special handling in non-polymorphic code.

The following is an excerpt taken from OOSC2:

"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."

Report this post to a moderator | IP: Logged

Old Post 11-06-2001 04:58 AM
SZ is offline Click Here to See the Profile for SZ Click here to Send SZ a Private Message Visit SZ's homepage! Find more posts by SZ Add SZ to your buddy list Edit/Delete Message Reply w/Quote
All times are GMT. The time now is 06:37 PM. Post New Thread    Post A Reply
Pages (2): [1] 2 »   Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 

< Contact Us - Objects by Design >

Powered by: vBulletin Version 2.3.5
Copyright ©2000 - 2017, Jelsoft Enterprises Limited.
Copyright 1999-2005, Objects by Design, Inc.