Page 1 of 1

Dynamic type and polymorphism

Posted: Mon Sep 05, 2005 10:17 pm
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!

Posted: Mon Sep 05, 2005 10:24 pm
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. :? :?

Posted: Mon Sep 05, 2005 11:01 pm
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]

Posted: Mon Sep 05, 2005 11:23 pm
by feyd
heh.. first time I've heard/seen it referenced like that.. :lol:

Posted: Tue Sep 06, 2005 1:48 am
by patrikG
Inheritance would logically look like this

Code: Select all

class duckling extends Mallard { 
   function follow_mom(){
      $this->quack();
  }
}

Re: Dynamic type and polymorphism

Posted: Tue Sep 06, 2005 3:26 am
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