Java hiding and overwriting

Source: Internet
Author: User

Recently reviewed the Java Foundation, covering and hiding this problem for a long time, after the actual writing and verification, concluded as follows


First of all there are two cases of hiding
Together in a class, if you define the instance variable A and the variable A in the method, a in the method hides the instance variable A, that is, the call in the method can only see a that is declared in the method
In a two-inheritance relationship, static methods static variables and member variables that are overridden by the quilt class are hidden.
With respect to overrides, it is the polymorphism of our daily understanding that if the subclass defines the same non-static method as the parent class, it overrides the method.

So what's the reason for this rule?
To mention the RTTI (run-time type identification) run-time type check, that is, the run-time polymorphism is only for overrides, that is,
If a reference to a parent class is directed to an instance of a subclass, the JVM does not dynamically check the actual type of the reference, and then calls the subclass's method, by overriding the case.
static method static variables and member variables are the methods and variables that refer to which class to invoke.

eg

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 ("Barking Dog");
}
}

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 barks");
}
}

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 ("Animals are called");
}
}

The output is:
0
0
Cat Bark
Animals

Java hiding and overwriting

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.