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.

Consider the <strong>in</strong>heritance of classes from Exercise R-2.5, <strong>and</strong> let d be an object<br />

variable of type Horse. If d refers to an actual object of type Equestrian,<br />

can it be cast to the class Racer? Why or why not?<br />

R-2.10<br />

Give an example of a <strong>Java</strong> code fragment that performs an array reference that<br />

is possibly out of bounds, <strong>and</strong> if it is out of bounds, the program catches that<br />

exception <strong>and</strong> pr<strong>in</strong>ts the follow<strong>in</strong>g error message: "Don't try buffer<br />

overflow attacks <strong>in</strong> <strong>Java</strong>!"<br />

R-2.11<br />

Consider the follow<strong>in</strong>g code fragment, taken from some package:<br />

public class Maryl<strong>and</strong> extends State {<br />

Maryl<strong>and</strong>() { /* null constructor */ }<br />

public void pr<strong>in</strong>tMe() { System.out.pr<strong>in</strong>tln("Read<br />

it."); }<br />

public static void ma<strong>in</strong>(Str<strong>in</strong>g[] args) {<br />

Region mid = new State();<br />

State md = new Maryl<strong>and</strong>();<br />

Object obj = new Place();<br />

Place usa = new Region();<br />

md.pr<strong>in</strong>tMe();<br />

mid.pr<strong>in</strong>tMe();<br />

((Place) obj).pr<strong>in</strong>tMe();<br />

obj = md;<br />

((Maryl<strong>and</strong>) obj).pr<strong>in</strong>tMe();<br />

obj = usa;<br />

((Place) obj).pr<strong>in</strong>tMe();<br />

usa = md;<br />

132

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

Saved successfully!

Ooh no, something went wrong!