public Classes and InterfacesPurpose:
- To allow a class or interface to be used outside the package in which it was defined
Mechanics:
public <other modifiers&t; class <class name> {/* Instance variables declared and methods defined here */
}- public interface <interface name> {
- /* Interface definition here */
}
Example:
public class Circle {}
Usage:
publicis one of several possible modifiers that can appear anywhere in the space-separated list of modifiers.- By defining a class as
public, a class in another package can instantiate that class, inherit from it, maintain a reference to an instance of it, or access itsstaticmethods and instance variables (if any). - Bu defining an interface as
public, an interface in another class can inherit from it or have a reference to an instance of it as a parameter, and a class in another package can implement it, maintain a reference to an instance of an implementation of it, or access it class constants.
Restrictions:
- Only one
publicinterface or class is allowed per file and the name of the file must be the same as the name of thepublicclass or interface (plus a java extension). For example, if I have apublicclass calledCircle, it must be the only public class in a file calledCircle.java.
email suggestions to: cs015tas@cs.brown.edu
