Java Reference
In-Depth Information
Chapter 5
Defining Classes
WHAT YOU WILL LEARN IN THIS CHAPTER
• What a class is, and how you define a class
• How to implement class constructors
• How to define class methods
• What method overloading is
• What a recursive method is and how it works
• How to create objects of a class type
• What packages are and how you can create and use them
• What access attributes are and how you should use them in your class definitions
• What nested classes are and how you use them
In this chapter you explore the heart of the Java language: classes. Classes specify the objects you use in
object-oriented programming. These form the basic building blocks of any Java program, as you saw in
Chapter 1. Every program in Java involves classes because the code for a program can appear only within a
class definition.
You explore the details of how a class definition is put together, how to create your own classes, and how
to use classes to solve your own computing problems. And in the next chapter, you build on this to look at
how object-oriented programming helps you work with sets of related classes.
WHAT IS A CLASS?
As you saw in Chapter 1, a class is a prescription for a particular kind of object — it defines a new type . You
use the definition of a class to create objects of that class type — that is, to create objects that incorporate all
the components specified as belonging to that class.
NOTE Incasethat'stooabstract,lookbacktothepreviouschapterwhereyouusedthe String
class. The String class is a comprehensive definition for a String object, with all the opera-
tions you are likely to need specified. Whenever you create a new String object, you are creat-
ing an object with all the characteristics and operations specified by the class definition. Every
Search WWH ::




Custom Search