简体   繁体   中英

What is the use of public method inside class with default level access?

I was just wondering if there is any reason java allow public methods inside class with default level access? I couldn't find any yet. Any knowledge sharing will be appreciated.

This is the only way a default-level access class could implement an interface.

The situation when classes with default access are used as helpers is very common. If you try implementing an interface using default access methods with the same signature, you would get this error:

attempting to assign weaker access privileges; was public

In addition, it is a good idea to use public for clarity everywhere a method is designed for access from outside the class, including private classes declared inside other classes or methods.

A class with default level access may implement an interface, in this case its methods need to be public. Or it may be a bean so it will need public getter and setter methods for properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM