1. Member
Join Date
Nov 2011
Posts
47
Rep Power
0

## Dynamic-binding & Polymorphism?

Can anyone explain to me what is meant by Polymorphism as it relates to
dynamic-binding? I searched the pdf of the core java textbook, and the teacher's online notes, and I could not find anything about dynamic binding.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

3. Member
Join Date
Nov 2011
Posts
47
Rep Power
0

## Re: Dynamic-binding & Polymorphism?

ok so would this be an example of dynamic-binding, and if so could you explain why?

public class MoveShapes5_1 {

public static void main(String args[])
{
public Moveable s[ ];

s = CreateShapes5_1(6);
for(int i=0; i<s.length; i++)
s[i].move(5*i,5*i);
}

public Moveable [ ] CreateShapes5_1(int size)
{
Moveable s[ ] = new Moveable[size];

for(int i=0; i<s.length; i++){
int rand = (int)(Math.random( )*2);
switch(rand){
case 0:
s[i] = new Point(2*i,i);
break;
case 1:
s[i] = new Triangle(i,i,2+i,2+i,5+i,5-i);
break;
}
}
return s;
}
}

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Dynamic-binding & Polymorphism?

It's just a matter of jargon.

The first sentence of the Wikipedia article says "In computer science, dynamic dispatch (also known as dynamic binding) is the process of mapping a message to a specific sequence of code (method) at runtime."

"s[i].move(5*i,5*i);" - in this line move() is the message being sent to each element of the array.

You didn't post the code for Moveable, Point and Triangle but somewhere in there are "specific sequence[s] of code" ie move() methods. The message causes one of these move() methods to happen (it is "mapped" to the method).

Moreover it is not until runtime that the specific mapping happens. This is made clear by the fact random() is used to determine whether you have a point or a triangle, and hence which of the two move methods will happen. And the result of random() is not known (or determined) in advance of the program being run.

So what we have is an example of a process of mapping a message to a specific sequence of code (method) at runtime, which was to be shown.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•