Overloading and overriding instance code for Java functions _java

Source: Internet
Author: User

In Java, polymorphism is divided into two kinds of compile-time polymorphism (overload) and Run-time polymorphism (rewrite), compile-time polymorphism is called forward binding, Run-time polymorphism is called after binding.

Here is an example to illustrate:

public class Overloadandoverwrite {public static void main (string[] args) {A a1 = new A (); 
    A A2 = new B (); 
    b b = new B (); 
    c C = new C (); 
    D d = new D (); 
    System.out.print ("A1.print (A1):"); 
    A1.print (A1)//Output A and a System.out.print ("A1.print (b):"); 
    A1.print (b);//Output A and a: the reason is that a method with a parameter of B is not present in a, because B is a System.out.print inherited from a ("A1.print (c):"); 
    A1.print (c);//Output A and a: the reason is that a method with a parameter of C is not present in a, so the method with parameter A is invoked because C is inherited from B, and B is inherited from a System.out.print ("A1.print (d):"); 
    A1.print (d);//Output A and D: The reason is that a method with the parameter d is present in a, so the method System.out.print ("A2.print (b)") is invoked; A2.print (b);//output B and A: The reason is that the first entry is a, first see if there is a print method of parameter B in a, found that there is no way to have the parameter A, because B is inherited from a, found that there is such a method, 
    So again see if B has overridden this method and found that there is a re, direct call to this overridden method in B System.out.print ("A2.print (c):"); A2.print (c);//output B and A: The reason is that the first entry is a, first see if there is a C-parameter print method, found that there is no way to find a parameter B, because C is inherited from B, found that there is no such method, Then look for the print method with the argument "a", because b inherits from a and discovers that there is such a method, then look again in B to see if there is any rewriting of this method, and find that there is a new, direct call to this overridden method in B System.out.print ("A2.print (d):"); 
    A2.print (d)//output A and D: The reason is that the entry is a, see a method with the parameter D in a, see again B does not rewrite this method, so output the result of this method in A; System.out.print ("A2.print (A2):"); A2.print (A2);//output B and A; The reason is that the type of A2 is a, so it calls the Print method with a parameter of a, but a2 to the right of the new is B, so because B has a method of parameter A, so it uses the method System.out in B. 
    Print ("B.print (b):"); 
    B.print (b);//output B and b; reason: The inlet is B, so view B does not exist the print function with a parameter of B, there is a direct output; 
    System.out.print ("B.print (c):"); B.print (c);//output B and b; Reason: The entry is B, so view B with a print function that does not exist in C, see that it does not exist, then view the print function that does not exist with a parameter B, and find that it exists, and that the method is not overridden in C, then output directly; 
    One thing to note is that you also need to see if there is a print method with the parameter C in a, because B inherits from a, and some of the results come in this way, so that the output will change to a and C System.out.print ("B.print (d):"); 
  B.print (d);//output A and D; reason: The inlet is B, although there is no print function with the parameter D in B, the print function with the parameter D in B inherits from A,a, so the output is the result of the parameter d in a; 
  Class A {public void print (a) {System.out.println ("A and a"); 
  public void print (d d) {System.out.println ("A and D"); 
}//public void print (c c)//{//System.out.println ("A and C"); Class B extends A {public voidPrint (b b) {System.out.println ("B and B"); 
  public void print (a) {System.out.println ("B and a");  Class C extends b{} class D extends c{}

Here's what you need to explain:

For A a2 = new B ();

If you print out the A2 separately, the print result is b@ (hash code), not a@ (hash code), but this does not mean that the type of A2 is type B because when we call A2.print (A2) in the above program, the result of the output is B and a instead of a and a ( If you assume that A2 is type B, you should call the Print method with the argument B in Class A, because there is no such method, and then a method that calls the argument "a" in the second should output a and a, because B is a subclass of a.

The above is the rewrite and overload of the instance Code analysis, I hope that the Java learning to help students.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.