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.

comparability feature to a class (impos<strong>in</strong>g a natural order on its <strong>in</strong>stances), <strong>and</strong><br />

java.util.Observer, which adds an update feature to a class that wishes to<br />

be notified when certa<strong>in</strong> "observable" objects change state.<br />

2.4.3 Abstract Classes <strong>and</strong> Strong Typ<strong>in</strong>g<br />

An abstract class is a class that conta<strong>in</strong>s empty method declarations (that is,<br />

declarations of methods without bodies) as well as concrete def<strong>in</strong>itions of methods<br />

<strong>and</strong>/or <strong>in</strong>stance variables. Thus, an abstract class lies between an <strong>in</strong>terface <strong>and</strong> a<br />

complete concrete class. Like an <strong>in</strong>terface, an abstract class may not be <strong>in</strong>stantiated,<br />

that is, no object can be created from an abstract class. A subclass of an abstract<br />

class must provide an implementation for the abstract methods of its superclass,<br />

unless it is itself abstract. But, like a concrete class, an abstract class A can extend<br />

another abstract class, <strong>and</strong> abstract <strong>and</strong> concrete classes can further extend A, as<br />

well. Ultimately, we must def<strong>in</strong>e a new class that is not abstract <strong>and</strong> extends<br />

(subclasses) the abstract superclass, <strong>and</strong> this new class must fill <strong>in</strong> code for all<br />

abstract methods. Thus, an abstract class uses the specification style of <strong>in</strong>heritance,<br />

but also allows for the specialization <strong>and</strong> extension styles as well (see Section 2.2.3.<br />

The java.lang.Number Class<br />

It turns out that we have already seen an example of an abstract class. Namely, the<br />

<strong>Java</strong> number classes (shown <strong>in</strong> Table 1.2) specialize an abstract class called<br />

java.lang.Number. Each concrete number class, such as<br />

java.lang.Integer <strong>and</strong> java.lang.Double, extends the<br />

java.lang.Number class <strong>and</strong> fills <strong>in</strong> the details for the abstract methods of<br />

the superclass. In particular, the methods <strong>in</strong>tValue, floatValue,<br />

doubleValue, <strong>and</strong> longValue are all abstract <strong>in</strong> java.lang.Number.<br />

Each concrete number class must specify the details of these methods.<br />

Strong Typ<strong>in</strong>g<br />

In <strong>Java</strong>, an object can be viewed as be<strong>in</strong>g of various types. The primary type of an<br />

object o is the class C specified at the time o was <strong>in</strong>stantiated. In addition, o is of<br />

type S for each superclass S of C <strong>and</strong> is of type I for each <strong>in</strong>terface I implemented<br />

byC.<br />

However, a variable can be declared as be<strong>in</strong>g of only one type (either a class or an<br />

<strong>in</strong>terface), which determ<strong>in</strong>es how the variable is used <strong>and</strong> how certa<strong>in</strong> methods<br />

will act on it. Similarly, a method has a unique return type. In general, an<br />

expression has a unique type.<br />

By enforc<strong>in</strong>g that all variables be typed <strong>and</strong> that methods declare the types they<br />

expect <strong>and</strong> return, <strong>Java</strong> uses the technique of strong typ<strong>in</strong>g to help prevent bugs.<br />

But with rigid requirements on types, it is sometimes necessary to change, or<br />

121

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

Saved successfully!

Ooh no, something went wrong!