General Java Questions I
Q: Can you call a class static abstract method from an abstract class or does it
need to be extended and then called from its concrete class?
I ve been told that abstract classes do not actually have any code in them cause they
are like a placeholder, so I guess you wouldn t bother calling a static method in an
abstract class cause it wouldn t have any code to begin with....?
Answer: You have been told wrong. Abstract classes can and do have code in them.
See, for example, java.awt.Component, an abstract class with a lot of code and no
abstract methods at all. If a class has any abstract method member, directly declared
or inherited, it is required to be declared abstract. If not, it is the programmer s
decision and should be based on whether it would make sense to have an instance of
that class.
Perhaps whoever told you was confusing abstract classes with interfaces, which don t
contain implementation, only abstract method and constant declarations.
You cannot declare a method to be both static and abstract. Abstract requires the
method to be overridden before you can have a concrete class, static prevents
overriding. You can have a static method in an abstract class such a method could
be called without creating an instance of the class, the only thing that is prohibited for
abstract classes.
And when a subclass of an abstract method has been instantiated, all the methods
from the original class will keep the same code in the instance. Most of the time an
abstract class will have abstract methods.
However, there are several examples of abstract classes that don t have any abstract
methods at all. Some examples are Component and FontMetrics from the AWT. It
doesn t make sense to have just a Component that s not a specific type of
component. It doesn t make sense to have a FontMetrics that doesn t measure any
specific kind of Font.
Also being abstract never prevents overriding, it just requires overriding in order to
derive a non abstract subclass. And if a class is a subclass of an abstract class, it
only MUST override those methods declared abstract. The other methods do not
require overriding.
Q: I write java about 2 years, but I still confuse one thing that is why should we
use interface???
If I need to implement an interface and just use its every methods name.
Why shouldn t just write every methods statments in a class, not in interface??
I only can think about that if I extend a class, than can implement another or the
others interface.
As you can saw, I really confuse about this. And I do see many books for the reasons
, but I can t get the answer, please tell me !
Answer: "Interface" is the Java way to do multiple inheritance, or a better way to think
of it is as a way to design plug ins.
For example, let s say we have an application that monitors a network of computers.
Our monitors might check for web pages, or they may check for other ports, or they
may have hooks for hardware checks.
The interface to our main control panel is always the same: We need some means to
poll the monitor object for an answer. This is the "NetworkMonitor" interface and all
file:///F|/a_jsite/350_tips/general_java I.htm (30 of 33) [2001 07 08 11:24:51]
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved