Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 22nd, 2004, 12:30 PM
vb.net vb.net is offline
Demonic Swordsman DGQB
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Apr 2003
Posts: 1,009 vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 14 h 42 m 21 sec
Reputation Power: 77
concept of polymorphism

I am slowly understanding more about Polymorphism in obj-oriented terms. I will try to illustrate it as I interpret; please tell me if my points are correct about the subject:

the ability to name identical methods that perform different things.
ex1: when an object/class has several methods with the same name that take different parameters:
Calculate(int a), Calculate(int a, int b), Calculate(string direction)
ex2: classes WINDOW and DOOR both have open() and close() methods -- they are named the same but perform duties in different manners
ex3: in VB.NET, the msgbox() method takes different paramters -- you may only include a msg, or you can give additional
settings such as bar title and Buttons included. This is possible because the class library must have done it this way:

msgbox(string msg) 'First form
msgbox(string msg, DialogResult buttons) 'Second form
msgbox(string msg, DialogResult buttons, string title) 'third form

Depending on which parameters you pass, the appropriate method will execute.

Reply With Quote
  #2  
Old April 22nd, 2004, 11:52 PM
goraya goraya is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 17 goraya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 20 m 20 sec
Reputation Power: 0
Thumbs up Right

Hi
It's correct all you write about polymorphism.......

Reply With Quote
  #3  
Old April 23rd, 2004, 02:14 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
I thought polymorphism is where you inherit properties from more then one class.

ie: my "myBigNum" class may inherit from the "string", "integer", and "math" classes I wrote.

What you describe sounds like simple method/operator overloading. But I may be wrong. I use a lot of techniques I don't know the names of.
__________________
"Science is constructed of facts as a house is of stones. But a collection of facts is no more a science than a heap of stones is a house." - Henri Poincare

Reply With Quote
  #4  
Old April 23rd, 2004, 08:53 PM
aragon
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Example 2 best describes polymorphism in OO terms, the other two might refer to other forms of polymorphism but I think they are usually referred to as "name overloading" or something like that. Foldoc describes different types of polymorphism, including 'OO polymorphism', but it might be confusing so here's a longer explanation of what polymorphism is in terms of objects.

In OO, polymorphism is the ability to call a method of an object without knowing precisely what object you have. All you know is what the method looks like - i.e. it's name and parameters - and that the object will have that method; essentially the object is an instance of a class which belongs to a family of related classes (typically through inheritance). The 'correct method' to call is resolved at runtime, not compile time.

Here is some OCaml code declaring some classes which don't do anything terribly useful but serve to highlight polymorphism in OO terms.

Code:
(* abstract class - doesn't have to be abstract, just is in this example *)
class virtual base_class = 
object(self)
  method virtual do_something : int -> int
end;;

(* non-abstract class 1 *)
class sub_one =
object 
  inherit base_class
  method do_something x = x*x
end;;

(* non-abstract class 2
class sub_two =
object
  inherit base_class
  method do_something x = -x
end;;

(* call method 'do_something' for object 'p' *)
let perform_action (p : base_class) x = 
  p#do_something x;;


And here is simple program using the above classes to illustrate polymorphism

Code:
let main () = 
  let 
    s1 = new sub_one and 
    s2 = new sub_two 
  in      
    print_int (perform_action s1 5); 
    print_newline();
    
    print_int (perform_action s2 5);
    print_newline();;

main();;


The above program will print the results of calling the do_something method on two objects...

Code:
25
-5


The perform_action function doesn't know anything about sub_one or sub_two classes, it only knows about base_class. Since both sub_one and sub_two inherit from base_class they can be used where an object of type base_class can and the runtime system will figure out the right version of the method to call.


HTH,
C

p.s. For the curious, OCaml has both parametric polymorphism and OO polymorphism and can combine both to provide interesting reuse capabilities.

edit: oops! forgot the results of the program...

Last edited by aragon : April 23rd, 2004 at 08:56 PM.

Reply With Quote
  #5  
Old April 24th, 2004, 01:55 AM
vb.net vb.net is offline
Demonic Swordsman DGQB
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Apr 2003
Posts: 1,009 vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 14 h 42 m 21 sec
Reputation Power: 77
Hi aragon. One confusion - you say that we can call a method of an object without knowing precisely what object you have, but in the illustration we see that s1 is an obj of the Sub_One class, right?

Reply With Quote
  #6  
Old April 24th, 2004, 03:16 PM
aragon
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Yes that's correct. We know what object we have by looking at the code, but this is a very simple example. I was referring to code like perform_action which is written so that it doesn't know exactly what object it has. It expects a parameter of type base_class and calls a method as if it were an object of type base_class. It doesn't know that the parameter is actually a sub_one or sub_two at compile time because it doesn't know these classes even exist. The code is the same for all classes inheriting from base_class.

In essence you can think of base_class as defining a family of classes. perform_action knows that it can call the methods of any member of this family without knowing where in the tree the member is. The runtime system works out that it's a sub_one and calls that right code, but at compile time we (i.e. perform_action) don't know we have anything other than an object of type base_class. The point of this polymorphism is that we don't want to know exactly what type of object we have, only that it extends from base_class. This allows us to write code that works for all objects of the family, even objects that haven't been written or even concieved of yet.

Consider a drawing program with a number of primitive shapes. Let's say you have two types of shape, rectangles and circles. The process for drawing a rectangle is different from a circle so we need to have different code for each. We could write code like this...

Code:
let draw_shape x =
   if x = circle then
      (* code for circle drawing algorithm. *)
   else
      (* code for rectangle algorithm. *)


but what happens if we suddenly discover we want to draw triangles? We could modify the code like this

Code:
let draw_shape x = 
   if x = circle then
      (* code for circle drawing algorithm. *)
   else 
      if x = rectangle then
         (* code for rectangle algorithm. *)
      else
         (* code for triangle algorithm. *)


Now what happens if we need to draw polygons, lines, etc? I haven't shown any parameters being passed to the code other than x, but you would need different parameters for each shape. 3 vertices/points for a triangle, 3 for a rectangle, 1 for a circle plus its' radius and you have to pass this info some how

A better way would be to write code that could work with all shapes. We can do this by creating a root class 'shape' with a draw method and have 'rectangle' and 'circle' classes provide the code to draw the shape... i.e. each type of shape knows how to draw itself. draw_shape disappears and we can extend the kinds of shapes the system can draw simply by extending the shape class and implementing the draw method. But the code that draws the entire scene doesn't change a bit and is simpler because it only knows about the shape class.

The higher level code will call the constructors on each object and provide the relevant parameters, but the guts of the system just deals with shapes.

I found this wikipidea link which describes polymorphism (with examples) better than foldoc.


C

Reply With Quote
  #7  
Old April 24th, 2004, 10:35 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Permanently Banned
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 7,351 DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level)DaWei_M User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 19 h 39 m 7 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Actually, the correct method to call is still usually determined at compile time. In the case of virtual functions, the binding is delayed until runtime if the pointer is to the base class and not a derivative. Polymorphism is as you define it, however....inheriting from multiple classes is just 'multiple inheritance.'

Reply With Quote
  #8  
Old April 26th, 2004, 12:52 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
Aragon's explanation sounds similar to function pointers, but using classes instead.

ie:
Code:
#include <stdio.h>

void pit(void);
void pit2(void);
void pit3(void);

void main(void){
	void (*c[2])(void);
	c[0]=&pit;
	c[1]=&pit2;

	c[0]();
	c[1]();

	void (*d)(void);
	d=&pit3;
	d();
}

void pit(void){
	printf("\nBlah blah blah");
}

void pit2(void){
	printf("\nMore signal, less noise!");
}

void pit3(void){
	printf("\nLook at me!  Woo Hoo!");
}


I use this often when writting my own languages. The p-code is a byte representing the array number of the function to call. It's very fast. (the function grabs the params from memory relative to it's location (param1=mem[pc+1];))

BTW: I had that code on my HD to make sure a free C compiler I downloaded allowed function pointers. That's why the "printf"s, and no "cout"s.

Reply With Quote
  #9  
Old April 27th, 2004, 03:38 AM
bergner bergner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 144 bergner User rank is Private First Class (20 - 50 Reputation Level)bergner User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 48 m 6 sec
Reputation Power: 5
There are three types of polymorphism

1. Inclusion polymorphism is found in OO languages where it is combined with dynamic binding to allow a reference to a baseclass refer to subclass. When invoking methods on that reference the implementation found in the subclass is used. The referencing part is inc. polymorphism and the execution of the subclass method is dynamic binding.

2. Parametric polymorphism is found in functional languages, e.g. Haskell, Miranda and Standard ML. It means that a parameter can have an arbitrary type. This is _not_ the same as templates and such.

3. Ad-hoc polymorphism is just a weird name for overloading. MsgBox is just several overloaded functions with the same name. Doesn't really have anything to do with OO, but this feature is present in most OO languages.

Reply With Quote
  #10  
Old April 29th, 2004, 04:12 PM
JeffCT JeffCT is offline
PHP & Ruby Developer
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2001
Posts: 1,437 JeffCT User rank is Lance Corporal (50 - 100 Reputation Level)JeffCT User rank is Lance Corporal (50 - 100 Reputation Level)JeffCT User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 5 h 36 m 40 sec
Reputation Power: 9
In most cases you will use polymorphism when you have a class inherit the funciontality of another class but need some (but not all) of the functionality of the child to be different. This way you can simply override the methods that require different functionality, while leaving the rest the same as they were when inherited.

Reply With Quote
  #11  
Old April 29th, 2004, 06:05 PM
AlexM4465 AlexM4465 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 41 AlexM4465 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
When I think of polymorphism I consider the following class declarations.

Code:
class C{
	public:
	virtual void print(){cout << "C Class";};
};
class A : public C{
	public:
	void print(){cout << "A Class";};
};
class B : public C{
	public:
	void print(){cout << "B Class";};
};

notice that all my pointers are of type C (the Base class) the next question is what do you think will be printed when I call print from my base class pointers ?
Code:
int main(){ 
    C *C_Class_Ptr1 = new A;
    C *C_Class_Ptr2 = new B;
    C *C_Class_Ptr3 = new C;
	
	C_Class_Ptr1->print();
	C_Class_Ptr2->print();
	C_Class_Ptr3->print();
}


if you thought the out put would be "C Class C Class C Class" then you would be wrong. Its A Class B Class C Class. this clearly shows polymorphism

Reply With Quote
  #12  
Old May 6th, 2004, 08:15 AM
bergner bergner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 144 bergner User rank is Private First Class (20 - 50 Reputation Level)bergner User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 48 m 6 sec
Reputation Power: 5
That's the standard example

Although polymorphism is actually only the ability for a C* to refer to an A* or B* transparently. The call dispatching to the correct method is dynamic binding. Don't confuse them like all those crappy OO book authors...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > concept of polymorphism


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |