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 include a number of classes and interface definitions. This chapter explains how to define new classes and how to do object-oriented programming with them. We also introduce the concept of an interface, but a full discussion of interfaces and Java’s type system is deferred until Chapter 4.
Note
If you have experience with OO programming, however, be careful. The term “object-oriented” has different meanings in different languages. Don’t assume that Java works the same way as your favorite OO language. (This is particularly true for C++ or Python programmers).This is a fairly lengthy chapter, so let’s begin with an overview and some definitions.
Overview of Classes
Classes are the most fundamental structural element of all Java programs. You cannot write Java code without defining a class. All Java statements appear within classes, and all methods are implemented within classes.
Basic OO Definitions
Here are a couple important definitions:
- Class
-
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 ...
Get Java in a Nutshell, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.