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.

equire that we override the dr<strong>in</strong>k method, as all dogs dr<strong>in</strong>k pretty much the<br />

same way. But it could require that we override the sniff method, as a<br />

Bloodhound has a much more sensitive sense of smell than a st<strong>and</strong>ard dog. In this<br />

way, the Bloodhound class specializes the methods of its superclass, Dog.<br />

Extension<br />

In us<strong>in</strong>g extension, on the other h<strong>and</strong>, we utilize <strong>in</strong>heritance to reuse the code<br />

written for methods of the superclass, but we then add new methods that are not<br />

present <strong>in</strong> the superclass, so as to extend its functionality. For example, return<strong>in</strong>g<br />

to our Dog class, we might wish to create a subclass, BorderCollie, which<br />

<strong>in</strong>herits all the st<strong>and</strong>ard methods of the Dog class, but then adds a new method,<br />

herd, s<strong>in</strong>ce Border Collies have a herd<strong>in</strong>g <strong>in</strong>st<strong>in</strong>ct that is not present <strong>in</strong> st<strong>and</strong>ard<br />

dogs. By add<strong>in</strong>g the new method, we are extend<strong>in</strong>g the functionality of a st<strong>and</strong>ard<br />

dog.<br />

In <strong>Java</strong>, each class can extend exactly one other class. Even if a class def<strong>in</strong>ition<br />

makes no explicit use of the extends clause, it still <strong>in</strong>herits from exactly one<br />

other class, which <strong>in</strong> this case is class java.lang.Object. Because of this<br />

property, <strong>Java</strong> is said to allow only for s<strong>in</strong>gle <strong>in</strong>heritance among classes.<br />

Types of Method Overrid<strong>in</strong>g<br />

Inside the declaration of a new class, <strong>Java</strong> uses two k<strong>in</strong>ds of method overrid<strong>in</strong>g,<br />

ref<strong>in</strong>ement <strong>and</strong> replacement. In the replacement type of overrid<strong>in</strong>g, a method<br />

completely replaces the method of the superclass that it is overrid<strong>in</strong>g (as <strong>in</strong> the<br />

sniff method of Bloodhound mentioned above). In <strong>Java</strong>, all regular methods<br />

of a class utilize this type of overrid<strong>in</strong>g behavior.<br />

In the ref<strong>in</strong>ement type of overrid<strong>in</strong>g, however, a method does not replace the<br />

method of its superclass, but <strong>in</strong>stead adds additional code to that of its superclass.<br />

In <strong>Java</strong>, all constructors utilize the ref<strong>in</strong>ement type of overrid<strong>in</strong>g, a scheme called<br />

constructor cha<strong>in</strong><strong>in</strong>g. Namely, a constructor beg<strong>in</strong>s its execution by call<strong>in</strong>g a<br />

constructor of the superclass. This call can be made explicitly or implicitly. To<br />

call a constructor of the superclass explicitly, we use the keyword super to refer<br />

to the superclass. (For example, super() calls the constructor of the superclass<br />

with no arguments.) If no explicit call is made <strong>in</strong> the body of a constructor,<br />

however, the compiler automatically <strong>in</strong>serts, as the first l<strong>in</strong>e of the constructor, a<br />

call to super(). (There is an exception to this general rule, which is discussed<br />

<strong>in</strong> the next section.) Summariz<strong>in</strong>g, <strong>in</strong> <strong>Java</strong>, constructors use the ref<strong>in</strong>ement type of<br />

method overrid<strong>in</strong>g whereas regular methods use replacement.<br />

The Keyword this<br />

100

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

Saved successfully!

Ooh no, something went wrong!