Sub topic 5
Choosing a Class's Public API
A class should give other code a clear way to use it without exposing every detail inside it. This chapter shows how to choose that set of operations.
A class is a plan for creating objects. An object is a value created from a class. A class can have fields, which store data, and methods, which perform actions or return information.
A class’s public API is the set of things other code can use through that class. It can include public methods, constructors, and fields. A constructor is the part of a class that sets up a new object.
Good API choices make a class easier to use and harder to use incorrectly.
Start with the user’s needs
Code that uses a class is called its client. When designing a class, ask what its clients need to do, not what fields the class happens to have.
For example, a client using a light might need to:
- Turn the light on.
- Turn the light off.
- Check whether the light is on.
The client does not need to know how the light stores its state. The state of an object is the data it holds at a given time.
An access modifier is a Java keyword that controls where a class member can be used. A member is a field or method that belongs to a class.
| Choice | Use it when |
|---|---|
| `public` | Other code needs to use the member. |
| `private` | The member is an internal detail of the class. |
| No modifier | Code in the same package needs it, but it is not intended for general use. |
A package is a named group of Java classes. A member with no access modifier is called package-private.
Build a small public API
Create a file named Light.java with this code:
public class Light {
private boolean on;
public Light() {
on = false;
}
public void turnOn() {
on = true;
}
public void turnOff() {
on = false;
}
public boolean isOn() {
return on;
}
}The field on holds a Boolean, a value that is either true or false. It is private, so client code cannot change it directly.
The constructor and three methods are public. They form the useful interface for this light:
Light()creates a light that starts off.turnOn()turns it on.turnOff()turns it off.isOn()reports whether it is on.
The word void means a method does not return a value. The isOn() method returns a Boolean instead.
Now create a file named LightDemo.java in the same folder:
public class LightDemo {
public static void main(String[] args) {
Light light = new Light();
light.turnOn();
System.out.println(light.isOn());
light.turnOff();
System.out.println(light.isOn());
}
}Run these commands from that folder. The first command compiles the files, which means it prepares them for Java to run.
javac Light.java LightDemo.java
java LightDemoThe program prints true and then false. The client uses the public methods to control and check the light. It does not need to use the on field directly. If it tried to access that private field, the program would not compile.
Choose what to expose
Public methods are promises to clients. If many programs call turnOn(), changing its name would force those programs to change too. Changing what it does could also surprise them.
Private details give you more room to change the class. For example, you could change how Light stores its state while keeping the same public methods and their behavior.
Use these questions when choosing an API:
- What actions must clients perform? Give those actions clear public method names.
- What information must clients read? Provide a method when that information is useful.
- Which details are only needed inside the class? Keep those private.
- Does every proposed public member have a purpose? Leave it out if clients do not need it.
Do not add a public method just because a field exists. For example, Light does not need a separate method that lets clients set on to any value. Its named actions already provide a clear way to control it.
Check your understanding
Exercise
A client needs to know whether a Light is on. Should it read the on field directly, or call isOn()? Why?
Show answer
It should call isOn(). The method is public and reports the needed information; the private field is an internal detail.