1. Understanding polymorphism:
It can be understood as a variety of forms of a transaction.
2. What is polymorphism?
Object polymorphism: references to parent classes point to objects of subclasses (or references assigned to parent classes by objects of subclasses)
It can be directly applied to abstract classes and interfaces.
3.Java reference variables have two types:
Compile-time type and run-time type.
(1) The compile-time type is determined by the type used to declare changes.
(2) The runtime type is determined by the object actually assigned to the variable.
Abbreviation: When compiling, look to the left; when running, look to the right.
>> If compile-time type and run-time type are inconsistent, Polymorphism of objects occurs.
>> In polymorphic cases, "Look left": Look at the reference of the parent class (there is no child-specific method in the parent class)
Look to the right: Look at the objects of the subclasses (the method that the subclasses actually run to override the parent class)
Calls to normal methods
Person e = new Person(); e.getInfo(); Student e = new Student(); e.getInfo();
Virtual method invocation (in polymorphic cases)
In the case of polymorphism, the method of the parent class is called a virtual method. The parent class dynamically calls the method belonging to the subclass according to the different subclass objects assigned to it. Such method calls are not deterministic at compile time.
Person e = new Student(); e.getInfo(); //Call the getInfo() method of the Student class
Compile-time type and run-time type
The compile time e is Person type, and the method invocation is determined at run time, so the getInfo() method of the Student class is invoked. Dynamic binding
Examples of polymorphism
public class AnimalTest { public static void main(String[] args) { AnimalTest test = new AnimalTest(); test.func(new Dog()); test.func(new Cat()); } public void func(Animal animal) { animal.eat(); animal.shout(); } public void func(Dog dog) { // If there is no polymorphism, you can only write one by one. What type of object do you declare? Only new type of object can show the benefits of polymorphism. dog.eat(); dog.shout(); } public void func(Cat cat) { cat.eat(); cat.shout(); } } class Animal { public void eat() { System.out.println("Animal:Eat"); } public void shout() { System.out.println("Animal:call"); } } class Dog extends Animal { public void eat() { System.out.println("Dogs eat bones"); } public void shout() { System.out.println("Wang Wang Wang"); } } class Cat extends Animal { public void eat() { System.out.println("Cats eat fish"); } public void shout() { System.out.println("cat"); } }
3. Overloading and rewriting of methods
Definition details of both: Brief
From the point of view of compilation and operation:
Overload means that multiple methods with the same name are allowed, and the parameters of these methods are different. The compiler modifies the names of methods with the same name according to the parameter tables of different methods. For compilers, these same-name methods become different. Their call addresses are bound at compile time. Java overloading can include both parent and child classes, that is, subclasses can overload methods with different parameters of the same name of the parent class.
So: for overloading, before method invocation, the compiler has determined the method to be invoked, which is called "early binding" or "static binding";
For polymorphism, only at the moment of method invocation can the compiler determine the specific method to be invoked, which is called "late binding" or "dynamic binding".
To quote Bruce Eckel: "Don't be silly. If it's not late binding, it's not polymorphic."
4. Is polymorphism compile-time or run-time? How to prove it?
import java.util.Random; //Interview question: Is polymorphism compile-time behavior or run-time behavior? //Runtime behavior //The proof is as follows: class Animal { protected void eat() { System.out.println("animal eat food"); } } class Cat extends Animal { protected void eat() { System.out.println("cat eat fish"); } } class Dog extends Animal { public void eat() { System.out.println("Dog eat bone"); } } public class InterviewTest { public static Animal getInstance(int key) { switch (key) { case 0: return new Cat(); case 1: return new Dog(); default: return new Sheep(); } } public static void main(String[] args) { int key = new Random().nextInt(3); System.out.println(key); Animal animal = getInstance(key); animal.eat(); } }