1. What is polymorphic
Polymorphism is the ability of the same behavior to have many different manifestations or forms.
Polymorphism is the embodiment of multiple forms of objects.
In reality, for example, we press the F1 key for this action:
- If the current pop-up in the Flash interface is the help document as 3;
- If the word help is currently popping up under word;
- Windows Help and support pops up under Windows.
The same event occurs on different objects and produces different results.
2. Three prerequisites for polymorphic presence
- Inherited
- Rewrite
- Parent class reference to subclass object (parent P = new child ())
When calling a method using polymorphic mode, first check whether the method is in the parent class, if not, compile the error, and if so, call the subclass's same name method.
The benefits of polymorphism: The program can be well extended, and the objects of all classes can be universally handled.
Example:
Public classEmployee {PrivateString name; PrivateString address; Private intNumber ; PublicEmployee (string name, address string,intNumber ) {System.out.println ("Employee Constructor"); This. Name =name; This. Address =address; This. Number =Number ; } Public voidMailCheck () {System.out.println ("Mail The Cheque to:" + This. Name+ " " + This. Address); } PublicString toString () {returnName + "+ address +" "+Number ; } PublicString GetName () {returnname; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String newaddress) {address=newaddress; } Public intGetNumber () {returnNumber ; }}
Public classSalaryextendsemployee{Private DoubleSalary//Annual Salary PublicSalary (string name, string address,intNumberDoublesalary) { Super(name, address, number); Setsalary (Salary); } Public voidMailCheck () {System.out.println ("MailCheck method of the Salary class"); System.out.println ("Mail The Cheque to:" +GetName ()+ ", wages are:" +salary); } Public Doublegetsalary () {returnsalary; } Public voidSetsalary (Doublenewsalary) { if(Newsalary >= 0.0) {Salary=newsalary; } } Public DoubleComputepay () {System.out.println ("Calculate wages, Pay:" +getName ()); returnSalary/52; }}
Public class Virtualdemo { publicstaticvoid main (String [] args) { new Salary ("Employee A", "Beijing", 3, 3600.00); New Salary ("Employee B", "Shanghai", 2, 2400.00); System.out.println ("Call MailCheck with reference to Salary"); S.mailcheck (); System.out.println ("\ n call mailcheck--using Employee's Reference"); E.mailcheck (); }}
Operation Result:
Employee constructor functionEmployee constructor functionUse Salary reference calls MailCheck -- Span class= "Typ" >salary MailCheck method Span class= "PLN" > mailed cheque to: Employee A 3600.0< Span class= "PLN" > use employee Mailcheck-- Salary MailCheck method Span class= "pun" > mailed cheque to: Employee B 2400.0
Example Analysis:
-
instance, two Salary objects were instantiated: one using Salary reference s and the other using the Employee reference E.
-
When S.mailcheck () is called, the compiler will find MailCheck () in the Salary class at compile time, and the execution process JVM invokes the MailCheck () of the Salary class.
-
Because E is a reference to employee, the compiler goes to the employee class to find the MailCheck () method when you call the MailCheck () method of E.
-
At compile time, the compiler validates the statement using the MailCheck () method in the Employee class, but at run time, the Java Virtual machine (JVM) calls the MailCheck () method in the Salary class.
3. Late binding of methods
late binding of a method is also called a dynamic binding or runtime binding , which refers to calling the corresponding method at run time based on the type of the object.
In addition to the static method and the final method in Java (the private method belongs to the final method), all other methods are late-bound. So usually we don't need to set the method to late binding, it happens automatically.
As in the above example:
New Salary ("Employee B", "Shanghai", 2, 2400.00); E.mailcheck ();
At compile time, the compiler validates the statement using the MailCheck () method in the Employee class, but at run time, the Java Virtual machine (JVM) calls the MailCheck () method in the Salary class.
4. Summary
For polymorphism, the following points can be summed up:
- Object that uses a reference to the parent class type to point to the child class
- The reference can only call methods and variables defined in the parent class
- If a method in the parent class is overridden in a subclass, the method (late binding) in the subclass is called when the method is called.
Java Learning notes-polymorphic