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.
Printable View
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.
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;
}
}
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.