This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.
Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
The course is maintained by Technische Informatica Breda
When extending a class, it is possible to use the public methods and attributes of the superclass. It is however, not possible to access the private methods and attributes. Sometimes it is needed to access those methods from a subclass, but not from other classes. We can use the protected keyword to access these methods and attributes
| Modifier | Class | Package | Subclass | World | 
|---|---|---|---|---|
public | 
      Y | Y | Y | Y | 
protected | 
      Y | Y | Y | N | 
| no modifier | Y | Y | N | N | 
private | 
      Y | N | N | N | 
As we can see, protected members are also accessable by other classes in the same package. It is mainly used to relax the strictnes of private to work better in subclasses. We could take the example of Person class. Suppose we have a Person class with a Student subclass.
Exercise access-modifiers-1: Abstract macro
To automate tasks on your computer, java has a
Robotclass. This class is provided by java, and has the following featuresRobot r = new Robot(); r.mouseMove(100, 100); // moves the mouse to 100, 100 r.mousePress(InputEvent.BUTTON1_MASK); // presses down the left mouse r.delay(50); // waits for 50ms r.mouseRelease(InputEvent.BUTTON1_MASK); // releases the mouse againWe’re going to build a
Macroclass, that can combine any number of actions into a list, and then execute them in order, with a small delay inbetween. The delay is set to a default of 250ms, but can be changed by any specific macro object. We’re going to build code that allows us to
- Move the mouse to a position
 - Click on a position
 - Wait for a delay
 The code to test this macro class would be
import java.awt.*; import java.awt.event.*; Macro macro = new Macro(); macro.add(new MouseMove(100, 100)); macro.add(new MouseClick()); macro.add(new Wait(100)); macro.add(new MouseClick()); macro.run();Exercise access-modifiers-1.1
We’re going to start by building an abstract class
Action. Put this action in theactionspackage. TheActionclass has
- an integer attribute
 delay, that gets set to 250 in the constructor- a setter for the
 delay- a protected abstract method
 void perform()- a public method
 performAndWait(), that executes theperform()method, and then waits for the time set in thedelayattributeExercise access-modifiers-1.2
Build the classes
MouseMove,MouseClickandWait. These classes all extend the Action class, and are all located in theactionspackage. In the classes, implement theperform()method, and put the proper code there to either move the mouse, click the mouse or to wait. Don’t forget to import theRobotclass from thejava.awtpackage, and theInputEventfrom thejava.awt.eventpackageExercise access-modifiers-1.3
Build a
Macroclass, in the default package and test it with themainmethod. The class should have the following:
- an attribute
 ArrayList<Action> actions;- a method
 add(Action action), that adds the action to the actions arraylist- a
 runmethod, that loops through theactionsArrayList, and runs theperformAndWait()method.