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
Robot
class. 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 again
We’re going to build a
Macro
class, 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 theactions
package. TheAction
class 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 thedelay
attributeExercise access-modifiers-1.2
Build the classes
MouseMove
,MouseClick
andWait
. These classes all extend the Action class, and are all located in theactions
package. 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 theRobot
class from thejava.awt
package, and theInputEvent
from thejava.awt.event
packageExercise access-modifiers-1.3
Build a
Macro
class, in the default package and test it with themain
method. 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
run
method, that loops through theactions
ArrayList, and runs theperformAndWait()
method.