Java Reference
In-Depth Information
A class is a collection of data fields that hold values and methods that operate
on those values. A class defines a new reference type, such as the Point type
defined in Chapter 2 .
The Point class defines a type that is the set of all possible two-dimensional points.
An object is an instance of a class.
A Point object is a value of that type: it represents a single two-dimensional point.
Objects are often created by instantiating a class with the new keyword and a con‐
structor invocation, as shown here:
Point p = new Point ( 1.0 , 2.0 );
Constructors are covered later in this chapter in “Creating and Initializing Objects”
on page 106 .
A class definition consists of a signature and a body . The class signature defines the
name of the class and may also specify other important information. The body of a
class is a set of members enclosed in curly braces. The members of a class usually
include fields and methods, and may also include constructors, initializers, and nes‐
ted types.
Members can be static or nonstatic. A static member belongs to the class itself while
a nonstatic member is associated with the instances of a class (see “Fields and Meth‐
ods” on page 100 ).
There are four very common kinds of members—class fields,
class methods, instance fields, and instance methods. The
majority of work done with Java involves interacting with
these kinds of members.
The signature of a class may declare that the class extends another class. The exten‐
ded class is known as the superclass and the extension is known as the subclass . A
subclass inherits the members of its superclass and may declare new members or
override inherited methods with new implementations.
The members of a class may have access modiiers public , protected , or private . 1
These modifiers specify their visibility and accessibility to clients and to subclasses.
This allows classes to control access to members that are not part of their public
API. This ability to hide members enables an object-oriented design technique
known as data encapsulation , which we discuss in “Data Hiding and Encapsulation”
on page 121 .
1 There is also the default, aka package, visibility that we will meet later.
Search WWH ::

Custom Search