Files
openide/java/java-impl/resources/inspectionDescriptions/ClassMayBeInterface.html
2025-02-05 04:43:28 +00:00

48 lines
1.3 KiB
HTML

<html>
<body>
Reports <code>abstract</code> classes that can be converted to interfaces.
<p>Using interfaces instead of classes is preferable as Java doesn't support multiple class inheritance,
while a class can implement multiple interfaces.</p>
<p>A class may be converted to an interface if it has no superclasses (other
than Object), has only <code>public static final</code> fields,
<code>public abstract</code> methods, and <code>public</code> inner classes.
<!-- tooltip end -->
<p>Example:</p>
<pre><code>
abstract class Example {
public static final int MY_CONST = 42;
public abstract void foo();
}
class Inheritor extends Example {
@Override
public void foo() {
System.out.println(MY_CONST);
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
interface Example {
int MY_CONST = 42;
void foo();
}
class Inheritor implements Example {
@Override
public void foo() {
System.out.println(MY_CONST);
}
}
</code></pre>
<p>Configure the inspection:</p>
<p>
Use the <b>Report classes containing non-abstract methods when using Java 8</b> option to report only the classes with <code>static</code> methods and non-abstract methods that can be converted to
<code>default</code> methods (only applicable to language level of 8 or higher).
<p>
</body>
</html>