Java9 - How to declare Private Static methods in interface | java9 new features
In this blog post, Learn the java9 interface private methods and static methods with examples. Java released many versions. This post is about how the interface is evolved from public modifier to private modifier and abstract methods only to non-abstract methods.
java7 Interface declaration version examples
The interface has only constants
and abstract
methods before the java 7 version.
Examples are
- public static final variables i.e constants
- public abstract methods - No implementation with method header.
public interface MyInterface {
public String message = "Hello";
public void getMessage();
}
In the above interface example,
We have defined one constant
and one abstract
method.
There is no implementation for methods. The method has only declarations i.e. called abstract.
If you want to have a non-abstract method, Then we can choose an Abstract class with abstract methods.
java8 interface default and static methods with an example
With the Java8 version, interface introduced a few features to it.
The Public Default
method and public Static
methods can be declared inside an interface.
So, We can declare the following methods to an interface.
- public static final variables
- public abstract methods
- public Default methods
- public static methods
Here is an example
public interface MyInterface {
public String CONSTANT = "constant";
public void abstractMethod();
// default method example
public default void defaultMethod() {
System.out.println("defaultMethod");
}
// static method example
public static void staticMethod() {
System.out.println("staticMethod");
}
}
public class TestInterface implements MyInterface{
public static void main(String[] args) {
TestInterface t=new TestInterface();
t.abstractMethod();
t.defaultMethod();
MyInterface.staticMethod();
}
@Override
public void abstractMethod() {
System.out.println("abstractMethod");
}
}
Output:
abstractMethod
defaultMethod
staticMethod
Default methods
can be overridden, and called using an instance
of the implementation class.
Static methods
can not be overridden, and called using interface name.
Java9 interface private and static methods example
With the java9 version, You can add private methods and private static method to an interfaces
.
For example, You have written a common code in many default
methods in the interface.
the redundant code is not a good way of design.
So we will move redundant code to private
methods to allow reusability.
However, Private
methods allow reusing the code in an interface.
Implementation classes will have access to these private
methods and below features.
- public static final variables
- public abstract methods
- public Default methods
- public static methods
- private methods
- private static methods
Here is an java9 private static interface method example
public interface MyInterface {
public String CONSTANT = "constant";
public void abstractMethod();
// default method example
public default void defaultMethod() {
privateMethod();
privateStaticMethod();
System.out.println("defaultMethod");
}
// static method example
public static void staticMethod() {
System.out.println("staticMethod");
}
// private method example
private void privateMethod() {
System.out.println("privateMethod");
}
// private static method example
private static void privateStaticMethod() {
System.out.println("privateStaticMethod");
}
}
public class TestInterface implements MyInterface{
public static void main(String[] args) {
TestInterface t=new TestInterface();
t.abstractMethod();
t.defaultMethod();
MyInterface.staticMethod();
}
@Override
public void abstractMethod() {
System.out.println("abstractMethod");
}
}
And the output of the above code execution is
abstractMethod
privateMethod
privateStaticMethod
defaultMethod
staticMethod
Modifier ‘private’ not allowed here error will be thrown in version 8 or less.
Private and static methods interface features
- These methods have a
private
keyword to denote them as a private method private
methods include body inside curly braces- The abstract keyword does not apply to these methods as No private and abstract keywords are applied to methods as both functionalities are reverse.
Private
method accessibility is within theinterface
where it is declaredprivate Static
methods can access other static and non-static methods i.e default methods.private
methods can not be used inprivate static
methods
Please be aware of the usage of these methods in interfaces. If properly not used, Compiler gives ’Exception in thread “main” java.lang.StackOverflowError’
Conclusion
In this tutorial, Learned private methods and static methods in java7,java8, and java9 versions with examples.