Analysis of the difference between covering and hiding Java methods _java

Source: Internet
Author: User

As for the difference between hiding and overwriting, refer to the RTTI (run-time type identification), the Run-time polymorphism, when a parent class references a subclass object, see the following code I wrote:

Copy Code code as follows:

public class RunTime {

public static void Main (string[] args) {
Animal a = new Cat ();
System.out.println (A.A);
System.out.println (A.B);
A.voice ();
A.method ();

}

}

Class Dog extends Animal {
public int b = 3;
public static int A = 3;
public static void Method () {
System.out.println ("dog");
}
public void Voice () {
System.out.println ("dog called");
}
}

Class Cat extends Animal {
public int b = 4;
public static int A = 4;
public static void Method () {
System.out.println ("Cat");
}
public void Voice () {
System.out.println ("Cat bark");
}
}

Class Animal {
public int b = 0;
public static int A = 0;
public static void Method () {
System.out.println ("Animal");
}
public void Voice () {
System.out.println ("animal called");
}
}


The output results are:
0
0
Cat Bark
Animals

As you can see, when the parent class animal reference A to the subclass dog, Rtti automatically determines the true type of the reference at runtime, and when the subclass overrides the parent class's method, it directly invokes the subclass's method, printing out the "Cat's name", whereas a non-static method overridden in a subclass is overwritten. And the static method of the quilt class rewrite words is hidden, in addition, static variables and member variables are also hidden, and Rtti is only for overlays, not for shadows, so static variables A and non-static variable B and static method methods () are not passed through the RTTI, which is the reference to which class is called the static method, member variables, and here is a reference to the parent class animal, so you call the methods in the parent class animal and the member variables directly. So static Methods method (), static variable A and member variable B print the results all in the parent class. Print subclasses only with voice () of the overridden Non-static method.

Related Article

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.