Reputation: 13
Is there a easy way to extract a interface from a Java class such that it includes public methods of super classes. The class Im trying to extract the interface has several layers of super classes, each one with lots of methods. I don't need an interface for each of those parent classes.
Currently I'm using InteliJ idea IDE. It only extracts public methods of current class, when extracting an interface. If U guys can tell me a tool or an easy way to extract interface, it would be a great help.
Upvotes: 1
Views: 1336
Reputation: 114767
Eclipse doesn't offer superclass methods in the "Extract Interface" refactoring. And this is correct, because you really shouldnt implement an interface and implement the methods in a superclass!
You want something like this:
public interface InterfaceForB {
void b();
void a();
}
public class A {
public void a(){}
}
public class B extends A implements InterfaceForB{
public void b(){}
}
Yes, it compiles. But it's awful style, because it says, B
implements the interface, which is just not true, because the a()
method is implemented in class A
.
My strong advice: use the refactorings as offered by IntelliJ, only extract methods to an interface that are really implemented by the actual class. Implement B
for the given example so that it implements all interface methods:
public class B extends A implements InterfaceForB{
public void b(){}
public void a(){ super(); }
}
And now IntelliJ (and eclipse) can extract the interface the way you want it.
Upvotes: 1
Reputation: 10939
Eclipse has a tool called the "type hierarchy" which lets you view the members of only that class, the inheritance structure (including interfaces) of that class (as well as the members of that respective class/interface), and also any classes which extend/implement your class.
There are also refactory tools for extracting interfaces/super classes.
Upvotes: 0