
- 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
What are the rules for private methods in an interface in Java 9?
Java 9 added a new feature of private methods to an interface. The private methods can be defined using a private modifier. We can add both private and private static methods in an interface from Java 9 onwards.
Rules for private methods in an interface:
- A private method has a body in an interface means that we can’t be declared as a normal abstract method as usually do in an interface. If we are trying to declare a private method without a body then it can throw an error says that "This method requires a body instead of a semicolon".
- We can't be used both private and abstract modifiers together in an interface.
- If we want to access a private method from a static method in an interface then that method can be declared as a private static method as we can’t make a static reference to the non-static method.
- A private static method used from a non-static context means that it can be invoked from a default method in an interface.
Syntax
interface <interface-name> { private methodName(parameters) { // some statements } }
Example
interface TestInterface { default void methodOne() { System.out.println("This is a Default method One..."); printValues(); // calling a private method } default void methodTwo() { System.out.println("This is a Default method Two..."); printValues(); // calling private method... } private void printValues() { // private method in an interface System.out.println("methodOne() called"); System.out.println("methodTwo() called"); } } public class PrivateMethodInterfaceTest implements TestInterface { public static void main(String[] args) { TestInterface instance = new PrivateMethodInterfaceTest(); instance.methodOne(); instance.methodTwo(); } }
Output
This is a Default method One... methodOne() called methodTwo() called This is a Default method Two... methodOne() called methodTwo() called
- Related Articles
- What are the advantages of private methods in an interface in Java 9?
- Can we use private methods in an interface in Java 9?
- What are the rules for the Subscriber interface in Java 9?
- What are the rules for the Subscription interface in Java 9?
- What are the rules for the Publisher interface in Java 9?
- Why do we need private methods in an interface in Java 9?
- @SafeVarargs annotation for private methods in Java 9?
- What are the rules for a functional interface in Java?
- What are the modifiers allowed for methods in an Interface in java?
- Private Methods in Java 9 Interfaces
- What kind of variables/methods defined in an interface in Java 9?
- What are the rules for external declarations in JShell in Java 9?
- Can we have a private method or private static method in an interface in Java 9?\n
- What are the conditions for Collection factory methods in Java 9?
- What are the new methods added to an Optional class in Java 9?

Advertisements