Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hiding Members (static methods /fields)

 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
static String onlyme="A.Only Me";
static String initStr = "A.Init";
String dynamic_bind_detect = "Dynamic Bind Detect";
public void local() // never called in this program
{
System.out.println("A.local() :"+initStr+":"+dynamic_bind_detect);
hi();

}
public void hi()
{
System.out.println("A.hi()");
}
}
public class OverloadStatic extends A
{
protected static int initStr = 10;
public int dynamic_bind_detect = 10000;

public void hi()
{
System.out.println("OverloadStaticField1.hi()");
}

public static void main(String args[])
{
OverloadStatic overload = new OverloadStatic();
System.out.println("overload.initStr :"+overload.initStr);
overload.local();
}
}

output is

/**

A.local() :A.Init ynamic Bind Detect

OverloadStaticField1.hi()

*/

As i know, hi() of OverrideStatic is called. 'this' reference is passed to local() of A, so finally dynamic method binding, class of object is checked and hi() of OverloadStatic is called.

Similarly is case of dynamic_bind_detect of OverloadStatic, but it is of A.

Could you please advise that why dynamic_bind_detect of OverrideStatic class is not displayed as the actual object is passing this reference implicitly into local() method call.
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this thread:

Only overridden instance methods are bind to run time polymorphism.
Others instance variables, static variables, static overridden (hidden) methods, and overloaded methods are all binded at compile time depending on the type of the reference variable and not on the object.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic