Access to domain and static methods in Java is not polymorphic

Source: Internet
Author: User

1. Associating a method call with a method body is called

2. Compile-time binding (static) is the type of reference object that was determined during the program compilation phase

3. Run-time binding (dynamic binding) is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type

4. In addition to the static method and the final method (the private method belongs to the final method), all other methods are late-bound, and all methods in Java are dynamically bound to implement polymorphic

5. The behavior of accessing a domain is not polymorphic

Package Polymorphism;class Superfield {public int field = 1;public int GetField () {return field;}} Class SubField extends Superfield {public int field = 2;public int GetField () {return field;} public int Getsuperfield () {return super.field;}} public class Fieldpolymorphism {public static void main (string[] args) {superfield sup = new SubField (); System.out.println ("Sup.field =" + Sup.field + ", sup.getfield () =" + Sup.getfield ()); SubField sub = new SubField (); System.out.println ("Sub.field =" + Sub.field + ", sub.getfield () =" + Sub.getfield () + ", sub.getsuperfield () =" + SUB.G Etsuperfield ());}}

Output Result:

Sup.field = 1, Sup.getfield () = 2
Sub.field = 2, Sub.getfield () = 2, Sub.getsuperfield () = 1

When the subfield object is transformed into a super reference, any domain access operations will be parsed by the compiler, so it is not polymorphic, and subfield actually contains two fields called field: their own and inherited from the Superfield.

The domain is usually set to private, cannot be accessed directly, and cannot be inherited by invoking methods to access

6. Access to a static method is not polymorphic and is not associated with a single object

Package Polymorphism;class Super {public static String Staticmethod () {return "Super Staticmethod ()";}} Class Sub extends Super {public static String Staticmethod () {return "Sub Staticmethod ()";}} public class Staticpolymorphism {public static void main (string[] args) {Super sup = new Sub (); System.out.println (Sup.staticmethod ()); System.out.println (Sub.staticmethod ());}}

Output Result:

Super Staticmethod ()
Sub Staticmethod ()

Access to domain and static methods in Java is not 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.