This lesson summarizes the specifications of every
basic class you will create in Java. In future you
will learn to add other modifiers and members - this
is just a summary of what we covered in Term 1.
General
An OOP class should NEVER assume the programmer
is using a specific mode of output. For example,
NEVER print to the console (e.g. println()). You have
no idea what kinds of programs a programmer will use
this class for: console? GUI? phone?
Will they want something
output to the user or to a file? When an output value
is required, have a method return it. When it's an
error message, throw an Exception.
Always JAVADOC you classes properly and according
to Javadoc standards.
Using Javadoc doesn't mean you can omit internal
program documentation. Internal program documentation is for
other programmers that might end up taking over your project
and editing the actual code. Javadoc is for the programmer
that ends up buying/downloading/obtaining your finished
class(es) and using them in his/her application - that programmer
will not be editing your source code to figure out how your class(es)
work.
UML: The following UML notations are often used in
textbooks and notes:
- (minus sign): private member
+ (plus sign): public member
: (colon): used to separate names with types,
for example a parameter might appear as
name : String,
which means there is a parameter called "name" that
is defined as a String parameter. The same is used
for method return types, for example
toString() : String shows
that the method is called toString(), has no
parameters, and it returns a String object.
Data members help represent the state of
an object.
Data members should be encapsulated within
the class, using the private
modifier.
Fields (constants) should be public and non-static.
Identifiers should follow the Java naming standards,
be descriptive but
not too long, and should not use the prefix m_
or any other similar prefix.
Data members and fields should use the appropriate data type.
Data members and fields are declared all together at the top or
bottom of the class.
Accessor and Mutator Methods
Accessors:
Are always used to provide read-access
to a single, private data member.
Are always public.
Always start with "get" in lower-case, followed
by the full data member name properly camel-cased
(e.g. for the data member firstName, the proper
accessor would get getFirstName()).
Don't have any parameters.
Always have the same return type as the data
member its accessing.
Mutators:
Are always used to provide write-access
to a single, private data member (you can omit a
mutator on a private data member to create a read-only
data member).
Are always public.
Always start with "set" in lower-case, followed
by the full data member name properly camel-cased
(e.g. for the data member firstName, the proper
mutator would get setFirstName()).
Always have a single parameter containing the value
to place inside the data member being changed (and must
therefore match the data member's data type).
Always return void.
Usually need to validate the parameter value to make sure
it's valid. If the param value is invalid, an IllegalArgumentException
is thrown, with an appropriate error message. If the
param value is invalid, it is assigned to the data
member.
Constructors
A constructor's job is to construct (create)
new instances of the class (new objects) and initialize
those objects with valid values.
Constructors are public.
Constructors don't have any return type
at all, not even void.
Constructors always have the exact same
name as the class, including case.
Don't put the same code in your constructor(s)
as your mutators: just call your mutator methods
inside your constructor(s) as appropriate.
The toString() Method
toString() is part of Java: it comes from the
class called Object.
toString() is always spelled exactly as you
see it here, including case (lower-case "to",
upper-case "S" on "String").
toString() is always public and always returns
String.
toString()'s job is to create a formatted
"string representation" of a specific object (the
object, as a String).