Introduction To Classes
Introduction To Classes
Class Declaration
Each class declaration that begins with the access modifier public
must be stored in a file that has the same name as the class and ends
with the .java filename extension.
Every class declaration contains keyword class followed immediately
by the class’s name.
The return statement passes a value from a called method back to its
caller.
Classes often provide public methods to allow the class’s clients to
set or get private instance variables.
The names of these methods need not begin with set or get, but this
naming convention is recommended.
Top Compartment
In the UML, each class is modeled in a class diagram as a rectangle
with three compartments. The top one contains the class’s name
centered horizontally in boldface.
Middle Compartment
The middle compartment contains the class’s attributes, which
correspond to instance variables in Java.
Bottom Compartment
The bottom compartment contains the class’s operations, which correspond
to methods and constructors in Java.
The UML represents instance variables as an attribute name, followed by a
colon and the type.
Private attributes are preceded by a minus sign (–) in the UML.
The UML models operations by listing the operation name followed by a set
of parentheses.
A plus sign (+) in front of the operation name indicates that the operation is
a public one in the UML (i.e., a public method in Java).
Return Types
The UML indicates an operation’s return type by placing a colon and
the return type after the parentheses following the operation name.
UML class diagrams do not specify return types for operations that do
not return values.
Declaring instance variables private is known as data hiding or
information hiding.
Parameters
The UML models a parameter of an operation by listing the parameter
name, followed by a colon and the parameter type between the
parentheses after the operation name