The use of visibility modifiers in Java

后端 未结 8 2261
谎友^
谎友^ 2021-01-06 01:32
class Orange{

    Orange(){
    }

}

What is the difference between the usage of the modifier - in this case, package-private - in front of the cl

8条回答
  •  感动是毒
    2021-01-06 02:07

    Class modifiers work similarly to method modifiers. Public, private, final, abstract, etc. work.

    Public allows the class and its methods to be accessed by classes from any package.

    No modifier only allows classes to be access from it's defined package.

    Private would prevent all access (no point to this if using with a top-level class).

    Abstract classes allow you to create child classes derived from the parent (abstract) class. For example, you can make an Abstract Shape class and have a Rectangle class extend shape, inheriting all its methods, variables, and forcing it to define any abstract methods.

提交回复
热议问题