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.

The ability of extend<strong>in</strong>g from more than one class is known as multiple<br />

<strong>in</strong>heritance. In <strong>Java</strong>, multiple <strong>in</strong>heritance is allowed for <strong>in</strong>terfaces but not for<br />

classes. The reason for this rule is that the methods of an <strong>in</strong>terface never have<br />

bodies, while methods <strong>in</strong> a class always do. Thus, if <strong>Java</strong> were to allow for multiple<br />

<strong>in</strong>heritance for classes, there could be a confusion if a class tried to extend from two<br />

classes that conta<strong>in</strong>ed methods with the same signatures. This confusion does not<br />

exist for <strong>in</strong>terfaces, however, s<strong>in</strong>ce their methods are empty. So, s<strong>in</strong>ce no confusion<br />

is <strong>in</strong>volved, <strong>and</strong> there are times when multiple <strong>in</strong>heritance of <strong>in</strong>terfaces is useful,<br />

<strong>Java</strong> allows for <strong>in</strong>terfaces to use multiple <strong>in</strong>heritance.<br />

One use for multiple <strong>in</strong>heritance of <strong>in</strong>terfaces is to approximate a multiple<br />

<strong>in</strong>heritance technique called the mix<strong>in</strong>. Unlike <strong>Java</strong>, some object-oriented<br />

languages, such as Smalltalk <strong>and</strong> C++, allow for multiple <strong>in</strong>heritance of concrete<br />

classes, not just <strong>in</strong>terfaces. In such languages, it is common to def<strong>in</strong>e classes, called<br />

mix<strong>in</strong> classes, that are never <strong>in</strong>tended to be created as st<strong>and</strong>-alone objects, but are<br />

<strong>in</strong>stead meant to provide additional functionality to exist<strong>in</strong>g classes. Such<br />

<strong>in</strong>heritance is not allowed <strong>in</strong> <strong>Java</strong>, however, so programmers must approximate it<br />

with <strong>in</strong>terfaces. In particular, we can use multiple <strong>in</strong>heritance of <strong>in</strong>terfaces as a<br />

mechanism for "mix<strong>in</strong>g" the methods from two or more unrelated <strong>in</strong>terfaces to<br />

def<strong>in</strong>e an <strong>in</strong>terface that comb<strong>in</strong>es their functionality, possibly add<strong>in</strong>g more methods<br />

of its own. Return<strong>in</strong>g to our example of the antique objects, we could def<strong>in</strong>e an<br />

<strong>in</strong>terface for <strong>in</strong>surable items as follows:<br />

public <strong>in</strong>terface InsurableItem extends Transportable,<br />

Sellable {<br />

}<br />

/** Returns <strong>in</strong>sured Value <strong>in</strong> cents */<br />

public <strong>in</strong>t <strong>in</strong>suredValue();<br />

This <strong>in</strong>terface mixes the methods of the Transportable <strong>in</strong>terface with the<br />

methods of the Sellable <strong>in</strong>terface, <strong>and</strong> adds an extra method, <strong>in</strong>suredValue.<br />

Such an <strong>in</strong>terface could allow us to def<strong>in</strong>e the BoxedItem alternately as follows:<br />

public class BoxedItem2 implements InsurableItem {<br />

}<br />

// … same code as class BoxedItem<br />

In this case, note that the method <strong>in</strong>suredValue is not optional, whereas it was<br />

optional <strong>in</strong> the declaration of BoxedItem given previously.<br />

<strong>Java</strong> <strong>in</strong>terfaces that approximate the mix<strong>in</strong> <strong>in</strong>clude java.lang.Cloneable,<br />

which adds a copy feature to a class, java.lang.Comparable, which adds a<br />

120

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

Saved successfully!

Ooh no, something went wrong!