About abstract class and abstract method

S

Sameer

Consider the declaration from Java API:

public abstract class Toolkit extends Object

This abstract class java.awt.Toolkit contains one abstract method beep.

public abstract void beep()

The Java Tutorial says that-
"An abstract class can contain abstract methods - methods with no
implementation. In this way, an abstract class can define a complete
programming interface for its subclasses but allows its subclasses to
fill in the implementation details of those methods."

But we can make instance of Toolkit and make use of this method.
Toolkit t= new Toolkit();
t.beep();

How can we make instance of this abstract class and make use of one of
its abstract method?
Who fill 'implementation details' for the beep method?

-Sameer
 
T

Thomas Fritsch

Sameer said:
Consider the declaration from Java API:

public abstract class Toolkit extends Object

This abstract class java.awt.Toolkit contains one abstract method beep.

public abstract void beep()

The Java Tutorial says that-
"An abstract class can contain abstract methods - methods with no
implementation. In this way, an abstract class can define a complete
programming interface for its subclasses but allows its subclasses to
fill in the implementation details of those methods."

But we can make instance of Toolkit and make use of this method.
Toolkit t= new Toolkit();
t.beep();
You can *not* do this. The compiler-error is "Class java.awt.Toolkit is
abstract and therefore cannot be instantiated"
How can we make instance of this abstract class and make use of one of
its abstract method?
Who fill 'implementation details' for the beep method?
You can do for example:
Toolkit t= Toolkit.getDefaultToolkit();
t.beep();
You will get a non-abstract subclass of java.awt.Toolkit here.
On Windows you get a sun.awt.windows.WToolkit, which has an
implementation for beep()
 
S

Sameer

If I subclass the Toolkit class and try to write implementation of
beep() method, what may be the coding?
Is it depends on the operating system for implementation?

Can anybody provide some examples which classes the abstract class of
Java and provide some useful implementation for the abstract methods?

Java subclasses of the abstract classes do this? Then why may be the
abstract classes designed?
 
R

Roedy Green

This abstract class java.awt.Toolkit contains one abstract method beep.

Some Sun classes, such as java.awt.Toolkit are abstract or only have
private constructors. This is infuriating. Where do you get one of
these objects from??

The key is SOMEWHERE in there is a static factory method that will
create such objects for you. For the beep problem see
http://mindprod.com/jgloss/beep.html


You often seen this pattern when Sun wanted to make implementations
pluggable. The implementation can be replaced at run time by one
tuned to the platform or by a third party enhancement.

The entire JCE works this way.

What we need is a browsing tool of the JavaDocs what will find those
factories for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top