Parent class subclasses have the same variable declaration

Source: Internet
Author: User

Public abstract class A {
int i=1;
Public void PrintI () {
System.out.println ("i=" +i);
  }   
}   
  
Public class B extneds a{
int i=2;
Public static void Main (string[] args) {
b b=new B ();
B.printi ();
  }   
}  

So, what is the value of the I that the console hits?
hehe, if a hard work can say the right result 1, then, the following section does not need to look down.

1, class of inheritance knowledge points
(1) Java does not support multiple inheritance, which means that subclasses can have at most one parent class
(2) subclasses inherit member variables and member methods that are not private in their parent class, as their own member variables and methods
(3) the member variables defined in the subclass and the member variables defined in the parent class are not inherited by the member variables in the parent class
(4) A member method defined in a subclass, and the name of the member method, the return type, and the number and type of arguments are exactly the same as a member method of the parent class, the member methods of the parent class cannot be inherited.

2, the answer is 2 said
The variable i in subclass B has the same name as the variable I in the parent class A, then the variable i in subclass B overrides the variable I with the same names in the parent class. When you access a variable in the parent class, the JVM casts the class to the parent class. Therefore, the printed result should be "i=2";

3, the emergence of ambiguity
The most important thing about ambiguity is that the variable i in subclass B overrides the two words of the variable i with the same name in the parent class. Here, I think these two words are easy to mislead. should be masked or hidden instead. Because the member variables of the parent class are not changed here.

4, the implementation of the JVM process
(1) The constructor method of subclass B is called, instantiating a B object, and the member of the B object is initialized
(2) The JVM implicitly calls the constructor of the parent class, instantiates a A object, and initializes the members of the A object.
(3) because the Printi () of the A object is not masked, the Printi () function of the A object is called.
Well, the member function of a here is of course to access its own member variable.

5. Super keyword
The Super keyword is used in Java to make a masked member variable or member method visible, or to refer to a masked member variable and member member method. Super is used in subclasses to access the masked members in the immediate parent class. The above code can also be written like this:
Java Code
Public abstract class A {
int i=1;
Public void PrintI () {
System.out.println ("i=" +i);
  }   
}   
Public class B extends a{
public int i=2;
Public void PrintI () {
Super.printi ();
    }   
Public static void Main (string[] args) {
b b= new B ();
B.printi ();
           
    }   
}  
Note:
in the process of instantiating a program from a JVM, if the child class is instantiated, it is necessary to instantiate the constructor of the parent class for inheritance.
C exteds D () {}
c C = new C ();
constructor, taking the relationship of the later son of the father.
The method , if the calling method is overridden by a subclass, invokes the method of the son, because at this point, the parent class has inherited the
related variables, and the method behavior, subclasses have relative independence, can be called independently.

D C = new C ();
the same is true of the JVM process.
first inherit the constructor of D (Gene), then load the C-alone constructor, and make corresponding calls to the relevant methods.

Parent class subclasses have the same variable declaration

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.