About early binding late binding in Java (Dynamic binding)

Source: Internet
Author: User

Early binding, which is tied to compile-time type before execution of the program, is less expensive to call, such as a C language with only a pre-binding method call.
Late binding refers to binding at run time based on the type of the object, also called dynamic binding or runtime binding. Implementing late binding requires some kind of mechanism support so that at runtime you can judge the type of the object, and the invocation overhead is larger than the previous binding.

The static method and the final method in Java (private belongs to the final method, the detailed explanation see "Java programming thought") belongs to the early binding, the subclass cannot rewrite the final method, and the member variables (both static and non-static) are also early bound. In addition to the static method and the final method (the private belongs to the final method) other methods belong to late binding, the runtime can judge the type of the object to bind. The verification program is as follows:

Class base{//member variables, subclasses also have the same member variable name as public String test= "Base Field";    Static methods, subclasses also have static methods of the same signature public static void Staticmethod () {System.out.println ("Base Staticmethod ()");    }//Subclasses will overwrite this method with 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 ()");    }//The value of the output member variable to verify that it is a pre-binding.    public static void Testfieldbind (base base) {System.out.println (base.test);    }//static method, verifying that it is a pre-binding. public static void Teststaticmethodbind (base base) {//the static method test () from the type Base should is ACC        Essed in a static means//using Base.test () is more reasonable, where this representation is used for a more intuitive demonstration 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 fieldbase staticmethod () Derive notstaticmethod () */

About early binding late binding in Java (Dynamic binding)

Related Article

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.