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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

We show <strong>in</strong> Code Fragment 2.13 a class, Student, that implements Person.<br />

The method equalTo assumes that the argument (declared of type Person) is<br />

also of type Student <strong>and</strong> performs a narrow<strong>in</strong>g conversion from type Person<br />

(an <strong>in</strong>terface) to type Student (a class) us<strong>in</strong>g a cast. The conversion is allowed<br />

<strong>in</strong> this case, because it is a narrow<strong>in</strong>g conversion from class T to <strong>in</strong>terface U,<br />

where we have an object taken from T such that T extends S (or T = S) <strong>and</strong> S<br />

implements U.<br />

Code Fragment 2.13<br />

<strong>in</strong>terface Person.<br />

: Class Student implement<strong>in</strong>g<br />

Because of the assumption above <strong>in</strong> the implementation of method equalTo, we<br />

have to make sure that an application us<strong>in</strong>g objects of class Student will not<br />

attempt the comparison of Student objects with other types of objects, or<br />

otherwise, the cast <strong>in</strong> method equalTo will fail. For example, if our application<br />

manages a directory of Student objects <strong>and</strong> uses no other types of Person<br />

objects, the assumption will be satisfied.<br />

125

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

Saved successfully!

Ooh no, something went wrong!