Late binding, which is binding at run time based on the type of the object, or dynamic or run-time binding. Implementation of late binding requires some kind of mechanism support to determine the type of object at run time, and the invocation overhead is larger than previous binding.
The static method and final method in Java are part of the early binding, and subclasses cannot override the final method, and member variables, both static and Non-static, also fall into the early binding. Except that the static method and the final method (private belongs to the final method) are late-bound, the runtime can determine the object's type to bind. The verification procedure is as follows:
Copy Code code as follows:
Class Base
{
Member variable, subclass also has the same member variable name
Public String test= "Base Field";
Static method, subclass also has a static method with the same signature
public static void Staticmethod ()
{
System.out.println ("Base Staticmethod ()");
}
Subclasses will overwrite this method
public void Notstaticmethod ()
{
System.out.println ("Base Notstaticmethod ()");
}
}
Public class derive extends Base
{
Public String test= "derive Field";
public static void Staticmethod ()
{
System.out.println ("Derive Staticmethod ()");
}
@Override
public void Notstaticmethod ()
{
System.out.println ("Derive Notstaticmethod ()");
}
Outputs the value of the member variable, verifying that it is an early bound.
public static void Testfieldbind (base base)
{
System.out.println (base.test);
}
static method to verify that it is an early bound.
public static void Teststaticmethodbind (base base)
{
The static method test () from the type Base should is accessed in a static way
Using Base.test () is more reasonable, and this representation is used here for a more intuitive presentation of early binding.
Base.staticmethod ();
}
Call a Non-static method to verify that it is late bound.
public static void Testnotstaticmethodbind (base base)
{
Base.notstaticmethod ();
}
public static void Main (string[] args)
{
Derive d=new derive ();
Testfieldbind (d);
Teststaticmethodbind (d);
Testnotstaticmethodbind (d);
}
}
/* Program output:
Base Field
Base Staticmethod ()
Derive Notstaticmethod ()
*/