Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Dynamic-binding & Polymorphism?


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

    Default 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. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

Similar Threads

  1. Dynamic binding vs Polymorphism
    By Navatha in forum New To Java
    Replies: 8
    Last Post: 07-18-2013, 03:36 PM
  2. Polymorphism - do you use it?
    By N00Bie in forum New To Java
    Replies: 10
    Last Post: 02-15-2011, 04:42 PM
  3. dynamic binding
    By apparao in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 08:41 AM
  4. Polymorphism Help
    By AWPtic in forum New To Java
    Replies: 5
    Last Post: 04-06-2009, 04:13 PM
  5. Dynamic Binding
    By javarishi in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 11:17 AM

Posting Permissions

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