Java interview knowledge points (a) polymorphism

Polymorphic Overview

1. Definitions

Polymorphism refers to reference variables defined in the program pointed to by the particular type and reference variables sent by the method is called when the program 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, must be made in order to determine 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.


2. Examples :

class Base
{
    public void method()
    {
        System.out.println("Base");
    } 
}
class Son extends Base
{
    public void method()
    {
        System.out.println("Son");
    }
     
    public void methodB()
    {
        System.out.println("SonB");
    }
}
public class Test01
{
    public static void main(String[] args)
    {
        Base base = new Son();
        base.method();
        base.methodB();
    }
}

Not compiled by

About polymorphic problems can be roughly summarized as follows:
Compile look left, look to the right to run. Meaning compile time, look at the left there is no way to see the results when running new object is who, who is called.


Polymorphic Example

For example, you are a Bacchus, wine and soft spot. One day home and found a few cups on the table are installed inside the liquor, from the outside it is impossible for us to know what wine is, after only drinking to be able to guess what kind of wine. You a drink, which is Jiannanchun, Wuliangye drink it, drink alcoholic wine ... this is where we can be described as follows:

      酒 a = 剑南春

      酒 b = 五粮液

      酒 c = 酒鬼酒

Here is the performance of the polymorphism. Jiannanchun, Wuliangye, alcoholic liquor are subclasses of wine, but we can refer to different subclasses of a superclass by this wine, which is polymorphic - we only at run time will know the specific reference variable points examples of objects.

Indeed, to understand the multi-state we have to understand what is "upward transition." In winded here: In the above example drinking wine (Win) is the parent class, Jiannanchun (JNC), Wuliangye (WLY), alcoholic liquor (JGJ) subclasses. We define the following code:

      JNC a = new  JNC();

For this we are very easy to understand code is nothing more than an example of an object that Jiannanchun thing! But this?

      Wine a = new JNC();

Here we understand this, Wine herein defines a type a, which points to the object instance JNC. Since JNC Inheritance and Wine, it can automatically upcast JNC Wine, it is a point JNC instance of an object. Doing so there is a very big advantage, in succession we know subclass is an extension of the parent class, it can provide than the parent class is more powerful, if we define a pointer to the parent class subclass reference type, then it apart able to refer to the common parent class, you can also use a subclass of powerful features.

But the upward transition, there are some shortcomings, it is that it will inevitably lead to the loss of some of the methods and properties, which led to we can not get them. Therefore, reference to the parent class type can be called the father of all the properties and methods defined in the class, for there is only the subclass methods and properties it far behind (ie: Compile look left, look right of the run)


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 子类重载父类方法
     *        父类中不存在该方法,向上转型后,父类是不能引用该方法的
     * @param a
     * @return void
     */
    public void fun1(String a){
        System.out.println("JNC 的 Fun1...");
        fun2();
    }
    
    /**
     * 子类重写父类方法
     * 指向子类的父类引用调用fun2时,必定是调用该方法
     */
    public void fun2(){
        System.out.println("JNC 的Fun2...");
    }
}

public class Test {
    public static void main(String[] args) {
        Wine a = new JNC();
        a.fun1();
    }
}
-------------------------------------------------
Output:
Wine 的Fun.....
JNC 的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 ().

Analysis: fun1 (String a) the subdivision in this procedure overrides the superclass JNC Wine fun1 method (), rewrite fun2 (), and after the overload fun1 () method is not the same, since the parent class Without this method, the method will be lost after the upward transition, the implementation of the JNC Wine type references are not cited fun1 (String a) method. The JNC subclass overrides fun2 (), then the point of Wine JNC JNC reference calls in fun2 () method.


So for us polymorphism can be summarized as follows:

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 the object-oriented, multi-state multi-state and 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/xieshijie/p/11410812.html