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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Doing the above prevents the problem of having the semaphore file left around when<br />

the system crashes. I implemented it recently in one of our apps, and it works like a<br />

charm.<br />

--<br />

Burt Johnson<br />

MindStorm Productions, Inc.<br />

http://www.mindstorm-inc.com<br />

Q: Can you call a class static abstract method from an abstract class or does it<br />

need to be extended and then called from its concrete class?<br />

I've been told that abstract classes do not actually have any code in them cause they<br />

are like a placeholder, so I guess you wouldn't bother calling a static method in an<br />

abstract class cause it wouldn't have any code to begin with....?<br />

Answer: You have been told wrong. Abstract classes can and do have code in them.<br />

See, for example, java.awt.Component, an abstract class with a lot of code and no<br />

abstract methods at all. If a class has any abstract method member, directly declared<br />

or inherited, it is required to be declared abstract. If not, it is the programmer's<br />

decision and should be based on whether it would make sense to have an instance<br />

of that class.<br />

Perhaps whoever told you was confusing abstract classes with interfaces, which<br />

don't contain implementation, only abstract method and constant declarations.<br />

You cannot declare a method to be both static and abstract. Abstract requires the<br />

method to be overridden before you can have a concrete class, static prevents<br />

overriding. You can have a static method in an abstract class - such a method could<br />

be called without creating an instance of the class, the only thing that is prohibited for<br />

abstract classes.<br />

And when a subclass of an abstract method has been instantiated, all the methods<br />

from the original class will keep the same code in the instance. Most of the time an<br />

abstract class will have abstract methods.<br />

However, there are several examples of abstract classes that don't have any abstract<br />

methods at all. Some examples are Component and FontMetrics from the AWT. It<br />

doesn't make sense to have just a Component that's not a specific type of<br />

component. It doesn't make sense to have a FontMetrics that doesn't measure any<br />

specific kind of Font.<br />

Also being abstract never prevents overriding, it just requires overriding in order to<br />

derive a non-abstract subclass. And if a class is a subclass of an abstract class, it<br />

only MUST override those methods declared abstract. The other methods do not<br />

require overriding.<br />

Q: I write java about 2 years, but I still confuse one thing that is why should we<br />

use interface???<br />

If I need to implement an interface and just use its every methods name.<br />

Why shouldn't just write every methods statments in a class, not in interface??<br />

I only can think about that if I extend a class, than can implement another or the<br />

others interface.<br />

As you can saw, I really confuse about this. And I do see many books for the reasons<br />

, but I can't get the answer, please tell me !<br />

Answer: "Interface" is the <strong>Java</strong> way to do multiple inheritance, or a better way to think<br />

file:///F|/350_t/350_tips/general_java-IV.htm (7 of 10) [2002-02-27 21:18:34]

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

Saved successfully!

Ooh no, something went wrong!