improbable
improbable

Reputation: 2934

Is there an option in "Refactor" context menu in IntelliJ IDEA to replace current empty superclass with an interface?

I have an empty superclass.

public class SampleClass implements Serializable {

}

This class is extended by a lot of other classes. I was curious: is there a possibility to make this class an interface in a way that this change will be reflected in all subclasses automatically? I tried to find an option to do that in the "Refactor" context menu, but all of them seem to do something different from what I imply to do.

Upvotes: 0

Views: 229

Answers (2)

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26462

Add the abstract modifier to the superclass and then Alt+Enter on the name of the superclass and invoke Convert class to interface.

This quick fix is provided by the "Abstract class may be interface" inspection which is enabled by default in "No highlighting, only fix" mode. But if you have disabled it, you will need to enable it again.

Upvotes: 4

Jan Thomä
Jan Thomä

Reputation: 13604

You can do it in a two step process. First extract the interface from your base class using the "Extract interface" refactoring (here I create an interface BaseClass and then name the original base class MyAbstractBaseClass:

enter image description here

Then you can use the "Inline Super Class" refactoring to get rid of MyAbstractBaseClass:

enter image description here

Upvotes: 1

Related Questions