Page 1 of 4

Polymorphism

Posted: Sat Jun 02, 2007 10:20 am
by abalfazl
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello friends!

Programming C# 

[quote]

5.3.2 Creating Polymorphic Methods
To create a method that supports polymorphism, you need only mark it as virtual in its base
class. For example, to indicate that the method DrawWindow( ) of class Window in
Example 5-1 is polymorphic, simply add the keyword virtual to its declaration, as follows:
public virtual void DrawWindow( )
Now each derived class is free to implement its own version of DrawWindow( ). To do so,
simply override the base class virtual method by using the keyword override in the derived
class method definition, and then add the new code for that overridden method.
In the following excerpt from Example 5-2 (which appears later in this section), ListBox
derives from Window and implements its own version of DrawWindow( ):


[syntax="csharp"]public override void DrawWindow( )
{
base.DrawWindow( ); // invoke the base method
Console.WriteLine ("Writing string to the listbox: {0}",
listBoxContents);
}
The keyword override tells the compiler that this class has intentionally overridden how
DrawWindow( ) works. Similarly, you'll override this method in another class, Button, also
derived from Window.
In the body of Example 5-2, you'll first create three objects, a Window, a ListBox, and a
Button. You'll then call DrawWindow( ) on each:

Code: Select all

Window win = new Window(1,2);
ListBox lb = new ListBox(3,4,"Stand alone list box");
Button b = new Button(5,6);
win.DrawWindow( );
lb.DrawWindow( );
b.DrawWindow( );
This works much as you might expect. The correct DrawWindow( ) object is called for each.
So far, nothing polymorphic has been done. The real magic starts when you create an array of
Window objects. Because a ListBox is-a Window, you are free to place a ListBox into a
Window array. You can also place a Button into an array of Window objects because a Button
is also a Window:

Code: Select all

Window[] winArray = new Window[3];
winArray[0] = new Window(1,2);
winArray[1] = new ListBox(3,4,"List box in array");
winArray[2] = new Button(5,6);
What happens when you call DrawWindow( ) on each of these objects?
for (int i = 0;i < 3; i++)
{
winArray[i].DrawWindow( );
}
All the compiler knows is that it has three Window objects and that you've called DrawWindow(
) on each. If you had not marked DrawWindow as virtual, Window's DrawWindow( ) method
would be called three times. However, because you did mark DrawWindow( ) as virtual and
because the derived classes override that method, when you call DrawWindow( ) on the array,
the compiler determines the runtime type of the actual objects (a Window, a ListBox and a
Button) and calls the right method on each. This is the essence of polymorphism. The
complete code for this example is shown in Example 5-2.

Example 5-2. Using virtual methods

Code: Select all

using System;
public class Window
{
// constructor takes two integers to
// fix location on the console
public Window(int top, int left)
{
this.top = top;
this.left = left;
}
// simulates drawing the window
public virtual void DrawWindow( )
{
Console.WriteLine("Window: drawing Window at {0}, {1}",
top, left);
}
// these members are protected and thus visible
// to derived class methods. We'll examine this
// later in the chapter
protected int top;
protected int left;
}
// ListBox derives from Window
public class ListBox : Window
{
// constructor adds a parameter
public ListBox(
int top,
int left,
string contents):
base(top, left) // call base constructor
{
listBoxContents = contents;
}

// an overridden version (note keyword) because in the
// derived method we change the behavior
public override void DrawWindow( )
{
base.DrawWindow( ); // invoke the base method
Console.WriteLine ("Writing string to the listbox: {0}",
listBoxContents);
}
private string listBoxContents; // new member variable
}
public class Button : Window
{
public Button(
int top,
int left):
base(top, left)
{
}
// an overridden version (note keyword) because in the
// derived method we change the behavior
public override void DrawWindow( )
{
Console.WriteLine("Drawing a button at {0}, {1}\n",
top, left);
}
}
public class Tester
{
static void Main( )
{
Window win = new Window(1,2);
ListBox lb = new ListBox(3,4,"Stand alone list box");
Button b = new Button(5,6);
win.DrawWindow( );
lb.DrawWindow( );
b.DrawWindow( );
Window[] winArray = new Window[3];
winArray[0] = new Window(1,2);
winArray[1] = new ListBox(3,4,"List box in array");
winArray[2] = new Button(5,6);
for (int i = 0;i < 3; i++)
{
winArray[i].DrawWindow( );
}
}
}
Output:
Window: drawing Window at 1, 2
Window: drawing Window at 3, 4
Writing string to the listbox: Stand alone list box
Drawing a button at 5, 6
Window: drawing Window at 1, 2

Window: drawing Window at 3, 4
Writing string to the listbox: List box in array
Drawing a button at 5, 6
Note that throughout this example, we've marked the new overridden methods with the
keyword override:
public override void DrawWindow( )
The compiler now knows to use the overridden method when treating these objects
polymorphically. The compiler is responsible for tracking the real type of the object and for
handling the "late binding" so that it is ListBox.DrawWindow( ) that is called when the
Window reference really points to a ListBox object.


[/quote]





Is it possible to do something like that in PHP?Polymorphism by inheritance in PHP....May you give me a simple example?

Does PHP support override ,Like C#?

Thanks in advance.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Jun 02, 2007 10:26 am
by jayshields
AFAIK PHP supports polymorhphism (properly) as of PHP5.

If I'm correct, it will be the same syntax as any other language which supports polymorphism, which can easily be found via Google.

Posted: Sat Jun 02, 2007 11:02 am
by Weirdan
jayshields wrote:AFAIK PHP supports polymorhphism (properly) as of PHP5.
since php4 actually

Posted: Sat Jun 02, 2007 11:28 am
by Ambush Commander
PHP is actually much more flexible with regards to polymorphism than C++ is, to the point where methods you wish to be polymorphic don't have to have a common abstract parent function to be interchangeable. Here is a simple case of PHP4-style polymorphism:

Code: Select all

class A {
 function foo() {echo 'A!';}
}
class B {
  function foo() {echo 'B!';}
}
function doStuff($obj) {
 $obj->foo();
}
$a = new A;
$b = new B;
doStuff($a);
doStuff($b);

Posted: Sat Jun 02, 2007 2:01 pm
by abalfazl
Hello!

Does PHP 5 support polymorphism like C#?

If it is,Then Please give me an example,Like C# example,An array that each member of that array can do something,And it uses inheritance...

Posted: Sat Jun 02, 2007 2:07 pm
by Weirdan
Does PHP 5 support polymorphism like C#?
It does. Refer to the previous posts in this thread.
Then Please give me an example,Like C# example
Do it yourself or read the manual.

Posted: Sat Jun 02, 2007 3:18 pm
by Christopher
abalfazl wrote:Does PHP 5 support polymorphism like C#?

If it is,Then Please give me an example,Like C# example,An array that each member of that array can do something,And it uses inheritance...
Polymorphism is not really related to inheritance, although inheritance can be used to provide polymorphic methods. Ambush Commander described the essence of it above -- you call the same method, but in different types of objects. PHP is one of the least restrictive languages when it comes to polymorphism. Some languages require that methods are declared in a specific way to be polymorphic -- PHP has no such restriction.

Posted: Sat Jun 02, 2007 4:52 pm
by Jenk
Ambush Commander wrote:PHP is actually much more flexible with regards to polymorphism than C++ is, to the point where methods you wish to be polymorphic don't have to have a common abstract parent function to be interchangeable. Here is a simple case of PHP4-style polymorphism:

Code: Select all

class A {
 function foo() {echo 'A!';}
}
class B {
  function foo() {echo 'B!';}
}
function doStuff($obj) {
 $obj->foo();
}
$a = new A;
$b = new B;
doStuff($a);
doStuff($b);
That's not polymorphism..

Posted: Sat Jun 02, 2007 5:23 pm
by Christopher
Jenk wrote:That's not polymorphism..
Looks like it to me ... if it's not then what is it? and what is your definition of polymorphism?

Posted: Sat Jun 02, 2007 5:35 pm
by Jenk
Polymorphism is the ability for an object to adapt to it's environment. Hence the popular examples of an object "Shape" being able to depict what shape it is by the number of sides provided as an argument.

AdoDB is an example of polymorphism in the real world - you provide the DB type at instantiation and it will behave the same for any DB it supports.

Posted: Sat Jun 02, 2007 7:36 pm
by superdezign
Jenk wrote:Polymorphism is the ability for an object to adapt to it's environment. Hence the popular examples of an object "Shape" being able to depict what shape it is by the number of sides provided as an argument.

AdoDB is an example of polymorphism in the real world - you provide the DB type at instantiation and it will behave the same for any DB it supports.
Sounds pretty similar to me.

Posted: Sat Jun 02, 2007 7:58 pm
by Christopher
Jenk wrote:Polymorphism is the ability for an object to adapt to it's environment. ... AdoDB is an example of polymorphism in the real world - you provide the DB type at instantiation and it will behave the same for any DB it supports.
That's what, but you don't explain how. The example given above does (as does you AdoDB example) -- polymorphic objects share the same properties and methods even though they are not the same class.

Posted: Sat Jun 02, 2007 8:04 pm
by alex.barylski
arborint wrote:
abalfazl wrote:Does PHP 5 support polymorphism like C#?

If it is,Then Please give me an example,Like C# example,An array that each member of that array can do something,And it uses inheritance...
Polymorphism is not really related to inheritance, although inheritance can be used to provide polymorphic methods. Ambush Commander described the essence of it above -- you call the same method, but in different types of objects. PHP is one of the least restrictive languages when it comes to polymorphism. Some languages require that methods are declared in a specific way to be polymorphic -- PHP has no such restriction.
Usually when disscussing polymorphic behavior in the context of OOP it has everything to do with inheritence. ;)

http://en.wikipedia.org/wiki/Polymorphi ... rogramming

This example:

Code: Select all

class A { 
 function foo() {echo 'A!';} 
} 
class B { 
  function foo() {echo 'B!';} 
} 
function doStuff($obj) { 
 $obj->foo(); 
} 
$a = new A; 
$b = new B; 
doStuff($a); 
doStuff($b);
Although it's polymorphic in nature it's not OOP polymorphism. It's simply calling a function with a known interface by passing an object to function doStuff().

Polymorphism is difficult to comprehend in the context of PHP as it relies pretty heavily on the concept of a "type" which PHP does not really force.
Jenk wrote:Polymorphism is the ability for an object to adapt to it's environment. Hence the popular examples of an object "Shape" being able to depict what shape it is by the number of sides provided as an argument.
First sentance I would agree with, although it's vague. Boldified section, I'm not sure I agree with but I'm also not sure I understand what your saying. What do arguments have to do with polymorphism? Are you sure your not confusing that with function overloading? In which case, thats not polymophism in the strictess OOP sense either.

C++ (probably the best known and widely used OOP language - which is why I use it as a template for all OOP disscussion) offers several ways of making your code "polymorphic" in nature:

1) Virtual functions - what were disscussing here I believe
2) Operator overloading
3) Function overloading

The latter two I do not believe are considered nessecary for polymorphic OOP.

I am not sure I agree with AdoDB being polymorphic - although I am not terribly familiar with it's inner workings or implementation. I was under the impression is was simply and adapter or abstraction layer - which isn't polymorphism.

http://www.devshed.com/c/a/PHP/Abstract ... n-PHP-5/2/

Cheers :)

Posted: Sat Jun 02, 2007 8:14 pm
by John Cartwright
Hockey wrote:I am not sure I agree with AdoDB being polymorphic - although I am not terribly familiar with it's inner workings or implementation. I was under the impression is was simply and adapter or abstraction layer - which isn't polymorphism.
Considering you can easily switch between database engines, and its respective abstraction, it seems polymorphic to me :wink:

Posted: Sat Jun 02, 2007 8:18 pm
by Ambush Commander
I like GoF's (Gang of Four) definition of polymorphism:
Moreover, dynamic binding lets you substitute objects that have identical interfaces for each other at run-time. This substitutability is known as polymorphism, and it's a key concept in object-oriented systems
Polymorphism is a design pattern used by OOP to achieve the basic idea: data as objects with associated behavior. I would argue that as long as polymorphism is used toward this end, it doesn't matter how it's implemented or what constraints are placed on it: it is polymorphism. I will give you many concessions: with my example, it's quite easy for someone to break polymorphism by adding extra methods and utilizing them inappropriately. But it's the spirit that counts.

Note that the Wikipedia page you referenced is not without dispute.
Considering you can easily switch between database engines, and its respective abstraction, it seems polymorphic to me
It doesn't make much sense to classify an entire library as polymorphic. I would say ADOConnection is polymorphic in nature.