My Java Note----bindings

Source: Internet
Author: User

# My Java Note----bindings
# Victor
# 2016.06.07

JAVA Bindings

1. Properties
Properties in Java are bound to classes (statically bound). If the child class and the parent class have the same properties, the parent class hides its properties.

2. Methods

2.1 Program Bindings:
Binding refers to the invocation of a method associated with the class (method body) in which the method resides. For Java, bindings are divided into static and dynamic bindings, or pre-and late-binding.

2.2 Static bindings:
The method is already bound before the execution of the program (that is, it is known in the process of compiling the method in which class the method is in), which is implemented by the compiler or other linker. For example: C.
For Java simple can be understood as the program compile time of the binding; In particular, the Java method is only final,static,private and the constructor method is pre-binding.

2.3 Dynamic Bindings:
Late binding: Binds at run time based on the type of the specific object.
If a language implements late binding, it must also provide mechanisms to determine the type of the object during run time and invoke the appropriate method, respectively. That is, the compiler still does not know the type of the object at this point, but the method invocation mechanism can investigate itself and find the correct method body. Different languages have a difference in how late binding is implemented. But at least we can think of it: they all have special types of information in the object.

The process of dynamic binding:
1 > The virtual machine extracts the actual type of object from the method table;
2 > virtual machine search method signature;
3 > Call method.

<NOTE>
The understanding of final,static,private and construction methods is early binding:
The <1> final method can inherit from the quilt class, but cannot be overwritten. Therefore, the parent class should be bound. (Private belongs to final method )
<2> construction methods cannot be inherited, so it is also possible at compile time to know which class the constructor method belongs to.
The <3> static method binds to the class at compile time, and subclasses can inherit, but cannot overwrite, only hide. The static method is independent of the object and is related only to the class. (The difference between hiding and overriding is that when a subclass object is converted to a parent object, it is able to access the hidden variables and methods of the parent class and cannot access the method that the parent class is overridden).

<CODE>

 public  class   base { public  String name =" base class "
   
    ;  
    public  
    static  
    void  
     Staticmethod () {System.out.println (" Base Staticmethod () "
    );  
    public  
    void  
     Commonmethod () {System.out.println ( "Base Commonmethod ()" 
    );  
    public  
     String GetName () { Span style= "color: #0000ff;"      >return  
     name; }  }
   
Base Class
 Public classDeriveextendsbase{ PublicString name = "Derive class";  Public Static voidStaticmethod () {System.out.println ("Derive Staticmethod ()"); }     Public voidCommonmethod () {System.out.println ("Derive Commonmethod ()"); }     PublicString GetName () {returnname; }  }
Derive Class
 Public classTestblind {//The value of the output member variable, which verifies that it is a pre-binding.      Public Static voidTestfieldbind (base base) {System.out.println (base.name); }    //a static method that verifies that it is a pre-binding.      Public Static voidTeststaticmethodbind (base base) {Base.staticmethod (); }    //call a non-static method to verify that it is late-bound.      Public Static voidTestnotstaticmethodbind (base base) {Base.commonmethod (); }     Public Static voidMain (string[] args) {Derive d=NewDerive (); Base b=NewDerive ();        Testfieldbind (d);        System.out.println (D.getname ());        Teststaticmethodbind (d);                Testnotstaticmethodbind (d);        Testfieldbind (b);        System.out.println (B.getname ());        Teststaticmethodbind (b);    Testnotstaticmethodbind (b); }}
Test Class
Base classbase Staticmethod () Derive commonmethod () Derive classbase classbase staticmethod () Derive Commonmethod () Derive class
OutPut

My Java Note----bindings

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.