Java Programming Ideas-polymorphic

Source: Internet
Author: User

In addition to the static method and the final method in Java (the private method belongs to the final method), all other methods are dynamic bound and run-time bound.

 Public classtest{Private voidf () {System.out.println ("Private F ()"); }     Public Static voidMain (string[] args) {Test T=NewDerived ();    T.F (); }}classDerivedextendstest{ Public voidf () {System.out.println ("Public f ()"); }}/*private F () Private method is automatically considered the final method, there is no polymorphic, static binding, the method of calling the base class directly*/

Only normal method calls can be polymorphic, and if you access a domain directly, the access will be parsed at compile time, with no polymorphic

classsuper{ Public intfield = 0;  Public intGetField () {returnfield;}}classSubextendssuper{ Public intField =1;  Public intGetField () {returnfield;}  Public intGetsuperfield () {return Super. field;}} Public classtest{ Public Static voidMain (string[] args) {Super sup=NewSub (); System.out.println (Sup.field+"  "+Sup.getfield ()); Sub Sub=NewSub (); System.out.println (Sub.field+ "" "+sub.getfield () +" "+Sub.getsuperfield ()); }}/*0 1 0*/

If a method is static, its behavior is not polymorphic.

classsuper{ Public Static voidGetField () {System.out.println ("Super"));}}classSubextendssuper{ Public Static voidGetField () {System.out.println ("sub");}} Public classtest{ Public Static voidMain (string[] args) {Super sup=NewSub ();    Sup.getfield (); }}/*Super*/

Java Programming Ideas-polymorphic

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.