Dynamic binding and static binding of Java

Source: Internet
Author: User

Dynamic Binding : The program binds the method and its owning class at run time.

Static binding : The program binds the method to its owning class at compile time, and if it is private, static, final, or constructor, the compiler knows exactly which method to invoke.

Base instance =new Son ()//instance is a base type that references a Son instance.

A run is a JV that binds a static method to its owning class, an instance method, and a referenced instance binding.

The process of dynamic binding:

1. First, the compiler searches the method table for the appropriate class (Son) and its parent (Father) based on the object's declaration type and method name, and finds all method methods that access the property as public.

There may be more than one method named method, except that the parameter type or number is different.

2. Then, follow the method's "signature" to find the exact matching method.

The name and parameter list of the method is called the signature of the method.

In previous versions of Java SE 5.0, overriding the methods of the parent class required the return type to be the same. The subclass now overrides the parent class's method, allowing its return type to be defined as a subtype of the original type.

Two, dynamic binding rules:

1. Subclasses override methods in parent class, call methods in subclasses

2. Subclasses do not override methods in the parent class, so find the appropriate method in the parent class

3. Dynamic binding is only a method for an object and is not valid for a property. Because the property cannot be overridden.

Iii. examples

1//subclasses have rewritten

Public classfather{2 Public voidmethod () {3System.out.println ("Parent class method:" + This. GetClass ());4 }5 }6 Public classSonextendsfather{7 Public voidmethod () {8System.out.println ("Subclass method" + This. GetClass ());9 }Ten Public Static voidMain (string[] args) { OneFather instance =NewSon (); A Instance.method (); - } - } the //Result: Subclass method: Class son

1 //subclasses have rewritten2  Public classfather{3      Public voidmethod () {4System.out.println ("Parent class method:" + This. GetClass ());5     }6 }7  Public classSonextendsfather{8      Public Static voidMain (string[] args) {9Father instance =NewSon ();Ten Instance.method (); One     } A } - //Result: Parent class Method: Class Son

1 //for Attributes2 3  Public classfather{4      PublicString name = "Father Attribute";5 }6  Public classSonextendsfather{7      PublicString name = "Child Properties";8 9      Public Static voidMain (string[] args) {TenFather instance =NewSon (); One System.out.println (instance.name); A     } - } - //Result: Father attribute

Dynamic binding and static binding of Java

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.