Chapter 4. Object-Oriented Programming in Java
Now that we’ve covered fundamental Java syntax, and how to run our programs, 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 an important special case, called record classes or records) 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 5.
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 other favorite OO languages. (This is particularly true for JavaScript or Python programmers.)
This is a fairly lengthy chapter, so let’s begin with an overview and some definitions.
Overview of Classes and Records
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 some important definitions:
- Class
-
A class is a collection of data fields that holds values, along with methods that operate on those values. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access