Chapter 3. Object-Oriented Programming in Java
Now that we've covered fundamental Java syntax, we are ready to begin object-oriented programming in Java. All Java programs use objects, and the type of an object is defined by its class or interface . Every Java program is defined as a class, and nontrivial programs usually include a number of classes and interface definitions. This chapter explains how to define new classes and interfaces and how to do object-oriented programming with them. 
This is a relatively long and detailed chapter, so we begin with an overview and some definitions. A
is a collection of fields that hold values and
A class defines a new reference type, such as the Point type defined in Chapter 2. An object is an instance of a class. The Point class defines a type that is the set of all possible two-dimensional points. A Point object is a value of that type: it represents a single two-dimensional point.
Objects are usually created by instantiating a class with the new keyword and a constructor invocation, as shown here:
Point p = new Point(1.0, 2.0);
Constructors are covered in Section 3.3 later in this chapter.
A class definition consists of a
. The class signature defines the
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 Section 3.2 later in this chapter).
The signature of a class may declare that the class
another class. The extended class is known as the
and the extension is known as the
. A subclass
the members of its superclass and may declare new members or
inherited methods with new
The signature of a class may also declare that the class implements one or more interfaces. An interface is a reference type that defines method signatures but does not include method bodies to implement the methods. A class that implements an interface is required to provide bodies for the interface's methods. Instances of such a class are also instances of the interface type that it implements.
The members of a class may have
, which specify their visibility and accessibility to
Classes and interfaces are the most important of the five fundamental reference types defined by Java. Arrays, enumerated types (or "enums") and annotation types are the other three. Arrays are covered in Chapter 2. Enumerated types and annotation types were introduced in Java 5.0 (see Chapter 4). Enums are a specialized kind of class and annotation types are a specialized kind of interface.