
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we use private methods in an interface in Java 9?
Yes, you can use private methods in an interface since Java9.
Example
interface MyInterface { public abstract void demo(); public default void defaultMethod() { privateMethod(); staticPrivateMethod(); System.out.println("This is a default method of the interface"); } public static void staticMethod() { staticPrivateMethod(); System.out.println("This is a static method of the interface"); } private void privateMethod(){ System.out.println("This is a private method of the interface"); } private static void staticPrivateMethod(){ System.out.println("This is a static private method of the interface"); } } public class InterfaceMethodsExample implements MyInterface { public void demo() { System.out.println("Implementation of the demo method"); } public static void main(String[] args){ InterfaceMethodsExample obj = new InterfaceMethodsExample(); obj.defaultMethod(); obj.demo(); MyInterface.staticMethod(); } }
Output
This is a private method of the interface This is a static private method of the interface This is a default method of the interface Implementation of the demo method This is a static private method of the interface This is a static method of the interface
- Related Articles
- Why do we need private methods in an interface in Java 9?
- Can we have a private method or private static method in an interface in Java 9?\n
- What are the rules for private methods in an interface in Java 9?
- What are the advantages of private methods in an interface in Java 9?
- Can we overload methods of an interface in Java?
- Can we override private methods in Java\n
- Can we write an interface without any methods in java?
- Private Methods in Java 9 Interfaces
- @SafeVarargs annotation for private methods in Java 9?
- Can I overload private methods in Java?
- How can we implement the Subscriber interface in Java 9?
- Can we declare an interface with in another interface in java?
- What kind of variables/methods defined in an interface in Java 9?
- Can we declare the variables of a Java interface private and protected?
- How can we create a Service Provider interface in Java 9?

Advertisements