abstract class without absract method

Folks:I know it's legal to create an abstract class with out any abstract method. I have seen some code like that written by somebody else. But I really don't know the purpose of doing it. Can anybody give me a hint?Thanks a lot!
[259 byte] By [chenG] at [2007-9-30 20:24:06]
# 1
Either because the base type is simply not appropriate for instantiation, just for typing (like data type, not like keyboard) reasons, or because it's expected that subclasses will add more methods of their own. Or both.
nasch_ at 2007-7-7 1:08:42 > top of Java-index,Java Essentials,Java Programming...
# 2

The only reason I can think of is as a flag to somebody using it that it doesn't do much, and you're supposed to provide your own implementation.

I'm not sure, but I think Swings XxxAdapter classes that implement listeners with empty methods might be abstract. They do nothing, and you're supposed to provide "real" implementations for one or more of the methods, but you wouldn't have to. That wouldn't be a very useful class, so the base class might as well be abstract.

jverd at 2007-7-7 1:08:42 > top of Java-index,Java Essentials,Java Programming...
# 3
I just asked the author of that piece of code. His reply is that "I don't want somebody to create an instance of that abstract class but I allow it to be subclassed". Thanks, folks.
chenG at 2007-7-7 1:08:42 > top of Java-index,Java Essentials,Java Programming...
# 4

> The only reason I can think of is as a flag to

> somebody using it that it doesn't do much, and you're

> supposed to provide your own implementation.

>

> I'm not sure, but I think Swings XxxAdapter classes

> that implement listeners with empty methods might be

> abstract. They do nothing, and you're supposed to

> provide "real" implementations for one or more of the

> methods, but you wouldn't have to. That wouldn't be a

> very useful class, so the base class might as well be

> abstract.

its funny that you mention this! i was just having problems with the listeners because eveyr time i type "implement actionListener" my IDE tells me that i need to declare my public class as being abstract. but once i type the method: public void actionListener(actionEvent e){} then that error goes away

just thought i'd give my two cents and support what jverd is saying (not like he needs support :)

moerderin at 2007-7-7 1:08:43 > top of Java-index,Java Essentials,Java Programming...
# 5

Two examples of abstract classes with no abstract methods, and a simple way to verify that:

import java.awt.*;

import javax.swing.*;

public class Example {

public static void main(String[] args) {

new Component(){};

new JComponent(){};

}

}

DrLaszloJamf at 2007-7-7 1:08:43 > top of Java-index,Java Essentials,Java Programming...
# 6

> > The only reason I can think of is as a flag to

> > somebody using it that it doesn't do much, and

> you're

> > supposed to provide your own implementation.

> >

> > I'm not sure, but I think Swings XxxAdapter classes

> > that implement listeners with empty methods might be

> > abstract. They do nothing, and you're supposed to

> > provide "real" implementations for one or more of

> the

> > methods, but you wouldn't have to. That wouldn't be

> a

> > very useful class, so the base class might as well

> be

> > abstract.

>

> its funny that you mention this! i was just having

> problems with the listeners because eveyr time i type

> "implement actionListener" my IDE tells me that i need

> to declare my public class as being abstract. but

> once i type the method: public void

> actionListener(actionEvent e){} then that error goes

> away

>

> just thought i'd give my two cents and support what

> jverd is saying (not like he needs support :)

Okay, but that's actually not what the OP was asking about or what I was talking about.

He was asking about an abstract class (a class declared as abstract) whose methods are all concrete--i.e. they all have implementations--and why make that class abstrac then.

What you're talking about is a class that's not declared abstract (your "implements ActionListener" class) but that does have abstract (unimplemented) methods.

The OP's case--a class declared abstract but with no abstract methods--is legal, but its value is non-intuitive.

Your case--a class that has abstract methods but is not declared abstract, which is the opposite of the OP's case--is not legal.

jverd at 2007-7-7 1:08:43 > top of Java-index,Java Essentials,Java Programming...