Dynamic type and polymorphism

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
abalfazl
Forum Commoner
Posts: 71
Joined: Mon Sep 05, 2005 10:05 pm

Dynamic type and polymorphism

Post by abalfazl »

Hello firends


From this site:

http://www.artima.com/designtechniques/interfaces3.html




Getting more polymorphism





Interfaces give you more polymorphism than singly inherited families of classes, because with interfaces you don't have to make everything fit into one family of classes. For example:


interface Talkative {
void talk();
}
abstract class Animal implements Talkative {
abstract public void talk();
}
class Dog extends Animal {
public void talk() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
public void talk() {
System.out.println("Meow.");
}
}
class Interrogator {
static void makeItTalk(Talkative subject) {
subject.talk();
}
}

Given this set of classes and interfaces, later you can add a new class to a completely different family of classes and still pass instances of the new class to makeItTalk(). For example, imagine you add a new CuckooClock class to an already existing Clock family:


class Clock {
}
class CuckooClock implements Talkative {
public void talk() {
System.out.println("Cuckoo, cuckoo!");
}
}

Because CuckooClock implements the Talkative interface, you can pass a CuckooClock object to the makeItTalk() method:


class Example4 {
public static void main(String[] args) {
CuckooClock cc = new CuckooClock();
Interrogator.makeItTalk(cc);
}
}

With single inheritance only, you'd either have to somehow fit CuckooClock into the Animal family, or not use polymorphism. With interfaces, any class in any family can implement Talkative and be passed to makeItTalk(). This is why I say interfaces give you more polymorphism than you can get with singly inherited families of classes




Java is static type,And PHP is dynamic type.


I think that example was about how to use polymorphism without

inheritance,By using interface

But How is it possible using polymorphism without inheritance and interface?

How does duck type allow deeper polymorphism?(please give me an example in PHP)


What is relationship between ducktype and dynamic type?



Thanks in advance

GOOD LUCK!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"duck type" ? I don't see where you're pulling that out from.. :? Nor do I completely understand what you are asking for. :? :?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Duck Typing defined

interface example:

Code: Select all

interface iDuck { function quack() }

class Mallard implements iDuck {
function quack() { echo 'quack!'; }
}

class Hunter {
function shoot_at(iDuck $duck) {
  //darn we missed
  $duck->quack();
}
without interfaces:

Code: Select all

class Mallard {
function quack() { echo 'quack!'; }
}

class Hunter {
function shoot_at($duck) {
  if (in_array('quack',get_class_methods(get_class($duck))) {
  $duck->quack();
  } else {
    trigger_error("I don't know what I shot at, but it was not a duck");
  }
}
or more simply, because not passing an iDuck interfaced object to the first example will raise a fatal error, just try the call will have the same effect:

Code: Select all

class Hunter {
function shoot_at($duck) {
  //darn we missed
  $duck->quack();
}
[/url]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

heh.. first time I've heard/seen it referenced like that.. :lol:
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Inheritance would logically look like this

Code: Select all

class duckling extends Mallard { 
   function follow_mom(){
      $this->quack();
  }
}
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Re: Dynamic type and polymorphism

Post by dbevfat »

abalfazl wrote:...
I think that example was about how to use polymorphism without inheritance,By using interface

But How is it possible using polymorphism without inheritance and interface?

How does duck type allow deeper polymorphism?(please give me an example in PHP)
...
Without inheritance of some sort, that wouldn't actually be polymorphism per se. It would just be relying on that different objects implement the same behaviour. This allows for more flexibility, but introduces some others issues, specifically when objects don't introduce proper methods and properties.


Regards
Post Reply