Dynamic type and polymorphism
Posted: Mon Sep 05, 2005 10:17 pm
Hello firends
From this site:
http://www.artima.com/designtechniques/interfaces3.html
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!
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!