Directory:

What is polymorphism?

Second, the benefits of polymorphism

Simple polymorphic cases

Necessary conditions for the use of polymorphism

1. A subclass must inherit from its parent

2. There must be a rewrite

3. A superclass reference points to a subclass instance

Realization of polymorphism

1. Rewrite the

2. Overloading

3. Interface implementation

conclusion

What is polymorphism?

So-called polymorphism is refers to the procedures defined in the reference variable is pointing to the specific type and referenced by the variable from method calls are not sure when programming, but during the program is run to determine, that is, a reference variable STLL can point to which class instance object, the reference variable from method calls the method to realize exactly is which class, It must be decided during the running of the program. Because the program is running to identify a specific class, so, don’t need to modify the source code, can make reference variables bound to the different class implements, leading to the reference call specific methods change, namely do not modify the program code can change the program of the specific binding in the runtime code, let the program can select multiple running state, this is polymorphism.

Simply put, polymorphism is the ability to have many different manifestations or forms of the same behavior.

Two: The benefits of polymorphism

Using polymorphism, we can well complete the decoupling and work of the code, strengthen the extensibility of the code, make the code more flexible, simplify the process without changing the original interface method, etc., to sum up:

  1. Reducing coupling
  2. Enhancements can be replaceable
  3. scalability
  4. Flexibility…

To use polymorphism, three necessary conditions must be met.

  • inheritance
  • rewrite
  • A parent class reference points to a child class

When a polymorphic method is invoked during code writing, the IDE first checks to see if the method is present in the parent class and compiles an error if it is not. If there is a parent class that calls a subclass method with the same name.

A simple case of polymorphism

The code is as follows (example) :

/** * Pay abstract class or interface */ public class Pay {public String Pay (); } /** * Pay extends Pay {@override public String Pay () {system.out.println (" Pay "); return "success"; }} /** * public class extends Pay {@override public String Pay () {system.out.println (" wechat Pay"); return "success"; }} /** * extends Pay {@override public String Pay () {system.out.println (); return "success"; }} public static void main(String[] args) {Pay Pay = new AliPay(); pay.pay(); Pay = new WeixinPay(); pay.pay(); // Test unionPay multimodal application pay = new YinlianPay(); pay.pay(); } ------> The following output is displayed: Alipay Pay wechat Pay UnionPayCopy the code

Necessary conditions for the use of polymorphism

Use “different ways to eat chicken” as an example of chestnut

First, we’ll give you a chicken:

Class Chicken{public void live(){system.out.println (" this is a Chicken "); } 1234Copy the code

1. A subclass must inherit from its parent

Subclasses must inherit from their parents because of the dependency inversion principle in accordance with the five basic principles of object orientation: abstraction does not depend on the concrete, and the concrete depends on the abstract. Since polymorphism is to be implemented, there must be one “abstract” class that defines “behavior” and several “concrete” classes that represent different forms or forms of behavior.

So we give a specific category – white cut chicken:

class BaiqieChicken extends Chicken{ }
1
Copy the code

But it’s not enough just to define a white cut chicken class, because here we can only reuse the attributes and behaviors of the parent class, without presenting a different form or shape of behavior.

2. There must be a rewrite

Overrides, simply understood as redefining a parent class method so that a parent class and a subclass behave differently on the same behavior. For example, we use the white cut chicken for chestnuts.

Class BaiqieChicken extends Chicken{public void live(){system.out.println (" this is a Chicken that is made of white cut Chicken "); }} 12345Copy the code

This completes the rewrite. The behavior defined in the live() method is different from that defined in the live() method for the chicken, which is a chicken with infinite possibilities, and the white chicken, whose fate is to be a white chicken.

But why is there a condition that a parent reference points to a subclass object?

3. A superclass reference points to a subclass instance

In fact, this condition is the Richter’s substitution principle in the five basic principles of object orientation, which simply states that a parent class can refer to a child class, but not vice versa.

When a chicken is chosen for the white cut, its fate is out of its control.

Chicken c = new BaiqieChicken();  
c.live();
Copy the code

Running results:

This is a chicken that's going to be white cutCopy the code

Why do we have this principle? Because a parent class is the “abstract” level for a child class, and a child class is the “concrete” level. Abstract can provide interfaces to concrete implementations, but why should concrete refer to abstraction? Furthermore, “subclass references refer to superclass objects” does not comply with the dependency inversion principle.

When a white-cut chicken wants to go back and choose its own fate, sorry, it’s already in the pot, can’t get out.

Realization of polymorphism

There are three ways to implement polymorphism: rewriting, overloading, and interface implementation. Although their implementation methods are different, their core is: different manifestations of the same behavior.

1. Rewrite the

Overrides, which means that the subclass redefines the parent class method, but the argument list and return value type of the subclass method must be the same as that of the parent class method. ** So it’s easy to think of overrides as subclasses redefining the core of a superclass method.

The code is as follows (example) :

class Chicken{ public void live(String lastword){ System.out.println(lastword); }} class BaiqieChicken extends Chicken{public void live(String lastword){system.out.println (" this Chicken says: "); System.out.println(lastword); }}Copy the code

The live() method of the chicken class is overwritten by the white-cut chicken class. The live() method has the same argument list and return value as its parent, but the body of the method is different.

2. Overloading

Overloading refers to the case where several methods in a class have the same name but different argument lists, and the return value can be the same or different method definition scenarios. It can also be understood simply as different manifestations of the same action (method).

The code is as follows (example) :

Class BaiqieChicken extends Chicken{public void live(){system.out.println (" this is a Chicken that is made of white cut Chicken "); } public void live(String lastword){system.out.println (" "); System.out.println(lastword); }}Copy the code

The two live() methods in the white-cut chicken class here, one with no parameters and one with parameters, have different descriptions of the live() methods in the white-cut chicken class, but they are both named live. Generally speaking, they have different forms of expression for sliced chicken.

3. Interface implementation

An interface is an abstract type that cannot be instantiated, but can be implemented. It is a collection of abstract methods. It is used to define a collection of methods, and the concrete implementation of methods is handed over to the concrete class that inherits the interface to define. So interfaces define methods, whose implementations are defined in concrete classes that inherit interfaces, and are different representations of the same behavior.

interface Chicken{ public void live(); } class implements Chicken{public void live(){system.out.println (" this is a Chicken that is made of white meat "); }} class ShousiChicken implements Chicken{public void live(){system.out.println (" implements Chicken "); }}Copy the code

As you can see from the above, both the white-cut and hand-torn chicken classes have their own unique definition of the live() method in the chicken interface.

conclusion

This article briefly introduces polymorphism, one of the three main features of object orientation, using the Java language and a few examples to make it easier to understand polymorphism. If this article is helpful to your understanding of polymorphism, please give me a thumbs up, it will be my biggest motivation ~