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.

Each of the pieces of this declaration have important uses, which we describe <strong>in</strong><br />

detail <strong>in</strong> this section. The modifiers part <strong>in</strong>cludes the same k<strong>in</strong>ds of scope modifiers<br />

that can be used for variables, such as public, protected, <strong>and</strong> static, with similar<br />

mean<strong>in</strong>gs. The type part of the declaration def<strong>in</strong>es the return type of the method.<br />

The name is the name of the method, which can be any valid <strong>Java</strong> identifier. The<br />

list of parameters <strong>and</strong> their types declares the local variables that correspond to the<br />

values that are to be passed as arguments to this method. Each type declaration type i<br />

can be any <strong>Java</strong> type name <strong>and</strong> each parameter i can be any <strong>Java</strong> identifier. This list<br />

of parameters <strong>and</strong> their types can be empty, which signifies that there are no values<br />

to be passed to this method when it is <strong>in</strong>voked. These parameter variables, as well<br />

as the <strong>in</strong>stance variables of the class, can be used <strong>in</strong>side the body of the method.<br />

Likewise, other methods of this class can be called from <strong>in</strong>side the body of a<br />

method.<br />

When a method of a class is called, it is <strong>in</strong>voked on a specific <strong>in</strong>stance of that class<br />

<strong>and</strong> can change the state of that object (except for a static method, which is<br />

associated with the class itself). For example, <strong>in</strong>vok<strong>in</strong>g the follow<strong>in</strong>g method on<br />

particular gnome changes its name.<br />

public void renameGnome (Str<strong>in</strong>g s) {<br />

name = s; // Reassign the name <strong>in</strong>stance variable<br />

of this gnome.<br />

}<br />

Method Modifiers<br />

As with <strong>in</strong>stance variables, method modifiers can restrict the scope of a method:<br />

• public: Anyone can call public methods.<br />

• protected: Only methods of the same package or of subclasses can call a<br />

protected method.<br />

• private: Only methods of the same class (not methods of a subclass) can<br />

call a private method.<br />

• If none of the modifiers above are used, then the method is friendly.<br />

Friendly methods can only be called by objects of classes <strong>in</strong> the same package.<br />

The above modifiers may be preceded by additional modifiers:<br />

• abstract: A method declared as abstract has no code. The signature of<br />

such a method is followed by a semicolon with no method body. For example:<br />

public abstract void setHeight (double newHeight);<br />

36

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

Saved successfully!

Ooh no, something went wrong!