Understanding of the polymorphism (java)

Understanding of polymorphism:

Polymorphism refers to the particular type of reference variables defined in the program pointed to by the reference variables and the method call issued when programming is not determined, but is determined only during the program run, i.e. they going to a reference variable which points to the class examples of objects that reference variable method calls made in the end is which class method implemented, in order to be determined by the program is running. Since determine the specific class only when the program is running, so, without modifying the source code, you can make reference variable is bound to achieve a variety of different classes, resulting in specific reference to the method invocation will change, that does not modify the program code can change specific code to run when bound, so that the program can select multiple operating states, this is polymorphism.

 

Code appreciated: public class Wine {  

    public void fun1(){  

        System.out.println("Wine 的Fun.....");  

        fun2();  

    }  

      

    public void fun2(){  

        System.out.println("Wine 的Fun2...");  

    }  

}  

  

public class JNC extends Wine{  

    /** 

     * @Desc child class overrides the parent class method 

     * The method parent class does not exist, the upward transition, the parent class is not referenced method 

     * @Param the 

     * @return void 

     */  

    public void fun1(String a){  

        System.out.println("JNC 的 Fun1...");  

        fun2();  

    }  

      

    /** 

     * Subclasses override the parent class method 

     * When the parent class to sub-class call by reference fun2, call the method must be 

     */  

    public void fun2(){  

        System.out.println("JNC 的Fun2...");  

    }  

}  

  

public class Test {  

    public static void main(String[] args) {  

        Wine a = new JNC();  

        a.fun1();  

    }  

}

-------------------------------------------------

operation result:

Wine of Fun .....

JNC's Fun2 ...

From the results of running the program, we found that, a.fun1 () is the first to run in the parent class Wine fun1 (). Then run a subclass JNC in fun2 ().

So we have a summary of the multi-state is:

Points to the parent Subclass REFERENCE since upward transition, it can only access methods and properties of the parent class has, methods parent class does not exist and there is a subclass of this reference can not be used, although heavy the load method. If a subclass overrides certain methods of the parent class, when the calling these methods, these methods must be used (dynamic linking, dynamic invocation) subclass definition.

For only object-oriented, multi-state and multi-state running into compile time polymorphism. Wherein editing is static polymorphism, mainly refers overloaded methods, it is to distinguish between different functions depending on the parameter list, after editing by the changes to two different functions, not to mention at run-time polymorphism . The run-time polymorphism is dynamic, it is through dynamic binding to achieve, what we call polymorphism.

Guess you like

Origin www.cnblogs.com/mojiangzz/p/11410162.html