Home Segments Top Top Previous Next

282: Mainline

Instead of defining public getters to expand access to private instance variables and methods, you can expand access, without providing totally public access, by marking the instance variables and methods with the protected keyword, rather than with the public or private keywords. Variables and methods so marked are said to be in the protected part of the class definition, which, by convention, generally is defined between the public and private parts of the class definition:

public class Attraction {
 // Define zero-parameter constructor:
 public Attraction () {minutes = 75;}
 // Define one-parameter constructor:
 public Attraction (int m) {minutes = m;}
 // Define getter:
 public int getMinutes () {return minutes;}
 // Define setter:
 public void setMinutes (int m) {minutes = m;}
 // Define protected variable:
 protected int minutes; 
}