8

I have several classes implements interface MyInterface and I want all of them to extend abstract class MyAbstractClass. What is the best way to do that?

Is there any better way than create another abstract class extending MyAbstractClass and implementing MyInterface?

(I swear I haven't found any question like this before I posted)

3
  • That is the only way. Hopefully :) Commented Oct 18, 2012 at 11:23
  • 1
    No, that way sounds like the best way. Or require it in your documentation. Why do you want this behavior? Commented Oct 18, 2012 at 11:24
  • Thanks. noisecapella I have several classes implements my interface "Listener" and I want all of them to be Observable. In the end I rewrited "Listener" to be abstract class extends Observable. Commented Oct 18, 2012 at 12:09

3 Answers 3

6

The cleanest solution would be to define such a requirement in the JavaDoc of the interface. In the JavaDoc it should then state something like "to use this interface you need to extend from MyAbstractClass or provide your own implementation". This way the programmer is responsible for the implementation. Remember that this is a sociological problem which you try to solve technicality.

Another 'solution' would be to drop the interface and use an abstract class. Implementing the interface in the abstract class wouldn't make sense here.

Sign up to request clarification or add additional context in comments.

1 Comment

"Another 'solution' would be to drop the interface and use an abstract class." - That's the way. Thanks. I'd vote up if I had enough reputation :)
6

You could define MyAbstractClass to implement MyInterface, and then make all of your other classes extend MyAbstractClass:

public abstract class MyAbstractClass implements MyInterface {
    ...
}

public class OneOfSeveral extends MyAbstractClass {
    ...
}

3 Comments

Then what use does the interface have?
Thanks Jamie, but I don't want every class which extends MyAbstractClass to implement MyInterface. (I want the other way that every implementation of MyInterface extends MyAbstractClass)
siebs0r in this way which Jamie wrote there may be some classes which implements only MyInterface but don't extend MyAbstractClass. But in the way I want it the interface is really useless, so I just make another abstract class, which extends MyAbstractClass and has abstract methods which were in MyInterface.
0

You can't force all your concrete classes to extend MyAbstractClass in any other way than actually changing their definitions from

class A implements MyInterface

to

class A extends MyAbstractClass

and of course

abstract class MyAbstractClass implements MyInterface

You don't need another abstract class as you write, though.

Edit: Regarding your comment below: "I want the other way that every implementation of MyInterface extends MyAbstractClass" - you cannot enforce that sensibly. You can define another interface that MyInterface extends and call that MyAbstractClassInterface if you want but you still won't be able to enforce your existing classes extend an abstract class implementing this latter interface, although they will of course have to actually implement the methods defined in this interface...

It sounds to me that you should drop the interface and replace it with the abstract class.

Cheers,

2 Comments

Thanks. That's the same as Jamie wrote. But that's not true that I don't need any another abstract class. I may need methods from MyAbstractClass without methods from MyInterface.
Actually it is true, there is nothing preventing you from adding (abstract) methods in MyAbstractClass that are not defined in the interface and implementing the former in your concrete classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.