Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Binding Doubt

 
Nitin Kasat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Following is the code snippet.

class A{
public void foo(String... a)
{
System.out.println("A:foo");
}
}

class B extends A{
public void foo(String a)
{
System.out.println("B:foo");
}
public static void main(String[] arr){
A a=new B();
a.foo("test");
}
}

The result of running class B is
A:foo

According to dynamic binding concept, as there is exact match with String parameter in class B output should be B:foo. Because compiler first searches for exact match then widening,boxing and at last var-args. But when I run it on eclipse I got this output.Can anybody clarify me on this output?
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The func foo() which is written in Class B is overloaded and not overriden. So for overloaded function are identfied by the instance of compile type object and not run time object.
 
Nitin Kasat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Harshit. Now I got it.
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
func???
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"yggdrasill",
Please check your private messages regarding an important administrative matter.
-Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic