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.

S's a() method when asked for o.a(). Polymorphism such as this is useful<br />

because the caller of o.a() does not have to know whether the object o refers to an<br />

<strong>in</strong>stance of T or S <strong>in</strong> order to get the a() method to execute correctly. Thus, the<br />

object variable o can be polymorphic, or take many forms, depend<strong>in</strong>g on the<br />

specific class of the objects it is referr<strong>in</strong>g to. This k<strong>in</strong>d of functionality allows a<br />

specialized class T to extend a class S, <strong>in</strong>herit the st<strong>and</strong>ard methods from S, <strong>and</strong><br />

redef<strong>in</strong>e other methods from S to account for specific properties of objects of T.<br />

Some object-oriented languages, such as <strong>Java</strong>, also provide a useful technique<br />

related to polymorphism, which is called method overload<strong>in</strong>g. Overload<strong>in</strong>g occurs<br />

when a s<strong>in</strong>gle class T has multiple methods with the same name, provided each one<br />

has a different signature. The signature of a method is a comb<strong>in</strong>ation of its name<br />

<strong>and</strong> the type <strong>and</strong> number of arguments that are passed to it. Thus, even though<br />

multiple methods <strong>in</strong> a class can have the same name, they can be dist<strong>in</strong>guished by a<br />

compiler, provided they have different signatures, that is, are different <strong>in</strong> actuality.<br />

In languages that allow for method overload<strong>in</strong>g, the run-time environment<br />

determ<strong>in</strong>es which actual method to <strong>in</strong>voke for a specific method call by search<strong>in</strong>g<br />

up the class hierarchy to f<strong>in</strong>d the first method with a signature match<strong>in</strong>g the method<br />

be<strong>in</strong>g <strong>in</strong>voked. For example, suppose a class T, which def<strong>in</strong>es a method a(),<br />

extends a class U, which def<strong>in</strong>es a method a(x,y). If an object o from class T<br />

receives the message "o.a(x,y)," then it is U's version of method a that is <strong>in</strong>voked<br />

(with the two parameters x <strong>and</strong> y). Thus, true polymorphism applies only to<br />

methods that have the same signature, but are def<strong>in</strong>ed <strong>in</strong> different classes.<br />

Inheritance, polymorphism, <strong>and</strong> method overload<strong>in</strong>g support the development of<br />

reusable software. We can def<strong>in</strong>e classes that <strong>in</strong>herit the st<strong>and</strong>ard <strong>in</strong>stance variables<br />

<strong>and</strong> methods <strong>and</strong> can then def<strong>in</strong>e new more-specific <strong>in</strong>stance variables <strong>and</strong> methods<br />

that deal with special aspects of objects of the new class.<br />

2.2.3 Us<strong>in</strong>g Inheritance <strong>in</strong> <strong>Java</strong><br />

There are two primary ways of us<strong>in</strong>g <strong>in</strong>heritance of classes <strong>in</strong> <strong>Java</strong>, specialization<br />

<strong>and</strong> extension.<br />

Specialization<br />

In us<strong>in</strong>g specialization we are specializ<strong>in</strong>g a general class to particular subclasses.<br />

Such subclasses typically possess an "is a" relationship to their superclass. A<br />

subclass then <strong>in</strong>herits all the methods of the superclass. For each <strong>in</strong>herited<br />

method, if that method operates correctly <strong>in</strong>dependent of whether it is operat<strong>in</strong>g<br />

for a specialization, no additional work is needed. If, on the other h<strong>and</strong>, a general<br />

method of the superclass would not work correctly on the subclass, then we<br />

should override the method to have the correct functionality for the subclass. For<br />

example, we could have a general class, Dog, which has a method dr<strong>in</strong>k <strong>and</strong> a<br />

method sniff. Specializ<strong>in</strong>g this class to a Bloodhound class would probably not<br />

99

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

Saved successfully!

Ooh no, something went wrong!