views:

1398

answers:

7

Are there some practical programming situations for someone to declare a class abstract when all the methods in it are concrete?

A: 

An abstract class is a class that is declared abstract - it may or may not include abstract methods. They cannot be instantiated so if you have an abstract class with concrete methods then it can be subclassed and the subclass can then be instantiated.

andHapp
That's correct, but it is not the answer to the question.
Gamecat
+7  A: 

Well you could be using a template method pattern where there are multiple override points that all have default implementations but where the combined default implementations by themselves are not legal - any functional implementation must subclass.

(And yes, I dislike the template method pattern ;)

krosenvold
What's to dislike about the Template Method pattern? It certainly can be misused, but when used wisely, I find it to be quite helpful.
Ilja Preuß
It locks down design very strongly and causes nasty workarounds when things turn out to be not quite the way you thought. It also tends to create very polluted class hierarchies as well as lots of weird couplings.
krosenvold
A: 

Nice question :)

One thing is for sure ... this is certainly possible. The template suggestion by krosenvold is one good reason for doing this.

I just want to say that a class must not be declared abstract just for preventing it's instantiation.

This is referred in the Java Language Specification Section 8.1.1.1

bruno conde
+2  A: 

Immagine an interface whose declared methods usually show the same default behavior when implemented. When writing a class that needs to support the interface you have to define said default behavior over and over.

To facilitate implementation of your concrete classes you might want to provide an abstract class providing default behavior for each method. To support the interface in a concrete class you can derive from the abstract class and override methods if they deviate from the standard behavior. That way you'll avoid the repeated implementation of the same (redundant) default behavior.

lithander
A: 

When you have an important class but the system cannot create an instance fo this class, because

  • this class is father of a lot of classes of the system;
  • this has a lot of responsability (methods used by a lot of class) for domain's requires;
  • this class not represents a concrete object;
alepuzio
+2  A: 

Another possible use case is a decorator which delegates all calls to the wrapped instance. A concrete decorator implementation can override only those methods where functionality is added:

public interface Foo {
    public void bar();
}
public abstract class FooDecorator implements Foo {
    private final Foo wrapped;
    public FooDecorator(Foo wrapped) { this.wrapped = wrapped; }
    public void bar() { wrapped.bar(); }
}
public class TracingFoo extends FooDecorator {
    //Omitting constructor code...
    public void bar() {
        log("Entering bar()");
        super.bar();
        log("Exiting bar()");
    }
}

Although I don't really see the necessarity to declare FooDecorator as abstract (non-abstract example: HttpServletRequestWrapper).

Markus
+1  A: 

Previous answers already hit the main issues, but there's a minor detail that might be worth mentioning.

You could have a factory that returns instances of (hidden) subclasses of the abstract class. The abstract class defines the contract on the resulting object, as well as providing default implementations, but the fact that the class is abstract both keeps it from being instantiated directly and also signals the fact that the identity of the "real" implementation class is not published.

joel.neely