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.

send a "message" to o that <strong>in</strong>vokes one of o's methods, for example, for o to pr<strong>in</strong>t<br />

a description of itself, for o to convert itself to a str<strong>in</strong>g, or for o to return the value<br />

of one of its data fields. The secondary way that p can <strong>in</strong>teract with o is for p to<br />

access one of o's fields directly, but only if o has given other objects like p<br />

permission to do so. For example, an <strong>in</strong>stance of the <strong>Java</strong> class Integer stores,<br />

as an <strong>in</strong>stance variable, an <strong>in</strong>teger, <strong>and</strong> it provides several operations for access<strong>in</strong>g<br />

this data, <strong>in</strong>clud<strong>in</strong>g methods for convert<strong>in</strong>g it <strong>in</strong>to other number types, for<br />

convert<strong>in</strong>g it to a str<strong>in</strong>g of digits, <strong>and</strong> for convert<strong>in</strong>g str<strong>in</strong>gs of digits to a number.<br />

It does not allow for direct access of its <strong>in</strong>stance variable, however, for such<br />

details are hidden.<br />

Dynamic Dispatch<br />

When a program wishes to <strong>in</strong>voke a certa<strong>in</strong> method a() of some object o, it<br />

sends a message to o, which is usually denoted, us<strong>in</strong>g the dot-operator syntax<br />

(Section 1.3.2), as "o.a()." In the compiled version of this program, the code<br />

correspond<strong>in</strong>g to this <strong>in</strong>vocation directs the run-time environment to exam<strong>in</strong>e o's<br />

class T to determ<strong>in</strong>e if the class T supports an a() method, <strong>and</strong>, if so, to execute<br />

it. Specifically, the run-time environment exam<strong>in</strong>es the class T to see if it def<strong>in</strong>es<br />

an a() method itself. If it does, then this method is executed. If T does not def<strong>in</strong>e<br />

an a() method, then the run-time environment exam<strong>in</strong>es the superclass S of T. If<br />

S def<strong>in</strong>es a(), then this method is executed. If S does not def<strong>in</strong>e a(), on the<br />

other h<strong>and</strong>, then the run-time environment repeats the search at the superclass of<br />

S. This search cont<strong>in</strong>ues up the hierarchy of classes until it either f<strong>in</strong>ds an a()<br />

method, which is then executed, or it reaches a topmost class (for example, the<br />

Object class <strong>in</strong> <strong>Java</strong>) without an a() method, which generates a run-time error.<br />

The algorithm that processes the message o.a() to f<strong>in</strong>d the specific method to<br />

<strong>in</strong>voke is called the dynamic dispatch (or dynamic b<strong>in</strong>d<strong>in</strong>g) algorithm, which<br />

provides an effective mechanism for locat<strong>in</strong>g reused software. It also allows for<br />

another powerful technique of object-oriented programm<strong>in</strong>g—polymorphism.<br />

2.2.2 Polymorphism<br />

Literally, "polymorphism" means "many forms." In the context of object-oriented<br />

design, it refers to the ability of an object variable to take different forms. Objectoriented<br />

languages, such as <strong>Java</strong>, address objects us<strong>in</strong>g reference variables. The<br />

reference variable o must def<strong>in</strong>e which class of objects it is allowed to refer to, <strong>in</strong><br />

terms of some class S. But this implies that o can also refer to any object belong<strong>in</strong>g<br />

to a class T that extends S. Now consider what happens if S def<strong>in</strong>es an a() method<br />

<strong>and</strong> T also def<strong>in</strong>es an a() method. The dynamic dispatch algorithm for method<br />

<strong>in</strong>vocation always starts its search from the most restrictive class that applies. When<br />

o refers to an object from class T, then it will use T's a() method when asked for<br />

o.a(), not S's. In this case, T is said to override method a() from S. Alternatively,<br />

when o refers to an object from class S (that is not also a T object), it will execute<br />

98

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

Saved successfully!

Ooh no, something went wrong!