public Classes and Interfaces

Purpose:

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:

  • public is 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 its static methods 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 public interface or class is allowed per file and the name of the file must be the same as the name of the public class or interface (plus a java extension). For example, if I have a public class called Circle, it must be the only public class in a file called Circle.java.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu