Lesson 92. What is polymorphism

Posted by cruz610 on Mon, 16 Sep 2019 08:10:37 +0200

What is polymorphism in java?

There are two kinds of polymorphism: compile-time polymorphism: method overload; run-time polymorphism: JAVA runtime system decides which method to call according to the type of instance calling the method. (We usually talk a lot about runtime polymorphism, so polymorphism mainly refers to runtime polymorphism.) The above description considers that overload is also a manifestation of polymorphism, but polymorphism mainly refers to runtime polymorphism.

 

Runtime polymorphism:

1. Three characteristics of object-oriented: encapsulation, inheritance and polymorphism. From a certain point of view, encapsulation and inheritance are almost all prepared for polymorphism. This is our last concept and the most important point of knowledge.

2. Definition of polymorphism: Allowing objects of different classes to respond to the same message. That is to say, the same message can adopt many different behavior modes according to the different sending objects. (Sending a message is a function call)

3. The technology of realizing polymorphism is called dynamic binding, which refers to judging the actual type of the object referenced during execution and calling its corresponding method according to its actual type.

4. The role of polymorphism: eliminating the coupling between types.

5. In reality, there are numerous examples of polymorphism. For example, the action of pressing the F1 key, if the current pop-up under the Flash interface is the AS3 help document; if the current pop-up under Word is the Word help; in Windows pop-up is the Windows help and support. The same event happens on different objects and produces different results.

 

Three Necessary Conditions for the Existence of Polymorphism

1. Inheritance;

2. Rewrite.

3. The parent reference points to the child class object.

 

Benefits of polymorphism:

1. substitutability. Polymorphism is replaceable for existing code. For example, polymorphism works for the Circle class, as does any other circular geometry, such as a ring.

2. extensibility. Polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance of existing classes, and the operation and operation of other features. In fact, it is easier to get polymorphic functions by adding new subclasses. For example, on the basis of polymorphism of cones, hemispheres and hemispheres, it is easy to increase the polymorphism of spheres.

3. interface-ability. Polymorphism is achieved by means of method signature of superclasses, which provides a common interface to subclasses and is perfected or overwritten by subclasses. As shown in Figure 8.3. The superclass Shape in the figure specifies two interface methods to implement polymorphism, computeArea() and computeVolume(). Subclasses, such as Circle and Sphere, perfect or override these two interface methods in order to achieve polymorphism.

4. flexibility. It embodies flexible and diverse operation in application and improves the efficiency of use.

5. simplicity. Polymorphism simplifies the coding and modification process of application software, especially when dealing with the operation and operation of a large number of objects, this feature is particularly prominent and important.

 

Code:

 

Parent class:

package com.xuenixiang.duotai;



public class Xuenixiang1 {

protected String name;

private String address;



public Xuenixiang1() {

System.out.println("The parent class parameterless construct is called");

}

public Xuenixiang1(String name,String address) {

System.out.println("Parent class parameterized constructs are called");

}



public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}



}

Subclass:

package com.xuenixiang.duotai;



public class Xuenixiang2 extends Xuenixiang1 {

private int age;



public Xuenixiang2() {

System.out.println("Subclass parameterless constructs are called");

}



public Xuenixiang2(String name, String address,int age) {

//this();

//super(name,address);

this.setName(name);

this.setAddress(address);

this.age=age;

System.out.println("Subclass parameterized constructs are called");

}



public int getAge() {

return age;

}



public void setAge(int age) {

this.age = age;

}



public void setName(String Mingzi) {

this.name = Mingzi; // Subclasses override the setName of the parent class

System.out.println("Parent class setName Method is rewritten");

}



public void xuenixiang() {

System.out.println("Website name:" + this.getName());

System.out.println("Website address:" + this.getAddress());

System.out.println("Age of website:" + this.getAge());

}

}

Main method:

package com.xuenixiang.duotai;



public class Xuenixiang {



public static void main(String[] args) {

Xuenixiang2 xnx=new Xuenixiang2("Learning Reverse Forum","xuenixiang.com",1);

xnx.xuenixiang();

}



}

 

Topics: Java Windows