Recently, when I was writing a class tree, I found some questions about inheritance, so I wrote a simple test.
Code After analysis and discussion with csdn Forum friends, I figured out some inheritance mechanisms of Java, but still confused.
The test code is as follows: Public Abstract Class A {
Int I = 1 ;
Public Void Printi () {
System.Out. Println ("I ="+I );
}
}
Public Class B {
Int I = 2 ;
Public Static Void Main (string [] ARGs) {
B=NewB ();
B. printi ();
}
}
In the preceding code, abstract class A has a data member I, which provides the method pringi () for implementation, and entity Class B inherits a and has a data member I. My idea is that B inherits a and the method pringi (), So B is called. when printi () is used, the value of the data member variable I in B should be printed, but the reality is different from my idea. B. printi () prints the I value in A, which is 1.
I have been wondering whether to modify the code with the help of a csdn User: Public B extends {
{
I=2;
}
Public Static Void Main (string [] ARGs) {
B=NewB ();
B. printi ();
}
}
This time,ProgramIf I want to execute it, print the data member I in B.
So I found the following problem: B inherits a and a's printi () method, but the printi () method executes the following: print a's data member I, in the first code, B redefined the data member I inherited from a and assigned a value of 2. When printi () is called, I in a is printed.
In the second code, if I = 2 is put into the main method, the function is the same, that is, the data member I is not redefined, instead, it directly modifies the data member I inherited from a, so the printed I can be changed to 2 at this time.
Modify the program again as follows:
Public Abstract Class A {
Private I = 1 ;
Public Void Printi () {
System.Out. Println ("I ="+I );
}
}
Public Class B extends {< br> Public static void main (string [] ARGs) {< br> B = New B ();
B. printi ();
}
}
This compilation has received an error:
C: \ Classes \ A> javac B. Java
B. Java: 3: I has private access in
I = 2;
^
1 error
Obviously, because the data member in a is defined as private, B cannot inherit the I of A, so the above error occurs.
However, if I = 2 is removed, the program will be executed as usual. Because B calls the printi () method in A and printi () belongs to a, it can call the private data member I in.
Modify the program again as follows:
Public Class B extends {
Public Void Printi () {
System.Out. Println ("I ="+I );
}
Public Static Void Main (string [] ARGs) {
B=NewB ();
B. Geti ();
}
}
The above program view overwrites the method printi () in A, and the result receives the same error message as the previous program in the compilation phase.
After several program tests, it can be determined that the subclass inherits the method of the parent class. If this method is not overwritten, it is equivalent to directly calling the method of the parent class.
However, if int I = 1 is defined in a and INT I = 2 is defined again in B, then I, I should have been overwritten and modified like {I = 2;}, but why is the first program still 1?
In this way, I doubt the role of inheritance. If the inherited method can only call data members or methods of the parent class, is this method required to be overwritten as long as the inherited method needs to be called to the class data member or class member method? In this way, the role of inheritance seems to be much narrower and even disappointing to me.