Java Reference
In-Depth Information
4
The loftier the building,
the deeper must the
foundation be laid.
THOMAS KEMPIS
Introduction
Classes are the single most important language feature that facilitates object-oriented
programming (OOP), the dominant programming methodology in use today. You
have already been using predefined classes. String and Scanner are two of the classes
we have used. An object is a value of a class type and is referred to as an instance of the
class . An object differs from a value of a primitive type in that it has methods (actions)
as well as data. For example, "Hello" is an object of the class String . It has the
characters in the string as its data and also has a number of methods, such as length .
You already know how to use classes, objects, and methods. This chapter tells you how
to define classes and their methods.
Prerequisites
This chapter uses material from Chapters 1 , 2 , and 3 . This chapter requires a basic
understanding of the Java programming language, including the ability to write simple
programs using expressions, assignments, and console I/O. You should be able to output
numbers, as well as have an understanding of how the Scanner class can be used in
console I/O. You should also know how to manage the flow of control using branching
and looping statements, as well as understand how to use Boolean expressions.
4.1
Class Definitions
“The Time has come,” the Walrus said,
“to talk of many things:
of shoes and ships and sealing wax
of cabbages and kings.”
LEWIS CARROLL, Through the Looking-Glass
A Java program consists of objects from various classes interacting with one another.
Before we go into the details of how you define classes, let's review some of the
terminology used with classes. Among other things, a class is a type and you can declare
variables of a class type. A value of a class type is called an object . An object has both
data and actions. The actions are called methods . Each object can have different data,
object
method
 
 
Search WWH ::




Custom Search