IT SOLUTIONS
Your TECHNOLOGY partner! 
-Collapse +Expand
Coder
Search Coder Group:

Advanced
-Collapse +Expand Coder Group Home
-Collapse +Expand Message Board
-Collapse +Expand Coder KB
-Collapse +Expand Coder Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Coder Store
PRESTWOODSTORE
-Collapse +Expand Members Only

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyCoding & OO BoardObject Orientation (OO) Topic     Print This   

Polymorphism Defined

Polymorphism Defined in Object Orientation (OO) topic (part of our Coding & OO group).

Quick Search: Polymorphism   Defined   Polymorphism Defined  

Mike Prestwood

Okay, I need some help "clearly" defining polymorphism for an upcoming article. Read the following text and reply and let me know if it's clear and if you have any suggestions.

Polymorphism

Polymorphism allows any descendant class to redefine any method inherited from its parent class. This allows the software architect to design a dog.run and a cat.run methods and then the programmer can decide at development time whether to instantiate a dog or cat object and either way he knows to call the run method to make the object run. If the designer created a MakeDogRun and a MakeCatRun methods, then the programmer would have a hard time remembering different commands.

Polymorphism and Substitutability

Substitutability allows a descendant class to be used anywhere an associated parent class is used. In object oriented programming a variable could refer to one object at one time and then another object another time. This allows the designer of software to create both a dog.run and a cat.run methods and then decide at runtime whether the variable will be a dog or a cat

--
Mike Prestwood
Prestwood IT Solutions

 Posted 12 years ago (Thread Starter)
Comment Quote
Location=Prestwood IT office in Citrus Heights, CA,  Joined=13 years ago   MB Posts=1398   KB Posts=1490  
More... -Collapse +Expand
Moderator
Mike Prestwood
Prestwood IT
Prestwood IT office in Citrus Heights, CA
Rank: Fleet Admiral
Email A E CA USA
Approved member.
Member subscribes to this thread with a verified email.
About Mike Prestwood

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Web Presence
Facebook, Prestwood IT Facebook page -- fan page. (Visit Me)
Twitter, Follow Prestwood IT on Twitter. (Visit Me)
LinkedIn, Prestwood IT company page on LinkedIn. (Visit Me)
YouTube, Prestwood IT YouTube Channel (Visit My Channel)
Website, My drum website where I sell my drum books. (http://www.play-drums.com)

Post ID #3342, 4 replies
Thread Started 8/13/2001 5:11:00 PM
View Counter=1606
Last Reply Posted 10/18/2001 3:41:00 PM)

Mike Prestwood
William:

Thanks for the reply. Your definition above is good and is typical of a Delphi explanation of ploymorphism, but I'm not looking for "another" definition, I need feedback on the defition in the post. The article I'm writing is for a non-Delphi tool and can't contain code. Therefore, the definition must be non-vendor and non-tool oriented. Given that, is there anything about my definition you would change? If so, what.

Mike

--
Mike Prestwood
Prestwood IT Solutions

 Posted 12 years ago (Thread Starter)
Comment Quote
Location=Prestwood IT office in Citrus Heights, CA,  Joined=13 years ago   MB Posts=1398   KB Posts=1490  
More... -Collapse +Expand
Moderator
Mike Prestwood
Prestwood IT
Prestwood IT office in Citrus Heights, CA
Rank: Fleet Admiral
Email A E CA USA
Approved member.
Member subscribes to this thread with a verified email.
About Mike Prestwood

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Web Presence
Facebook, Prestwood IT Facebook page -- fan page. (Visit Me)
Twitter, Follow Prestwood IT on Twitter. (Visit Me)
LinkedIn, Prestwood IT company page on LinkedIn. (Visit Me)
YouTube, Prestwood IT YouTube Channel (Visit My Channel)
Website, My drum website where I sell my drum books. (http://www.play-drums.com)

Post ID #3370 (Level 1.1)  Reply to 3342
Thread Started 8/14/2001 11:57:00 AM
View Counter=2

William Pantoja
Polymorphism (poly - multiple, morph - change)

The ability of a descendant class to take on the identity of it's ancestor. Polymorphism can only be supported through true inheritance. That is, all the properties and methods of an ancestor class must be inherited by the descendant.

If a descendant class overrides a method of the ancestor, the descendant's method is called regardless if the descendant class is being used directly or if it is acting as it's ancestor.

For example, suppose you have a drawing object called TShape and has a method called Draw which draws itself on a drawing surface (for example, a drawing context on Windows or a Canvas object in Delphi) at a point (X, Y) which are specified by two properties, X and Y.

type
TShape = class
private
FX : Integer;
FY : Integer;
public
constructor Create (X, Y : Integer); virtual;
procedure Draw (Canvas : TCanvas); virtual;
property X : Integer read FX write FX;
property Y : Integer read FY write FY;
end;


You also define two classes, TCircle and TRectangle which both decend from TShape. In TCircle, you add a Radius property and override the Draw method to draw the circle with it's center at (X, Y) and a radius of Radius. You define TRectangle with Width and Height properties and also override the Draw method to draw the rectangle with a width Width, a height Height, and it's upper left corner at (X, Y).

type
TCircle = class
private
FRadius : Double;
public
constructor Create (X, Y : Integer; Radius : Double); virtual;
procedure Draw (Canvas : TCanvas); override;
property Radius : Double read FRadius write FRadius;
end;

TRectangle = class
private
FHeight : Integer;
FWidth : Integer;
public
constructor Create (X, Y, Width, Height : Integer); virtual;
procedure Draw (Canvas : TCanvas); override;
property Height : Integer read FHeight write FHeight;
property Width : Integer read FWidth write FWidth;
end;


What can you do with this? Lets say you have various circles and rectangles which you want drawn. Instead of having two arrays, one an array of TCircle and another an array of TRectangle, you can take advantage of polymorphisim and have a single array of TShape objects which you place the various TCircle and TRectangle objects.

 :
:
var
Shapes : array [0..4] of TShape;

begin
Shapes[0] := TCircle.Create(100,50,8);
Shapes[1] := TRectangle.Create(56,345,50,50);
Shapes[2] := TRectangle.Create(210,90,5,100);
Shapes[3] := TCircle.Create(245,124,34);
Shapes[4] := TRectangle.Create(5,115,124,87);
end;


Because TCircle and TRectangle are polymorphic, you can assign then to a variable that is of type TShape. Moreover, if you were to iterate through this array and call the Draw method of TShape, the appropriate Draw method of TCircle or TRectangle would be called.

How is this accomplished? Part of being polymorphic requires that all descendant objects inherit all properties and methods of it's ancestor. In this case, TCircle and TRectangle inherit the Create constructor, the Draw method, and the X and Y properties. Because of this, you can utilize any property or method defined in the ancestor.

A common misconception is that polymorphism is the ability to override the ancestor methods. Although this ability makes polymorphism more powerful (and useful), it is not actually part of polymorphism. You could eliminate this ability and still have a polymorphic object.

How do languages (and Delphi in particular) know which method to call of a polymorphic object? When you create a class, an entity called a virtual method table (VMT) is also created. The VMT is nothing more then a list of methods and their pointers in the object. When a descendant class is declared, it gets a copy of it's ancestor's VMT and then adds it's own entries at the end for it's own methods. If a descendant overrides a method of it's ancestor, the descendant replaces the pointer to the original method in it's own VMT with the pointer to the new method.

So, when the Draw method is called, it is looked up in the VMT and the pointer that cooresponds to the Draw method is used. The mechinism that does this at run time does not know, or indeed care, whether or not the pointer is pointing to the original Draw method of TShape or the Draw method of TCircle and TRectangle.

---
William Pantoja

Consultant/Software Engineer
ForceOne Technologies, Inc.

 Posted 12 years ago
Comment Quote
Location=Woodinville, WA, USA ,  Joined=12 years ago   MB Posts=163   KB Posts=1  
More... -Collapse +Expand
William Pantoja
Woodinville, WA, USA
Rank: Lieutenant-Junior Grade
Email A E WA USA
Approved member.
Member subscribes to this thread with a verified email.

Post ID #3345 (Level 1.2)  Reply to 3342
Reply Posted 8/13/2001 9:42:00 PM

William Pantoja
I would take care not to confuse readers with the concepts of polymorphism and inheritance. Inheritance includes an object's gaining all the functionality of its ancestor and the option to redefine (override) that functionality. Polymorphism only deals with the interchangability or substitutability of objects.

Your first definition is convering inheritance. Your second is polymorphism.

Short definition:

Polymorphism (or interchangability) is the ability of a descendant class to take on the identity of it's ancestor. An object which is polymorphic can be interchanged with it's ancestor and used as if it was its ancestor.

Polymorphism can only be supported through true inheritance. That is, all the properties and methods of an ancestor class must be inherited by the descendant.

A common misconception is that polymorphism is the ability to override the ancestor methods. Although this ability makes polymorphism more powerful (and useful), it is not actually part of polymorphism. You could eliminate this ability and still have a polymorphic object.

---
William Pantoja

Consultant/Software Engineer
ForceOne Technologies, Inc.

 Posted 12 years ago
Comment Quote
Location=Woodinville, WA, USA ,  Joined=12 years ago   MB Posts=163   KB Posts=1  
More... -Collapse +Expand
William Pantoja
Woodinville, WA, USA
Rank: Lieutenant-Junior Grade
Email A E WA USA
Approved member.
Member subscribes to this thread with a verified email.

Post ID #3374 (Level 1.3)  Reply to 3342
Reply Posted 8/14/2001 1:17:00 PM
Most Recent Post

deleted_member


Polymorphism (or interchangability) is the ability of a descendant class to take on the identity of it's ancestor. An object which is polymorphic can be interchanged with it's ancestor and used as if it was its ancestor.

Polymorphism can only be supported through true inheritance. That is, all the properties and methods of an ancestor class must be inherited by the descendant.



That is not necessarily true. Polymorphism, in the simplest sense, in the ability for an object to be treated like another object of a seemingly different type. An excellent example of polymorphism is in Java Interfaces. When you implement an interface on an object you can treat that object as something that seems to be completely different. To do this you implement the interface or 'teach' the object what it needs to know to act like another object.

An example I heard once (that probably over simplifies it a bit) is the Whale-Alarmclock vs. The Whale-that-can-tell-time. The Whale-Alarmclock is a great example of bad multiple-inheritence. It would be like you took a whale and tried to surgically put an alarmclock in it's brain. If you succeeded (unlikely) the whale would be miserable and the clock probably wouldn't work quite right. But at 3:00pm, when the alarm clock goes off, you'll get a reaction from the whale I'm sure... Big Grin!

Now, given enough motivation, training, and fishmeal you could instead teach the whale how to tell time.. and teach the whale that at 3:00pm it needs to blow water from it's spout. The end effect is the same - you have an alarm clock but the approach is different.

To take it to a more language-specific level: In Java, you can have an object that extends from an GUI (JFrame) but knows how to work in it's own thread (Implements Runnable). Now I create my new GUI object (we'll call it myJFrame) and then create my new Thread and tell it to run on myJFrame. You now have a Threaded JFrame.

Polymorphism is what you make of it... Get it? Big Grin!
 Posted 12 years ago
Comment Quote
Location=USA,  Joined=6 years ago   MB Posts=182  
More... -Collapse +Expand
Deleted Member
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.
Post ID #4101 (Level 1.4)  Reply to 3342
Reply Posted 10/18/2001 3:41:00 PM

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post:

Enter your name and security key.

Your Name:
Today's security key = P235A
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #3342 Counter
1606
Since 4/2/2008

Regarding...

Linked Knowledge Base Article.

This thread is linked to the following KB article.


Mike Prestwood
1. Polymorphism

A coding technique where the same named function, operator, or object behaves differently depending on outside input or influences. Usually implemented as parameter overloading where the same named function is overloaded with other versions that are called either with a different type or number of parameters. Polymorphism is a general coding technique and other specific implementations are common such as inheritance, operator overloading, and interfaces.

Posted to KB Topic: Object Orientation (OO)
10 years ago, and updated 51 months ago

Definition
Nothing New Since Your Last Visit
5388
Hits

Coding & OO

-
  Load Time=1 seconds.
 
Print This
-
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


491 People Online Now!!  
Online Now: Sign In to see who's online now!  Not a member? Join now. It's free!
Show More...


©1995-2013 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech