
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What kind of variables/methods defined in an interface in Java 9?
Since Java 9, we are able to add private methods and private static methods in an interface. The advantage of using private methods in an interface is to reduce code duplication among default and static methods.
For instance, if two or more default methods need to share some code, a private method can be created for the same and called from each of the default methods.
Different Types of Variables and Methods in an Interface
In Java 9, the following variables/methods have been defined in an interface.
Constant Variables
All variables in interfaces are implicitly public, static, and final, and must be initialized at the time of the declaration. Usually named in uppercase with underscores.
Below is the syntax for declaring a constant variable in Java:
String DEFAULT_STRING = "TUTORIALSPOINT"; int MIN_VALUE = 100;
Example
Below is an example of constant variables in Java:
interface A { int VALUE=20; } public class B implements A { public static void main(String[] a) { System.out.println(VALUE); } }
Output
20
Abstract Method
A method that does not have a body is known as an abstract method. It contains only a method signature with a semicolon, and we do not need to explicitly use the abstract keyword in an interface, since all methods in an interface are implicitly abstract and public.
Below is the syntax for declaring an abstract method in Java:
public void dosomething();
Example
Below is an example of an abstract method in Java:
interface A { void dosomething(); } public class B implements A { public void dosomething() { System.out.println("Welcome to Tutorials Point"); } public static void main(String[] a) { new B().dosomething(); } }
Output
Welcome to Tutorials Point
Default Method
The default method lets us add new methods to an interface without changing the classes that we are using already. This means we don't have to update the implementing classes. Can be created with the default keyword.
Below is the syntax for declaring a default method in Java:
default void methodDefault()
Example
Below is an example of a default method in Java:
interface A { default void methodDefault() { System.out.println("Default"); } } public class B implements A { public static void main(String[] a) { new B().methodDefault(); } }
Output
Default
Static Method
A static method is a method that belongs to the class itself, rather than to any specific instance of the class. They cannot be overridden by implementing classes and are defined using the static keyword.
Below is the syntax for declaring a static method in Java:
static void staticMethod()
Example
Below is an example of a static method in Java:
interface A{ static void staticMethod() { System.out.println("Static"); } } public class Main { public static void main(String[] a) { A.staticMethod(); } }
Output
Static
Private and Private Static method
Private and static private interface methods were introduced in Java 9. A private method, such as a method, cannot be accessed via implementing a class or a sub-interface.
Example
Below is an example of a private and a private static method in Java:
import java.util.*; import java.util.stream.*; interface InterfaceTest { static void printEvenNumbers() { getDataStream().filter(i -> i%2==0).forEach(System.out::println); } static void printLOddNumbers() { getDataStream().filter(i -> i%2!=0).forEach(System.out::println); } private static Stream<Integer> getDataStream() { // private static method List<Integer> list = Arrays.asList(10, 13, 5, 15, 12, 20, 11, 25, 16); return list.stream(); } } public class InterfacePrivateMethodTest implements InterfaceTest { public static void main(String args[]) { System.out.println("The even numbers: "); InterfaceTest.printEvenNumbers(); System.out.println("The odd numbers: "); InterfaceTest.printLOddNumbers(); } }
Output
The even numbers: 10 12 20 16 The odd numbers: 13 5 15 11 25