27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

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.

General <strong>Java</strong> Questions II<br />

}<br />

public Object clone() {<br />

return super.clone();<br />

}<br />

public AnotherClass extends MyClass {<br />

SomeMember someMember;<br />

}<br />

public Object clone() {<br />

AnotherClass ac = (AnotherClass)(super.clone());<br />

if (someMember != null) {<br />

ac.someMember = (SomeMember)(someMember.clone());<br />

}<br />

return ac;<br />

}<br />

Note that the class AnotherClass, that extends MyClass, automatically becomes<br />

Cloneable, because MyClass is Cloneable.<br />

Also note, that super.clone() always returns an Object of the type of the actual object,<br />

although the superclass doesn't know anything about that sub class. The reason is,<br />

that Object.clone() is a native method, which just allocates new memory for the new<br />

object and copies the bytes to that memory. Native code has it's own ways of finding<br />

out which type to return ;-)<br />

--<br />

Karl Schmidt<br />

I was just wondering about the usefulness of Interfaces...<br />

I was just wondering about the usefulness of Interfaces. I was under the impression<br />

that interfaces could be used to perform multiple inheritance. But an interface only<br />

declares a method - in a very abstract way.<br />

A class that implements an interface needs to define its own implementation of a<br />

certain method. What is the use of having an interface when nothing is being<br />

gained...?<br />

Answer: If two classes implements the same interface, you can get a reference to the<br />

interface instead of the effective class without bother what class are you managing.<br />

This is very useful in RMI (for example) or in any condition when you have to take an<br />

object without knowing exactly his class, but only the interface that it implement.<br />

For example:<br />

public void recurseList( List l )<br />

the generic List ensure that you can use every List for this method (ArrayList,<br />

AbstractList, Vector...), so your calling method can be:<br />

ArrayList l = new ArrayList(); or<br />

Vector l = new Vector();<br />

file:///F|/350_t/350_tips/general_java-II.htm (11 of 13) [2002-02-27 21:18:24]

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

Saved successfully!

Ooh no, something went wrong!