[Think in Java] Chapter 8th polymorphism

Source: Internet
Author: User

polymorphic

Encapsulation creates a new data type by merging features and behaviors.

The function of polymorphism is to eliminate the coupling relationship between types.


method Call Binding

Definition: Associating a method invocation with the same method topic is called a binding.

Binding before the execution of the program (if any, implemented by the compiler and linker) is called early binding. Binding is called late binding, or binding at execution time, depending on the type of the object. Polymorphism in Java is achieved through dynamic binding.

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


defects: Domain and Static methods

Once you understand the polymorphic mechanism, you may feel that all things can happen in a polymorphic state. However, only normal method invocations can be polymorphic. If you visit a domain field, the interview will be parsed at compile time. Look at the following examples:

Class Super{public int filed = 0;public int getfiled () {return filed;}} Class Sub extends super{public int filed = 1;public int getfiled () {return filed;} public int getsuperfiled () {return super.filed;}} public class Filedaccess {public static void main (String [] agrs) {Super sup = new Sub (); System.out.println ("Sup.field =" + sup.filed + ", sup.getfield () =" +sup.getfiled ()); Sub Sub = new sub (); System.out.println ("Sub.field =" + sub.filed + ", sub.getfield () =" +sub.getfiled () + ", sub.getsuperfield () =" + sub.ge Tsuperfiled ());}} Sup.field = 0, Sup.getfield () = 1sub.field = 1, Sub.getfield () = 1, Sub.getsuperfield () = 0

When a sub is transformed into a super reference, no matter what the domain access is resolved by the compiler, it is not polymorphic. In this example, different storage spaces are allocated for Super.field and Sub.field. In this way, the sub actually consists of two fields that become field: his own and what it gets from the super. However, the default domain generated when referencing filed in sub is not the Field field of the Super version number. Therefore, in order to get super.filed, the indicated Super.field must be displayed.

The problem seems to be very easy and confusing, but it doesn't happen very often in practice. As a result of the design principle, we often set all the fields to private, so we can not directly visit them, only through the method to access.

Conclusion: The method is polymorphic and the domain is not polymorphic.


Static methods are not polymorphic in the same way. Because a static method is associated with a class, not with a single object. Look at the sample below to digest the ^.^~:

Class Staticsuper{public static String Staticget () {return "Base staticget ()";} Public String Dynamicget () {return ' Base dynamicget () ';}} Class Staticsub extends Staticsuper{public static String Staticget () {return "Derived staticget ()";} Public String Dynamicget () {return ' Derived dynamicget () ';}} public class Staticpolymorphism {public static void main (String [] args) {staticsuper sup = new staticsub (); System.out.println (Sup.staticget ()); System.out.println (Sup.dynamicget ());}} Output:base staticget () Derived dynamicget ()

References: "Thinking in Java"

[Think in Java] Chapter 8th polymorphism

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.