Replacing an abstract base class with an interface in IntelliJ
I have an abstract base class with a single abstract method that I would like to get rid of. I would like to introduce a new interface with this method and make all existing subclasses to implement this interface, not extend the base class.
From:
public abstract class Base {
public abstract Foo getFoo();
}
public class Concrete extends Base {
public Foo getFoo() {
throw new UnsupportedOperationException();
}
}
To:
public interface Base {
Foo getFoo();
}
public class Concrete implements Base {
public Foo getFoo() {
throw new UnsupportedOperationException();
}
}
I tried to inline the base class with no success, created the interface manually, pulled out the member, and then deleted the empty base class, but I cannot get IntelliJ to do this automatically.
Any suggestions on how I can solve this?
source to share
Now I realize that I managed to fool myself into thinking that this is an IDEA problem. Rather, it has to do with the abstract type I was trying to inline, is used in a whole host of generics references, i.e.
final MyMapping<Base, String> = (...)
IDEA cannot guess what I would like to do with all these shared links. They have to be manually resolved and it also works. I start with:
public abstract class AbstractSomething {
public abstract void foo();
}
public class ConcreteSomething extends AbstractSomething {
@Override
public void foo() {
throw new UnsupportedOperationException();
}
}
Then I add:
public abstract class AbstractSomething implements Something {..}
and alt-enter to Something that wants to add this interface. Then I refactor-> pull elements and validate to move the abstract method to the Something interface, the result is:
public interface Something {
void foo();
}
public abstract class AbstractSomething implements Something {
}
With cursor blinking over AbstractSomething in ConcreteSomething, ctrl + alt + n for inline superclass. AbstractSomething is a goner and I stay with
public class ConcreteSomething implements Something {
@Override
public void foo() {
throw new UnsupportedOperationException();
}
}
You just have to love this IDE, it is always smarter than me.
source to share