Imagine a scenario where you are developing a library, and you want to restrict the usage of some specific methods to the external world but allow them to be used inside the package. How would you implement this using access modifiers?

  • package-private
  • private
  • protected
  • public
To restrict the usage of certain methods to the external world while allowing them to be used within the package, you would use the package-private access modifier. This is achieved by not specifying any access modifier (default) before the method declaration. Public methods are accessible from anywhere, private methods are restricted to the class, and protected methods allow access within the package and subclasses.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *