Extract interface
With the Extract Interface refactoring you have the following options:
- Create an interface based on the methods of a class.
- Rename the original class, and it implements the newly created interface. In such case, IntelliJ IDEA changes all usages of the original class to use the interface where possible.
In addition, static final fields, declared in the initial class, can be moved to an interface. As a result, an interface will be created containing the specific methods and fields. Thereby, the specified class methods become implementations of the corresponding interface methods.
Examples
Here we have a class, and perform Extract Interface refactoring to create an interface based on the methods of the class.
// File AClass.java class AClass < public static final double CONSTANT = 3.14; public void publicMethod() < >public void secretMethod() < >>
// File AClass.java class AClass implements AnInterface < public void publicMethod() < >public void secretMethod() < >// File AnInterface.java public interface AnInterface < double CONSTANT = 3.14; void publicMethod(); >>
Another example of the Extract Interface refactoring, when the Rename original class and use interface where possible option is selected.
public class FormerAClass implements AClass < public void publicMethod() < >public void secretMethod() < >>
public interface AClass < double CONSTANT=3.14; void publicMethod(); >
You can extract an interface from the class that already implements another interface. Let’s extract interface from the class that implements AnInterface . Depending on whether we want AnotherInterface (extracted interface) to extend the AnInterface (existing one) or we want source AClass to implement them both, we will get the following code:
Extracted Interface extends the existing one:
class AClass implements AnotherInterface < public void publicMethod() < //some code here >public void secretMethod() < //some code here >>
public interface AnotherInterface extends AnInterface < >
Source class implements both interfaces.
class AClass implements AnInterface, AnotherInterface < public void publicMethod() < //some code here >public void secretMethod() < //some code here >>
public interface AnotherInterface < >
Extract an interface
- Select a class in the Project view, Structure view, or place the caret anywhere within a class in the editor.
- On the main menu or from the context menu of the selection, choose Refactor | Extract | Interface . The Extract Interface dialog appears.
- To extract a new interface, select the Extract Interface option and specify the name for the new interface. To rename the original class and make it an implementation of the newly created interface, select the Rename original class and use interface where possible option and specify the new name for the original class. IntelliJ IDEA will alter all original class usages to the usages of the implementing only where it is still necessary.
- Specify the package, where the new interface will be located.
- Select the class members you want to be listed in the interface in the Members to form interface area. The list shows all the methods of the class, as well as final static fields (constants).
- In the JavaDoc area, select the action to be applied on the JavaDoc .
- To leave it where it is, select the As is option.
- To copy it to the extracted interface, select the Copy option.
- To move it to the extracted interface, select the Move option.
- Click Refactor to proceed.
- Click Refactor when ready. If IntelliJ IDEA shows you a Refactoring Preview in the Find tool window, review the suggested changes. To have the interface extracted and the proposed changes applied, click Do Refactor .
Как добавить Interface в пакет?
Установил новую версию IntelliJ Idea (Community Edition 2017.3.4×64). В предыдущей версии IntelliJ все было просто и понятно: New ⇒ Class ⇒ имя класса ⇒ Interface . А теперь что-то поменялось. Открывается New ⇒ File ⇒ Enter a file name ⇒ Ввожу имя файла – нет возможности выбрать опцию Interface. Столкнулся с неожиданной проблемой и не знаю что делать. Существует ли возможность еще как-то добавлять Interface в пакет, а не только на пакете Правая клавиша ⇒ New ?
Отслеживать
user328896
задан 30 янв 2018 в 21:42
1 3 3 бронзовых знака
2 ответа 2
Сортировка: Сброс на вариант по умолчанию
New ⇒ Java Class ⇒ Вводим имя и выбираем тип класса. Выглядит это примерно так:

Версия: 2020.1.1
Creating a Project with IntelliJ IDEA
With both Community Edition and Ultimate Edition, you can create a Vaadin application most easily with a Maven archetype and deploy it to a server with a Maven run/debug configuration.
Creating a Maven Project
You can choose to create a Maven project in IntelliJ IDEA. You will not have the application server integration, but can deploy the application to an application server using a run/debug configuration.
- Select New Project
- In the New Project window, select Maven
- Select the Java SDK to be used for the project. Vaadin requires at least Java 8.
- Check Create from archetype checkbox
- If the Vaadin archetype is not in the list, click Add archetype , enter GroupId com.vaadin , ArtifactId vaadin-archetype-application , and Version LATEST (or a specific version number).
Click OK in the dialog.

Click Next .
Creating the Maven project takes some time as Maven fetches the dependencies. Once done, the project is created and the Maven POM is opened in the editor.
Fort more detailed instructions, refer to https://www.jetbrains.com/help/idea/
Compiling the Project
To compile a Vaadin application using Maven, you can define a run/debug configuration to execute a goal such as package to build the deployable WAR package. It will also compile the widget set and theme, if necessary. See «Compiling and Running the Application» for more details.
Compilation is included in the following instructions for deploying the application.
Deploying to a Server
There exists Maven plugins for deploying to various application servers. For example, to deploy to Apache Tomcat, you can to configure the tomcat-maven-plugin and then execute the tomcat:deploy goal. See the documentation of the plugin that you use for more details. If no Maven plugin exists for a particular server, you can always use some lower-level method to deploy the application, such as running an Ant task.
In the following, we create a run/debug configuration to build, deploy, and launch a Vaadin Maven application on the light-weight Jetty web server.
- Select Run›Edit Configurations .
- Click + and select Maven to create a new Maven run/debug configuration.
- Enter a Name for the run configuration. For the Command line , enter package jetty:run to first compile and package the project, and then launch Jetty to run it.
IntelliJ IDEA Community Edition Essential Training
Get started with IntelliJ IDEA, one of the most popular IDEs for programming with Java and other Java virtual machine (JVM) languages. In this course, David Gassner explains how to install how to install JetBrains Toolbox and IntelliJ IDEA, configure the Java Development Kit (JDK), and create new projects. He also walks through the IntelliJ IDEA user interface and demonstrates the IDE’s powerful code generation and debugging tools. Plus, he explains how to work with Git and GitHub to manage your source code, and how to program with other popular JVM languages such as Kotlin, Groovy and Scala.
Показать еще Свернуть
Навыки, которые вы получите
Получайте сертификат, которым можно поделиться
Расскажите, чему вы научились, и станьте выдающимся специалистом в интересующей вас отрасли, получив сертификат, который демонстрирует ваши знания, приобретенные в рамках курса.
Learning LinkedIn Learning
Сертификат об окончании курса