Question:
Class Parent {
Private void method1 (){
System. out. println ("Parent's method1 ()");
}
Public void method2 (){
System. out. println ("Parent's method2 ()");
Method1 (); // ①
}
}
Class Child extends Parent {
Public void method1 (){
System. out. println ("Child's method1 ()");
}
Public static void main (String args []) {
Parent p = new Child (); // ②
P. method2 (); // ③
}
}
A reference p of the Parent type is declared at ②, which points to a Child object. Generally, p should have the post-binding function. ③ Call method2 (). This method2 () Definitely refers to method2 () inherited from the Parent class ().
But the problem is that method1 () is called in method2 () of the Parent class. Who is this method1? Parent or Child?
It seems difficult to judge, but the result is that the Parent method1 () is called ()? May I ask if method1 () is not a private member of Parent? How can a Child object be called?
Please take a look at the following example to get a solution --
Example 1:
Class Base5 {
Protected void doStuff1 (){
System. out. println ("Base-doStuff1 ");
}
Final void doStuff2 (){
System. out. println ("Base-doStuff2 ");
}
}
Class Child5 extends Base5 {
Protected void doStuff1 (){
System. out. println ("Child-doStuff1 ");
}
Protected void doStuff2 () {// ④
System. out. println ("Child-doStuff2 ");
}
Public static void main (String [] args ){
Base5 c = new Child5 ();
C. doStuff1 ();
C. doStuff2 ();
}
}
The compilation result is as follows:
D:/JavaEx/scjp/tst/tst18.java: 14: doStuff2 () in Child5 cannot override doStuff2 () in Base5; overridden method is final
Protected void doStuff2 (){
^
1 error
Because the doStuff2 () of Base5 is final, the Child5 cannot overwrite doStuff2 () in section 4, so there will be no post-binding polymorphism. That is:Parent classFinalThe method does not support post-binding polymorphism.
Example 2:
Class Base5 {
Protected void doStuff1 (){
System. out. println ("Base-doStuff1 ");
}
Private void doStuff2 (){
System. out. println ("Base-doStuff2 ");
}
}
Class Child5 extends Base5 {
Protected void doStuff1 (){
System. out. println ("Child-doStuff1 ");
}
Protected void doStuff2 (){
System. out. println ("Child-doStuff2 ");
}
Public static void main (String [] args ){
Base5 c = new Child5 ();
C. doStuff1 ();
C. doStuff2 ();
}
}
The compilation result is as follows:
D:/JavaEx/scjp/tst/tst18.java: 21: doStuff2 () has private access in Base5
C. doStuff2 ();
^
1 error
Because it is a private member, c. doStuff2 () produces errors, but more importantly, private is equivalent to final. If the final method in the parent class does not support post-binding, so is private. That is:Parent classPrivateThe method does not support post-binding polymorphism.
Only one step away
There is only one step away from answering the questions, and we have to add a basic knowledge. Whether or not a parameter is accepted, the member method of the class implicitly accepts a parameter, that is, this.
What is this? In Thinking in Java (3rd Edition), this is a reference to the current object.
Back to the original question:
Class Parent {
Private void method1 (){
System. out. println ("Parent's method1 ()");
}
Public void method2 (){
System. out. println ("Parent's method2 ()");
Method1 (); // ⑤
}
}
Class Child extends Parent {
Public void method1 (){
System. out. println ("Child's method1 ()");
}
Public static void main (String args []) {
Parent p = new Child ();
P. method2 ();
}
}
According to the knowledge added in the previous section, the fifth part is actually:
This. method1 ();
According to examples 1 and 2, the private method in the Parent class does not support post-binding polymorphism. Therefore, it calls the corresponding method strictly according to the referenced type, so it calls the method1 () of the Parent ().
When you change private before method1 () of the Parent to public/protected/friendly (default), the binding will be completed successfully and the Child's method1 () will be automatically called ().
Summary
The final method in the parent class cannot be overwritten in the subclass, so there is no post-binding polymorphism problem.
The private method in the parent class is similar to final. Although it can be redefined, It is not overwritten.
When the post-binding polymorphism problem occurs when the parent class contains private methods, non-private methods can still call the corresponding methods in real time based on the actual situation of the Post-binding objects. private methods, this feature is not supported. It only determines the method to be called based on the reference type of the called method.
Address: http://lslling.blogchina.com/1717345.html