Java Dynamic binding

Source: Internet
Author: User

1. Dynamic binding

Associating a method call with the same method body is called a binding.

Binding is based on the type of the object at run time, called late-bound or run-time binding. In addition to the static method and final in Java

For example, the following defines a variable of shape type, which is a shape reference that, due to late binding, gives an object reference to its subclass circle, which is ultimately called the Circle.draw () method.

1  Packagecom.khlin.binding.test;2 3  Public classApp {4      Public Static voidMain (string[] args) {5Shape shape =NewCircle ();6 Shape.draw ();7     }8 }9 Ten classShape { One      Public voidDraw () { ASystem.out.println ("i m a shape ..."); -     } - } the  - classCircleextendsShape { -      Public voidDraw () { -System.out.println ("I m a circle ..."); +     } -}

Output: i m a circle ...

2. No Dynamic binding defects

any domain will be parsed by the compiler and therefore not polymorphic.

static methods are also not polymorphic.

Take a look at the following example:

1  Packagecom.khlin.binding.test;2 3  Public classApp {4      Public Static voidMain (string[] args) {5         //shape shape = new Circle ();6         //Shape.draw ();7 8Shape shape =NewTriangel ();9         /**direct access to the domain, resulting in 0, indicating that the domain cannot be dynamically bound*/TenSystem.out.println ("Shape Field[degreeofangels]:" One+shape.degreeofangels); A         /**call method, get 180, description is dynamic bound*/ -System.out.println ("Shape [Getdegreeofangels () method]:" -+shape.getdegreeofangels ()); the         /**static methods cannot be dynamically bound*/ - shape.print (); -         /**static domains also cannot be dynamically bound*/ -System.out.println ("Shape Field[lines]:" +shape.lines); +     } - } +  A classShape { at     /**the sum of internal angles*/ -      Public intDegreeofangels = 0; -  -     /**There are several sides*/ -      Public Static Final intLines = 1; -  in      Public intGetdegreeofangels () { -         returnDegreeofangels; to     } +  -      Public voidDraw () { theSystem.out.println ("i m a shape ..."); *     } $ Panax Notoginseng      Public Static voidprint () { -System.out.println ("Printing shapes ..."); the     } + } A  the classTriangelextendsShape { +     /**the sum of internal angles*/ -      Public intDegreeofangels = 180; $  $     /**There are several sides*/ -      Public Static Final intLines = 3; -  the      Public intGetdegreeofangels () { -         returnDegreeofangels;Wuyi     } the  -      Public voidDraw () { WuSystem.out.println ("i m a Triangel ..."); -     } About  $      Public Static voidprint () { -System.out.println ("Printing Triangel ..."); -     } -}

The output is:

Shape field[degreeofangels]:0
Shape [Getdegreeofangels () method]:180
Printing Shapes ...
Shape Field[lines]:1

Java Dynamic binding

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.