Java class access modifiers

1, the external access modifier java class are the following four:

public, default, abstract, final

// public,默认,abstract,final。
public class Test1 {}
class Test2{}
abstract class Test3{}
final class Test4{}

  

  • Where the public can access any location represents the class;
  • Do not write access modifier indicates the default, which can be accessed in the same package;
  • abstract class is an abstract class, the class can not be instantiated, i.e., can not be used Test3 test3 = new Test3 (); Test3 example of obtaining manner. If you want to use it you need to find a class that inherits the class;
  • This class represents the final can not be inherited by subclasses, the class is final class can no longer be inherited.

 

2, why can not modify it outside the class with private and protected?

1, private: private modified with outside class, indicating that the other classes outside the class can not be accessed, then this class definition would be meaningless, so the private inner class can only be modified. Class is an internal class outside, then the corresponding four access control modifiers: (private), with the package (default), the parent-child class (protected), the present position of any class (public). When a class uses internal private modifier, internal use only outside of this class.

2, protected: protected class, the same package or in different packages subclass can be accessed. If a class is unable to access the protected modified, outside class, so what inheritance. So with protected it is of no significance

 

Guess you like

Origin www.cnblogs.com/wgblog-code/p/11330624.html