Java pre-binding and late-bound usage examples _java

Source: Internet
Author: User

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 ()
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.