Dynamic type and polymorphism

This is a discussion on "Dynamic type and polymorphism" within the PHP Forum section. This forum, and the thread "Dynamic type and polymorphism are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 6th, 2005, 07:18
Junior Member
Join Date: Sep 2005
Location: Beyond the seas there's a town,In hand of each child a branch of insight lies,I shall build a boat.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Dynamic type and polymorphism

Hello firends


From this site:

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




Quote:

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!
Reply With Quote

  #2 (permalink)  
Old Sep 6th, 2005, 10:03
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
Sounds a lot like homework to me.
Reply With Quote
  #3 (permalink)  
Old Sep 7th, 2005, 02:50
Tim356's Avatar
Reputable Member
Join Date: Nov 2003
Location: Australia
Age: 25
Posts: 331
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tim356
Same here...
Reply With Quote
Reply

Tags
dynamic, type, polymorphism

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the best way to achieve these type of sites ? marSoul Website Planning 4 Dec 11th, 2007 01:08
Assigning a value to Hidden Type. thanawala84 Web Page Design 1 Oct 13th, 2007 08:10
Using type 'Text' everywhere, wrong? @lun Databases 1 Aug 8th, 2007 18:46
what type of code big tim Starting Out 7 Apr 16th, 2007 17:59
How to setup this type of page? port80 Web Page Design 10 Feb 23rd, 2007 14:40


All times are GMT. The time now is 06:58.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43