[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 (implemented by the compiler and linker, if any) before the program executes is called early binding. Binding based on the type of the object at run time is called late binding, or run-time binding. 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 think that everything can happen in a polymorphic state. However, only normal method invocations can be polymorphic. If you access a domain field, this access will be parsed at compile time. Look at the following example:

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, any domain access operations are parsed by the compiler and therefore not polymorphic. In this example, different storage spaces are allocated for Super.field and Sub.field. In this way, the sub actually contains two fields that become field: his own and what it gets from the super. However, the default domain generated when referencing filed in a sub is not a super version of the Field field. Therefore, in order to get super.filed, the indicated Super.field must be displayed.

The problem seems easy to confuse, but it doesn't happen very often in practice. Because according to the design principle, we often set all the domains to private, so we cannot access them directly, only through methods.

Conclusion: The method is polymorphic, and the domain does not have multi-state access.


Static methods are also not polymorphic. Because a static method is associated with a class, not with a single object. Consider the following example 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 ()

Reference: "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.