0

Can we define public methods in package-private classes? For example, the main method is always public even within a package-private class. How comes that we can access a public method belonging to a class that is not intended to be public?

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
Nana
  • 1

1 Answers1

1

A class not being public only means the world can't refer to it directly. If the class implements an interface, then you can access the class and its public methods through that interface. This is a very common idiom, for example anonymous classes are the most private form of class and they are routinely used by foreign code as callback holders.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436