Intellij Idea How to add interface implementation to classes?
Asked Answered
C

4

13

For example I have many classes than implement my interface. After adding new method definition in the interface, how can I fast-add blank interface implementations for all that classes?

Crapshooter answered 18/1, 2013 at 14:46 Comment(0)
O
27

Alt+Enter on the new method in the interface, Implement method:

enter image description here

Press Enter, the list of implementation classes will be shown, select the desired classes using Shift+arrow keys or press Ctrl+A to select all of them, then press Enter again to confirm the choice. Stub implementations will be added to all the selected classes.

Osman answered 22/1, 2013 at 10:12 Comment(0)
D
6

I think the best you can do comes from their code generation tutorial, particularly by using

Ctrl+O

in an implementing class. Otherwise I'm not aware of a way to generate an entire class.

Doublepark answered 18/1, 2013 at 14:50 Comment(2)
That's the question. I know that I can go through all classed and insert implementation. But I want ability to implement methods for all classes from interface.Crapshooter
@Jack, right. I believe that is not possible, and this is the closest alternative.Doublepark
C
5

You can generate an implementation of an interface by moving the caret to the name of the interface and pressing option+return (on Mac) or ALT+Enter (on PC), then hovering over Implement interface and pressing return/Enter:

Implement interface Please note that my caret can't be seen but it's between the h and a of Shape

IntelliJ will then prompt you for a class name and which methods to include in the implementation, then generate a class, like this:

enter image description here

Cameron answered 25/10, 2016 at 22:17 Comment(0)
C
0

In IntellyJ IDEA 12 you can use Push Members Down dialog:

Refactor -> Pull Members Down... 

Then select methods you want to push to subclasses and click "Refactor": http://clip2net.com/s/i6DK41

Note that it doesn't generate the stub method by base class - it just moves it as is. So you'll need to implement the stub by yourself in the parent (root) class, e.g.:

public boolean getRequiredDatabaseNameWhenPartitioned() {
  return true;
}

then use "Push members down", refactor, and finally make the method of the parent class abstract manually

Chromatin answered 12/5, 2014 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.