IntroJB06.pdf
(
220 KB
)
Pobierz
Microsoft Word - CH06ProgrammingWithObjectsAndClasses.DOC
Part II: Object-Oriented Programming
In Part I, “Fundamentals of Programming,” you learned how to
write simple Java applications using primitive data types,
control statements, methods, and arrays, all of which are
features commonly available in procedural programming
languages. Java, however, is a class-centric object-oriented
programming language that uses abstraction, encapsulation,
inheritance, and polymorphism to provide great flexibility,
modularity, and reusability for developing software. In this
part of the book you will learn how to define, extend, and
work with classes and their objects.
Chapter 6 Programming with Objects and Classes
Chapter 7 Strings
Chapter 8 Class Inheritance and Interfaces
Chapter 9 Object-Oriented Software Development
218
6
Objects and Classes
Objectives
•
Understand objects and classes and the relationship
between them.
•
Learn how to define a class and how to create an
object of the class.
•
Comprehend the roles of constructors.
•
Know object references and how to pass objects to
methods.
•
Understand instance variables and methods.
•
Understand static variables, constants, and methods.
•
Use objects as array elements.
•
Use UML graphical notations to describe classes and
objects.
•
Understand the scope of variables in the context of a
class.
•
Become familiar with the organization of the Java API.
219
Introduction
Programming in procedural languages like C, Pascal, BASIC,
Ada, and COBOL involves choosing data structures, designing
algorithms, and translating algorithms into code. An object-
oriented language like Java combines the power of procedural
languages with an added dimension that provides such
benefits as abstraction, encapsulation, reusability, and
inheritance.
In procedural programming, data and operations on the data
are separate, and this methodology requires sending data to
procedures and functions. Object-oriented programming places
data and the operations that pertain to them within a single
entity called
object
; this approach solves many of the
problems inherent in procedural programming. The object-
oriented programming approach organizes programs in a way
that mirrors the real world, in which all objects are
associated with both attributes and activities. Programming
in Java involves thinking in terms of objects; a Java
program can be viewed as a collection of cooperating
objects.
This chapter introduces the fundamentals of object-oriented
programming: declaring classes, creating objects,
manipulating objects, and making objects work together.
Objects and Classes
Object-oriented programming (OOP) involves programming using
objects. An
object
represents an entity in the real world
that can be distinctly identified. For example, a student, a
desk, a circle, and even a mortgage loan can all be viewed
as objects. An object has a unique identity, a state and
behaviors. The state of an object consists of a set of
fields, or fields with their current values. The behavior of
an object is defined by a set of methods. Figure 6.1 shows a
diagram of an object with its data fields and methods.
***Insert Figure 6.1 (Same as Figure 5.1 in the 3
rd
Edition,
p139)
Figure 6.1
An object contains both state and behavior. The state
defines the object, and the behavior defines what the object
does.
A Circle object, for example, has a data field radius, which
is the property that characterizes a circle. One behavior of
a circle is that its area can be computed. A Circle object
is shown in Figure 6.2.
220
***Insert Figure 6.2 (Same as Figure 5.2 in the 3
rd
Edition,
p139)
Figure 6.2
A Circle object contains the radius data field and the
findArea method.
Classes
are constructs that define objects of same type. In
a Java class, data are used to describe properties, and
methods are used to define behaviors. A class for an object
contains a collection of method and data definitions. Here
is an example of the class for a circle:
class Circle
{
/**The radius of this circle*/
double radius = 1.0;
/**Return the area of this circle*/
double findArea()
{
return radius*radius*3.14159;
}
}
This class is different from all of the other classes you
have seen thus far. The Circle class does not have a main
method. Therefore, you cannot run this class; it is merely a
definition used to declare and create Circle objects. For
convenience, the class that contains the main method will be
referred to as the
main class
in this book.
A class is a blueprint that defines what an object’s data
and methods will be. An object is an instance of a class.
You can create many instances of a class (see Figure 6.3).
Creating an instance is referred to as
instantiation
. The
terms
object
and
instance
are often interchangeable. The
relationship between classes and objects is analogous to the
relationship between apple pie recipes and apple pies. You
can make as many apple pies as you want from a single
recipe.
***Insert Figure 6.3
221
Circle
UML Graphical notation for classes
radius: double
UML Graphical notation for fields
UML Graphical notation for methods
findArea(): double
new Circle()
new Circle()
circle1: Circle
circlen: Circle
UML Graphical notation
for objects
radius = 2
...
radius = 5
Figure 6.3
A class can have many different objects.
NOTE: Figure 6.3 uses the graphical notations
adopted in the Unified Modeling Language (UML)
to illustrate classes and objects. UML has
become the standard for object-oriented
modeling. For more information on UML, see
www.rational.com/uml
. For a summary of the
graphical notations used in this book, see
Appendix G, “UML Graphical Notations.”
Creating Objects and Object Reference Variables
Objects are created using the new operator as follows:
new ClassName();
For example, new Circle() creates an object of the Circle
class. Newly created objects are allocated in the memory.
Objects are accessed via object
reference variables
, which
contain references to the objects. Such variables are
declared using the following syntax:
ClassName objectReference;
The types of reference variables are known as
reference
types
. The following statement declares the variable
myCircle to be of the Circle type:
Circle myCircle;
The variable myCircle can reference a Circle object. The
following statement creates an object and assigns its
reference to myCircle.
myCircle = new Circle();
222
Plik z chomika:
rrdzony
Inne pliki z tego folderu:
bookcover.jpg
(9 KB)
index.html
(7 KB)
IntroJB01.pdf
(540 KB)
IntroJB06.pdf
(220 KB)
IntroJB10.pdf
(456 KB)
Inne foldery tego chomika:
3D Studio Max 4
61.ebooks
ABC Nagrywania Płyt CD
ABC Systemu Windows XP
Absolute Java 1st Edition
Zgłoś jeśli
naruszono regulamin