Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use an empty abstract class instead of an interface?

I use GWT and the Place & Activity mechanism.

It's really sad that Place is a class because my custom place can't extend another class.

When I look at the Place code, I see the following :

public abstract class Place {

  /**
   * The null place.
   */
  public static final Place NOWHERE = new Place() {
  };

}

Seeing that, the Place can be an interface. Is there a good reason that GWT team has chosen to make Place an abstract class instead of an interface ?

And to generalize : is there a good reason to create really empty abstract class vs interface ?

like image 685
Jerome Cance Avatar asked Apr 12 '12 11:04

Jerome Cance


People also ask

Why do we need abstract class rather than interface?

An abstract class is used if you want to provide a common, implemented functionality among all the implementations of the component. Abstract classes will allow you to partially implement your class, whereas interfaces would have no implementation for any members whatsoever.

When would you use an abstract class instead of an interface?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. If you are designing small, concise bits of functionality, use interfaces.

What is the use of empty abstract class in Java?

A empty abstract class can be used to group together classes. This is in order to show some intent and to ensure the single responsibility principle that a class should have just one purpose. The example you give uses an interface, not an empty abstract class.

Why would you use an abstract class over an interface Java?

If we want to provide common, implemented functionality among all implementations of our component, use an abstract class. Abstract classes allow us to partially implement our class, whereas interfaces contain no implementation for any members.


2 Answers

In general I would not define an empty abstract class

However, when I would expect some members in the future, I may decide to use abstract class instead of interface

"class" means: this IS A

"interface" means: this SUPPORTS

For "Place" I could really see both with a little intuitive preference for class

like image 125
stefan bachert Avatar answered Nov 14 '22 21:11

stefan bachert


Is there a good reason that GWT team has chosen to make Place an abstract class instead of an interface ?

I can't think of one. But to be realistic, complaining about it on SO is not going to achieve anything.

And to generalize : is there a good reason to create really empty abstract class vs interface ?

Hypothetically, you might do this to ensure that there is a single common base class for future anticipated requirements ... or for some other reason.

But generally speaking it is a bad idea ... IMO.

(Of course, things like this often happen for historical reasons; e.g. the abstract class may have had more members in the past that have since been removed.)

like image 26
Stephen C Avatar answered Nov 14 '22 21:11

Stephen C