Java learning for a long time, it has been thought to be a notebook, they simply have to go to understand what the Java language logic, it began to do something today.
Java basic syntax much to say, and C ++ is not too bad.
Since Java fully object-oriented, so I think Java permissions modifiers is important, but also to master the feeling is not so easy.
Because I did not learn interface, the following permissions just talking in the class hierarchy.
Java permissions modifiers are summarized as follows:
private default protected public
Which, private of the most restrictive, by its modified variables and methods of this class can only use
The default is to be accessed within the same package
public is in any place can call
protected and can be called from within the present package subclasses, note that if a parent-child class is not in the same package, the only protected method can be called the parent class in subclasses, subclasses can not call.
The following code:
1 package testProtected.test; 2 3 public class Father { 4 protected int num; 5 protected void showNum(){ 6 System.out.println(num); 7 } 8 } 9 10 package testProtected.test; 11 12 public class Son1 extends Father { 13 public void main(){ 14 num = 2; 15 showNum(); 16 } 17 } 18 19 20 package testProtected.test2; 21 22 import testProtected.test.Father; 23 24 public class Son2 extends Father { 25 public void main(){ 26 num = 1; 27 showNum(); 28 } 29 } 30 31 package testProtected.main; 32 33 import testProtected.test.Father; 34 import testProtected.test.Son1; 35 import testProtected.test2.Son2; 36 37 public class main { 38 public static void main(String[] args) { 39 Father fa1 = new Father(); 40 Son1 son1 = new Son1(); 41 Son2 son2 = new Son2(); 42 fa1.showNum();//error,因为不在同一包 43 son1.showNum();//error 44 son2.showNum();//error 45 son1.main();//ok 46 son2.main();//ok 47 } 48 }
Of course, the situation may be more complex, where rookie posted a link to a tutorial:
https://www.runoob.com/w3cnote/java-protected-keyword-detailed-explanation.html