23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

An Arithmetic Progression Class<br />

Next, we consider the class ArithProgression, which we present <strong>in</strong> Code<br />

Fragment 2.3. This class def<strong>in</strong>es an arithmetic progression, where the next value<br />

is determ<strong>in</strong>ed by add<strong>in</strong>g a fixed <strong>in</strong>crement, <strong>in</strong>c, to the previous value.<br />

ArithProgression <strong>in</strong>herits fields first <strong>and</strong> cur <strong>and</strong> methods<br />

firstValue() <strong>and</strong> pr<strong>in</strong>tProgression(n) from the Progression<br />

class. It adds a new field, <strong>in</strong>c, to store the <strong>in</strong>crement, <strong>and</strong> two constructors for<br />

sett<strong>in</strong>g the <strong>in</strong>crement. F<strong>in</strong>ally, it overrides the nextValue() method to conform<br />

to the way we get the next value for an arithmetic progression.<br />

Polymorphism is at work here. When a Progression reference is po<strong>in</strong>t<strong>in</strong>g to<br />

an Arith Progression object, then it is the ArithProgression methods<br />

firstValue() <strong>and</strong> nextValue() that will be used. This polymorphism is<br />

also true <strong>in</strong>side the <strong>in</strong>herited version of pr<strong>in</strong>tProgression(n), because the<br />

calls to the firstValue() <strong>and</strong> nextValue() methods here are implicit for<br />

the "current" object (called this <strong>in</strong> <strong>Java</strong>), which <strong>in</strong> this case will be of the Arith<br />

Progression class.<br />

Example Constructors <strong>and</strong> the Keyword this<br />

In the def<strong>in</strong>ition of the Arith Progression class, we have added two<br />

constructors, a default one, which takes no parameters, <strong>and</strong> a parametric one,<br />

which takes an <strong>in</strong>teger parameter as the <strong>in</strong>crement for the progression. The default<br />

constructor actually calls the parametric one, us<strong>in</strong>g the keyword this <strong>and</strong><br />

pass<strong>in</strong>g 1 as the value of the <strong>in</strong>crement parameter. These two constructors<br />

illustrate method overload<strong>in</strong>g (where a method name can have multiple versions<br />

<strong>in</strong>side the same class), s<strong>in</strong>ce a method is actually specified by its name, the class<br />

of the object that calls it, <strong>and</strong> the types of arguments that are passed to it—its<br />

signature. In this case, the overload<strong>in</strong>g is for constructors (a default constructor<br />

<strong>and</strong> a parametric constructor).<br />

The call this(1) to the parametric constructor as the first statement of the<br />

default constructor triggers an exception to the general constructor cha<strong>in</strong><strong>in</strong>g rule<br />

discussed <strong>in</strong> Section 2.2.3. Namely, whenever the first statement of a constructor<br />

C ′ calls another constructor C ″ of the same class us<strong>in</strong>g the this reference, the<br />

superclass constructor is not implicitly called for C. Note that a superclass<br />

constructor will eventually be called along the cha<strong>in</strong>, either explicitly or<br />

implicitly. In particular, for our ArithProgression class, the default<br />

constructor of the superclass (Progression) is implicitly called as the first<br />

statement of the parametric constructor of Arith Progression.<br />

We discuss constructors <strong>in</strong> more detail <strong>in</strong> Section 1.2.<br />

104

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!