Understanding of java polymorphism

1, What is polymorphism

  1. The three characteristics of object-oriented are encapsulation, inheritance and polymorphism.
  2. Definition of polymorphism: parent class reference variables can point to subclass objects, allowing different objects to call the same method, that is, the same method can have different implementation forms according to different calling objects.
  3. The technology to realize polymorphism is dynamic binding, which means to judge the actual type of the referenced object during the execution of the method, and call its corresponding method according to its actual type.
  4. The role of polymorphism: eliminate the coupling relationship between types.
  5. In reality, there are many examples of polymorphism. For example, press ctrl+F   The action of key, if currently displayed under Word, is the search and replace function of Word; What pops up in vscode is the search and replace function of vscode. The same event executed in different objects will produce different results.

2, Three necessary conditions for the existence of polymorphism

      1. Succession;
      2. Rewrite;
      3. The parent class reference points to the child class object.

3, Polymorphic characteristics

  1. substitutability. Polymorphism is replaceable for existing code. For example, the polymorphic Circle class works equally with any other circular geometry, such as a torus.
  2. extensibility. Polymorphic code is extensible. Adding new subclasses does not affect the polymorphism, inheritance, and operation of other features of existing classes.
  3. Interface capability. Polymorphism is the realization that the parent class provides a common interface to the child class through method signature, and the child class completes or overwrites it. For example, define a parent class Shape, which specifies two interface methods that implement polymorphism, computerarea () and computeVolume(). Subclasses, such as Circle and Sphere, can complete or override these two interface methods in order to achieve polymorphism.

4, Exercises

public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Father a1 = new Father();
        Father a2 = new Son();
        Son b = new Son();
        Grandson1 c = new Grandson1();   
        Grandson2 d = new Grandson2();   
        			
        System.out.println(a1.show(a2)); // It's Father and Father
        System.out.println(a1.show(b)); // It's Father and Father
        System.out.println(a1.show(c));  // It's Father and Father
        System.out.println(a1.show(d));  // It's Father and Grandson2
        
        System.out.println(a2.show(a1));  // It's Son and Father
        System.out.println(a2.show(b));  // It's Son and Father
        System.out.println(a2.show(c));  // It's Son and Father
        System.out.println(a2.show(d)); // It's Father and Grandson2
        
        System.out.println(b.show(b));  //  It's Son and Son
        System.out.println(b.show(a1));  // It's Son and Father
        System.out.println(b.show(a2));  // It's Son and Father
        System.out.println(b.show(c));  // It's Son and Son
        System.out.println(b.show(d));   // It's Father and Grandson2
	}
}
class Father {  
    public String show(Grandson2 obj){  
           return ("It's Father and Grandson2");  
    }   
    public String show(Father obj){  
           return ("It's Father and Father");  
    }   
}   
class Son extends Father{  
        public String show(Son obj){  
               return ("It's Son and Son");  
        }  
        public String show(Father obj){  
               return ("It's Son and Father");  
        }   
}  
class Grandson1 extends Son{}   
class Grandson2 extends Son{}

Resolution:

         a1 and a2 are defined as the Father class. You can use the show(Grandson2) and show(Father) methods of the Father class; a2 is a polymorphic implementation. You can also use the show(Son) and show(Father) methods of Son class, and override the show(Father) method of Father class; b is the Son class, which inherits the Father class. You can use the show(Grandson2) and show(Father) methods of the Father class and the show(Son) and show(Father) methods of the Son class, and override the show(Father) method of the Father class

Keywords: Java Eclipse git Polymorphism

Added by jakem on Sat, 27 Nov 2021 01:09:59 +0200