 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Can we have a private method or private static method in an interface in Java 9?\\n
Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only. We can't access or inherit private methods from one interface to another interface or class.
Syntax
interface <interface-name> {    private static void methodName() {       // some statements    }    private void methodName() {       // some statements    } }  Example
interface Java9Interface {    public abstract void method1();    public default void method2() {       method4();       method5();       System.out.println("Inside default method");    }    public static void method3() {       method5(); // static method inside other static method       System.out.println("Inside static method");    }    private void method4() { // private method       System.out.println("Inside private method");    }    private static void method5() { // private static method       System.out.println("Inside private static method");    } } public class PrivateStaticMethodTest implements Java9Interface {    @Override    public void method1() {        System.out.println("Inside abstract method");    }    public static void main(String args[]) {       Java9Interface instance = new PrivateStaticMethodTest();       instance.method1();       instance.method2();       Java9Interface.method3();    } } Output
Inside abstract method Inside private method Inside private static method Inside default method Inside private static method Inside static method
Advertisements
 