In this hour, you are looking at a class of objects called Virus
whose sole purpose in life is to reproduce in as many places as possible—much like some people I knew in college. A Virus
has several different things it needs to do its work, and these are implemented as the behavior of the class. The information that’s needed for the methods are stored as attributes.
The attributes of an object represent variables needed for the object to function. These variables could be simple data types such as integers, characters, and floating-point numbers, or they could be arrays or objects of classes such as String
or Calendar
. You can use an object’s variables throughout its class, in any of the methods the object contains. By convention, you create variables immediately after the class
statement that creates the class and before any methods.
One of the things that a Virus
object needs is a way to indicate that a file already has been infected. Some computer viruses change the field that stores the time a file was last modified; for example, a virus might move the time from 13:41:20 to 13:41:61. Because no normal file would be saved on the 61st second of a minute, the time signifies that the file was infected. The Virus
object uses the impossible seconds value 86 in an integer variable called newSeconds
.
The following statements begin a class called Virus
with an attribute called newSeconds
and two other attributes:
public class Virus {
public int newSeconds = 86;
public String author = "Sam Snett";
int maxFileSize = 30000;
}
All three variables are attributes for the class: newSeconds
, maxFileSize
, and author
.
Putting a statement such as public
in a variable declaration statement is called access control because it determines how other objects made from other classes can use that variable—or if they can use it at all.
Making a variable public
makes it possible to modify the variable from another program that is using the Virus
object.
If the other program attaches special significance to the number 92, for instance, it can change newSeconds
to that value. The following statements create a Virus
object called influenza
and set its newSeconds
variable:
Virus influenza = new Virus();
influenza.newSeconds = 92;
In the Virus
class, the author
variable also is public
, so it can be changed freely from other programs. The other variable, maxFileSize
, can be used only within the class itself.
When you make a variable in a class public
, the class loses control over how that variable is used by other programs. In many cases, this might not be a problem. For example, the author
variable can be changed to any name or pseudonym that identifies the author of the virus. The name might eventually be used on court documents if the author is prosecuted, so you don’t want to pick a dumb one. The State of Ohio v. LoveHandles doesn’t have the same ring to it as Ohio v. MafiaBoy.
Restricting access to a variable keeps errors from occurring if the variable is set incorrectly by another program. With the Virus
class, if newSeconds
is set to a value of 60 or less, it isn’t reliable as a way to tell that a file is infected. Some files might be saved with that number of seconds regardless of the virus. If the Virus
class of objects needs to guard against this problem, you need to do these two things:
• Switch the variable from public
to protected
or private
, two other statements that provide more restrictive access.
• Add behavior to change the value of the variable and report the value of the variable to other programs.
You can use a protected
variable only in the same class as the variable, any subclasses of that class, or classes in the same package. A package is a group of related classes that serve a common purpose. An example is the java.util
package, which contains classes that offer useful utilities such as date and time programming and file archiving. When you use the import
statement in a Java program with an asterisk, as in import java.util.*
, you are making it easier to refer to the classes of that package in a program.
A private
variable is restricted even further than a protected
variable—you can use it only in the same class. Unless you know that a variable can be changed to anything without affecting how its class functions, you should make the variable private
or protected
.
The following statement makes newSeconds
a private
variable:
private int newSeconds = 86;
If you want other programs to use the newSeconds
variable in some way, you have to create behavior that makes it possible. This task is covered later in the hour.
There also is another type of access control: the lack of any public
, private
, or protected
statement when the variable is created.
In most of the programs you have created prior to this hour, you didn’t specify any access control. When no access control is set, the variable is available only to classes in the same package. This is called default or package access.
18.225.156.236