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