Skip to main content

Implementing Dynamic Binding while Coding?

Posted by n_varun on August 21, 2008 at 7:14 AM PDT

Few weeks back, I had blogged at my other blog, about "Getting your basics right?". I don't know how many of you would agree, however the discussion that took place for filing an RFE, which eventually got filed as Issue #142112, was a long lasting one, and if people had clarity of concepts, then it might not have extended that long.

Anyways, I think you would like to comment on that. Read my blog for more details.

Here's the Usecase, which was sufficient to justify the filing of a RFE.

 6 public class UserImpl implements User {
 7 
 8     public void callCheck() {
 9         callImpl();
10         new UserImpl().callImpl();
11     }
12 
13     public void callImpl() {
14         System.out.println("Implementation Invoked...");
15     }
16 
17     public static void main(String[] args) {
18         new UserImpl().callCheck();
19 
20         // Dynamic Binding...
21         User anonUser = new UserImpl();
22         anonUser.callImpl();
23     }
24 }
25 
26 interface User {
27 
28     public void callImpl();
29 };

Clicking on callImpl() in line #22, doesn't navigate to the implemented version of callImpl(), i.e. line #13. Instead, user is navigated to line #28. So, this should not exist, hence someone filed a RFE.

Thanks for your time!

Related Topics >>

Comments

I can understand your code snippet, and I fully agree with this as I have already mentioned in my other blog pointed by this entry. Also, I mentioned there- Intelli J does that, and it shows all possible implementations, and prompts user to select one of them, for redirection, which I thought shouldn't happen as its not making sense. Dynamic binding concept is failed there, as user gets to see multiple impl's instead of one!

Damn it! Java.net's commenting system is very prehistoric!

Test: [code] User u = null; if (Random.nextBoolean()) { u = new UserImpl1(); } else { u = new UserImpl2(); } u.callImpl(); [/code]

It is not possible to always navigate to the right implementation, if there is more than one implementation of the interface in your code base. For example, consider this example: User u = null; if (Random.nextBoolean()) { u = new UserImpl1(); } else { u = new UserImpl2(); } u.callImpl(); Now if you go on the callImpl() invokation in the last line, and ask your IDE to take you to the implementation class, it can't do it correctly for you, because the implementation class is chosen randomly.