Thinking in polymorphism in java3
People are often confused by other, non-object-oriented features of Java, like method overloading, which are sometimes presented as object-oriented. Don't be fooled: If it isn't late binding, it isn't polymorphism
Translation by text
People are always confused by another non-object-oriented feature of java, like method overloading. Sometimes it exists as object-oriented. Please don't be stupid. If there is no late binding (that is, dynamic binding during runtime), it is not polymorphic.
Therefore, what this passage wants to express is that polymorphisms must have dynamic binding, otherwise it is not polymorphism, and method overloading is not polymorphism (because method overloading is determined during the compilation period, and there is no dynamic binding in the later period, that is, the runtime period)
When these three conditions are met 1. There is inheritance 2. There is overridden 3. There must be a parent class reference pointing to a child class object
<span style="font-size:14px;"> Example: public class Address {private String name;public Address(String name){this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}Define the base class (abstract class): public abstract class Vehicle {abstract void go(Address address);}Car Implementation of base class: public class Car extends Vehicle{@Overridepublic void go(Address address){System.out.println("Car to " + address.getName());}}Plane implements the base class: public class Plane extends Vehicle{@Overridevoid go(Address address) {System.out.println("Plane to " + address.getName());}}Polymorphism in Driver: public void drive(Vehicle v){ ///Polymorphism, the parent class reference points to the subclass object, and the actual transmission is a subclass of the abstract class Vehicle, or implementation class, and the compiler will find the implementation method based on the specific implementation class. v.go(new Address("Hangzhou(abstract)")); ///This method is rewritten in the specific implementation}Test:public static void main(String[] args) {Driver d = new Driver();d.drive(new Plane()); //It is actually a Plane object, then the compiler will find the implementation of go in Plane, d.drive(new Car()); //It is actually a Car object, then the compiler will find the implementation of go in Plane}Output result: Plane to Hangzhou (abstract)Car to Hangzhou (abstract)</span>Polymorphism: refers to the different states of the same thing, such as: water. Water can have three states:
Gas, liquids and solids. Then the polymorphism in JAVA can also be understood as this meaning:
The technique to set the parent object to be equal to one or more of its child objects,
For example Parent=Child;
Polymorphism enables the reference of objects of different classes using the same class (parent class),
And perform the same operation in different ways depending on the referenced object.
Polymorphic implementation includes two ways: overloading and overwriting
For example: Animal a = new Tiger(); This is an old topic, haha...
The parent class reference points to the child class object. The Animal class contains an eat() method, and the Tiger class inherits from
Animal class, if the subclass rewrites the eat() method of the parent class, then when called, you can follow the child class.
The formal call of , is essentially the parent class method, but after the subclass is rewritten, it becomes another way, which is polymorphism.
The above is the brief discussion of what aspects of the implementation of Java polymorphism mainly reflects. I hope it will be helpful to everyone and support Wulin.com more~