Chapter 5 Introducing Classes
Chapter 5 Introducing Classes
OBJECTIVES
How to declare a class and use it to create an object.
How to implement a class’s behaviors as methods.
How to implement a class’s attributes as instance variables and
properties.
How to call an object’s methods to make them perform their tasks.
How to use a constructor to initialize an object’s data.
INTRODUCTION
Object-oriented programming in Java is based on classes. Any concept you wish to
implement in a Java program must be encapsulated within a class.
A class defines a new data type.
Once defined, this new type can be used to create objects of that type.
Thus, a class is a template for an object, and an object is an instance of a class.
CLASS FUNDAMENTALS
The data, or variables, defined within a class are called instance variables.
The code is contained within methods.
Collectively, the methods and variables defined within a class are called members of
the class.
Variables defined within a class are called instance variables because each instance
of the class (that is, each object of the class) contains its own copy of these variables.
DECLARING A CLASS WITH A METHOD
Every class declaration contains keyword class followed immediately by
the class’s name.
Every class’s body is enclosed in a pair of left and right braces