This is my code.
package alpha ; class A1 { static class A11 { private final // WHAT IS THE EFFECT OF THIS MODIFIER? void fun ( String caller ) { System . out . println ( "A11:\t" + caller ) ; } } static class A12 extends A11 { private void fun ( String caller ) { super . fun ( caller + caller ) ; } } public static void main ( String [ ] args ) { A12 a12 = new A12 ( ) ; a12 . fun ( "Hello" ) ; } }
I found that with or without the final mdifer in A1.A11, the program compiles and runs.
I can understand that without the final modifier, A1.A12 can see and thus override the fun method. This is personal, but they are in the same class, so there is no visibility.
I canβt understand why it works with the last modifier. Should you not prohibit overriding in A1.A12?
This is the result of the program working with the final modifier in place.
java alpha/A1 A11: HelloHello
If he simply ignored another fun method, then
- no compiler complained about super link
- A11 will not be output
java private final modifiers
emory Jul 28 2018-11-18T00: 00Z
source share