Introduction To Java Programing (370-378)
Introduction To Java Programing (370-378)
(a) variable radius and method findArea() can be (b) i has to be declared before j because j’s initial
declared in any order value is dependent on i.
FIGURE 10.1 Members of a class can be declared in any order, with one exception.
You can declare a class’s variable only once, but you can declare the same variable name in
a method many times in different nonnesting blocks.
If a local variable has the same name as a class’s variable, the local variable takes prece-
dence and the class’s variable with the same name is hidden. For example, in the following
program, x is defined as an instance variable and as a local variable in the method.
What is the printout for f.p(), where f is an instance of Foo? The printout for f.p() is 1 for
x and 0 for y. Here is why:
346 Chapter 10 Thinking in Objects
■ x is declared as a data field with the initial value of 0 in the class, but is also declared
in the method p() with an initial value of 1. The latter x is referenced in the
System.out.println statement.
■ y is declared outside the method p(), but is accessible inside it.
Tip
To avoid confusion and mistakes, do not use the names of instance or static variables as local
variable names, except for method parameters.
public class Foo { Suppose that f1 and f2 are two objects of Foo.
int i = 5;
static double k = 0; Invoking f1.setI(10) is to execute
this.i = 10, where this refers f1
void setI(int i) {
this.i = i ; Invoking f2.setI(45) is to execute
} this.i = 45, where this refers f2
(a) (b)
FIGURE 10.2 The keyword this refers to the calling object that invokes the method.
The this keyword gives us a way to refer to the object that invokes an instance method within
the code of the instance method. The line this.i = i means “assign the value of parameter i to the
data field i of the calling object.” The keyword this refers to the object that invokes the instance
method setI, as shown in Figure 10.2(b). The line Foo.k = k means that the value in parameter k
is assigned to the static data field k of the class, which is shared by all the objects of the class.
call another constructor Another common use of the this keyword is to enable a constructor to invoke another
constructor of the same class. For example, you can rewrite the Circle class as follows:
Tip
If a class has multiple constructors, it is better to implement them using this(arg-list) as
much as possible. In general, a constructor with no or fewer arguments can invoke the construc-
tor with more arguments using this(arg-list). This often simplifies coding and makes the
class easier to read and to maintain.
Note
Java requires that the this(arg-list) statement appear first in the constructor before any
other statements.
FIGURE 10.3 Class abstraction separates class implementation from the use of the class.
Class abstraction and encapsulation are two sides of the same coin. Many real-life exam-
ples illustrate the concept of class abstraction. Consider, for instance, building a computer
system. Your personal computer has many components—a CPU, memory, disk, motherboard,
fan, and so on. Each component can be viewed as an object that has properties and methods.
To get the components to work together, you need know only how each component is used
and how it interacts with the others. You don’t need to know how the components work inter-
nally. The internal implementation is encapsulated and hidden from you. You can build a
computer without knowing how a component is implemented.
The computer-system analogy precisely mirrors the object-oriented approach. Each com-
ponent can be viewed as an object of the class for the component. For example, you might
have a class that models all kinds of fans for use in a computer, with properties such as fan
size and speed and methods such as start and stop. A specific fan is an instance of this class
with specific property values.
As another example, consider getting a loan. A specific loan can be viewed as an object of
a Loan class. Interest rate, loan amount, and loan period are its data properties, and comput-
ing monthly payment and total payment are its methods. When you buy a car, a loan object is
created by instantiating the class with your loan interest rate, loan amount, and loan period.
You can then use the methods to find the monthly payment and total payment of your loan. As
a user of the Loan class, you don’t need to know how these methods are implemented.
Listing 2.8, ComputeLoan.java, presented a program for computing loan payments. The
program cannot be reused in other programs. One way to fix this problem is to define static
methods for computing monthly payment and total payment. However, this solution has limi- Video Note
tations. Suppose you wish to associate a date with the loan. The ideal way is to create an The Loan class
348 Chapter 10 Thinking in Objects
Loan
-annualInterestRate: double The annual interest rate of the loan (default: 2.5).
-numberOfYears: int The number of years for the loan (default: 1).
-loanAmount: double The loan amount (default: 1000).
-loanDate: java.util.Date The date this loan was created.
FIGURE 10.4 The Loan class models the properties and behaviors of loans.
object that ties the properties for loan information and date together. Figure 10.4 shows the
UML class diagram for the Loan class.
The UML diagram in Figure 10.4 serves as the contract for the Loan class. Throughout
this book, you will play the roles of both class user and class developer. The user can use the
class without knowing how the class is implemented. Assume that the Loan class is available.
We begin by writing a test program that uses the Loan class (Listing 10.1).
The main method reads interest rate, payment period (in years), and loan amount; creates a
Loan object; and then obtains the monthly payment (line 29) and total payment (line 30)
using the instance methods in the Loan class.
The Loan class can be implemented as in Listing 10.2.
From a class developer’s perspective, a class is designed for use by many different customers.
In order to be useful in a wide range of applications, a class should provide a variety of ways
for customization through constructors, properties, and methods.
The Loan class contains two constructors, four get methods, three set methods, and the
methods for finding monthly payment and total payment. You can construct a Loan object by
using the no-arg constructor or the one with three parameters: annual interest rate, number of
years, and loan amount. When a loan object is created, its date is stored in the loanDate field.
The getLoanDate method returns the date. The three get methods, getAnnualInterest,
getNumberOfYears, and getLoanAmount, return annual interest rate, payment years, and
loan amount, respectively. All the data properties and methods in this class are tied to a specific
instance of the Loan class. Therefore, they are instance variables or methods.
10.6 Object-Oriented Thinking 351
This method is useful for computing body mass index for a specified weight and height. How-
ever, it has limitations. Suppose you need to associate the weight and height with a person’s
name and birth date. You may declare separate variables to store these values. But these val-
ues are not tightly coupled. The ideal way to couple them is to create an object that contains
them. Since these values are tied to individual objects, they should be stored in instance data Video Note
fields. You can define a class named BMI, as shown in Figure 10.5. The BMI class
+BMI(name: String, age: int, weight: Creates a BMI object with the specified
double, height: double) name, age, weight, and height.
+BMI(name: String, weight: double, Creates a BMI object with the specified
height: double) name, weight, height, and a default age 20.
Line 3 creates an object bmi1 for John Doe and line 7 creates an object bmi2 for Peter King.
You can use the instance methods getName(), getBMI(), and getStatus() to return the
BMI information in a BMI object.
The BMI class can be implemented as in Listing 10.4.
The mathematic formula for computing the BMI using weight and height is given in §3.10.
The instance method getBMI() returns the BMI. Since the weight and height are instance
data fields in the object, the getBMI() method can use these properties to compute the BMI
for the object.
The instance method getStatus() returns a string that interprets the BMI. The interpre-
tation is also given in §3.10.
This example demonstrates the advantages of the object-oriented paradigm over the
procedural paradigm. The procedural paradigm focuses on designing methods. The object- Procedural vs. Object-
oriented paradigm couples data and methods together into objects. Software design using the Oriented Paradigms
object-oriented paradigm focuses on objects and operations on objects. The object-oriented
approach combines the power of the procedural paradigm with an added dimension that inte-
grates data with operations into objects.
In procedural programming, data and operations on the data are separate, and this method-
ology requires sending data to methods. Object-oriented programming places data and the
operations that pertain to them in an object. This approach solves many of the problems inher-
ent in procedural programming. The object-oriented programming approach organizes pro-
grams in a way that mirrors the real world, in which all objects are associated with both
attributes and activities. Using objects improves software reusability and makes programs
easier to develop and easier to maintain. Programming in Java involves thinking in terms of
objects; a Java program can be viewed as a collection of cooperating objects.