polymorphism

Java object-oriented programming has three main features: encapsulation, inheritance, and polymorphism encapsulation hides the internal implementation mechanism of a class, protects data, hides the internal details from the outside world, and only provides access to the methods it allows. Inheritance is used to reuse code and achieve upward transformation, and of course inheritance is also preparation for polymorphism. Polymorphism is the essence of Java object orientation.

What is polymorphism

Polymorphism refers to allowing objects of different subtypes to respond differently to the same message. In simple terms, it means using the same object to call the same method but do different things. It can also be understood as multiple manifestations of one thing. Polymorphism is divided into compile-time polymorphism and run-time polymorphism. For example: Method overloading: implements compile-time polymorphism method overwriting: implements polymorphism of a running polymorphism subclass object, which is the most common thing in Java polymorphism.

Conditions for realizing polymorphism

1. Method override, that is to say, there must be inheritance, only with inheritance, subclass overrides the parent class existing or abstract method 2. Object casting, which means that a reference from the parent class should point to a subclass object. Only then, the same reference calling the same method will give a different response.

Public void walk(){system.out.println (" walk "); } public void eat(){system.out.println (system.out.println); }Copy the code

If the subclass Man class does not override these two methods, then we do the following:

	Person man = new Man();
    man.walk();
Copy the code

Output result:

People walkCopy the code

In summary: the parent class refers to man, which refers to the subclass object. This is the polymorphism of the subclass object (also called upward transformation). Man can call the method of the parent class, provided that the subclass does not override the method of the parent class. If the subclass Man overrides the superclass method

Public void walk(){system.out.println (" men should walk tall "); } public void eat(){system.out.println (" men should eat more meat "); }Copy the code

Same call:

Person man = new Man();
man.walk();
Copy the code

The result:

Men should walk straightCopy the code

In summary: the subclass object overrides the superclass method, so man calls the superclass method and executes the subclass method. If the same parent object is applied to a different subclass object, it will respond differently. For example, subclass Woman overrides the parent method:

Public void walk(){system.out.println (" women should walk softly "); } public void eat(){system.out.println (" women should eat less meat and more fruits and vegetables ");} public void eat(){system.out.println (" women should eat less meat and more fruits and vegetables "); }Copy the code

Call method:

Person man = new Man();
Person woman = new Woman();
man.walk();
woman.walk();
Copy the code

The result is:

Men should walk tall and women should walk softlyCopy the code

To sum up: it should be clear what polymorphism is when you have the same parent class reference, and different subclass objects respond to the methods of the corresponding subclass object.

Method overloading and method overwriting

Method overloading and method overwriting are both ways of implementing polymorphism. The difference is that method overloading implements compile-time polymorphism, while method overwriting implements runtime polymorphism. Overloading occurs in the same class, and methods with the same name have different argument lists.

Note: method overloading has nothing to do with the return value, i.e. two different, same class, same method, different argument lists

Overriding a method occurs between a subclass and its parent class. Overriding requires that the subclass return a value, the method name, the argument list be the same as the parent class burst, the permission modifier be greater than the parent class, and that both methods be static or non-static.

Note: constructors cannot be inherited, so they cannot be overridden, but they can be overridden

Examples of the use of polymorphism

For example, the parent Person class has the following methods:

Public void show(Person Person){system.out.println (" this is the parent show method "); }Copy the code

The subclass Man overrides the subclass method:

Public void show(Person Person){system.out.println (" this is the subclass show method "); }Copy the code

If you instantiate a Man object and call Man’s show(Person Person) method, you should pass in the Person object, at which point you can pass in a reference to the parent object.

Person man = new Man();
man.show(man);
Copy the code

Superclass references point to different subclass objects and respond differently.

The classic case of polymorphism

From http://blog.csdn.net/thinkGhoster/archive/2008/04/19/2307001.aspx

    public class A {  
    public String show(D obj) {  
        return ("A and D");  
    }  
  
    public String show(A obj) {  
        return ("A and A");  
    }   
  
}  
  
public class B extends A{  
    public String show(B obj){  
        return ("B and B");  
    }  
      
    public String show(A obj){  
        return ("B and A");  
    }   
}  
  
public class C extends B{  
  
}  
  
public class D extends B{  
  
}  
  
public class Test {  
    public static void main(String[] args) {  
        A a1 = new A();  
        A a2 = new B();  
        B b = new B();  
        C c = new C();  
        D d = new D();  
          
        System.out.println("1--" + a1.show(b));  
        System.out.println("2--" + a1.show(c));  
        System.out.println("3--" + a1.show(d));  
        System.out.println("4--" + a2.show(b));  
        System.out.println("5--" + a2.show(c));  
        System.out.println("6--" + a2.show(d));  
        System.out.println("7--" + b.show(b));  
        System.out.println("8--" + b.show(c));  
        System.out.println("9--" + b.show(d));        
    }  
}  
Copy the code

The output is:

1--A and A  
2--A and A  
3--A and D  
4--B and A  
5--B and A  
6--A and D  
7--B and B  
8--B and B  
9--A and D  
Copy the code

The results are analyzed as follows:

System.out.println("1--" + a1.show(b)); A and A system.out. println("2--" + a1.show(c)); // A and A system.out. println("2--" + a1.show(c)); Println ("3--" + a1.show(d)); A and d system.out. println("4--" + a2.show(b)); // show(B) is A subclass-specific method, so it cannot be called. B is A subclass-specific method, so it cannot be called. Println and A system.out.println ("5--" + a2.show(c)); // Call c to show(c); // Call c to show(c); // Call c to show(c); Println and A system.out. println("6--" + a2.show(d)); A and D system.out.println ("7--" + b.how (B)); A and D system.out.println ("7--" + b.how (B)); Println ("8--" + b.how (c)); Println ("9--" + b.how (d)); A and d = A and d = A and d = A and dCopy the code

Case summary

Super.show (O) = super.show(O) = super.show(O) = super.show(O) = super.show(O) = super.show(O) = super.show(O) = super.show(O) Show (super) = A; show(super) = B; show(super) = A; This. Show (O), super.show(O), this.show((super)O), super.show((super)O).