
- 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
Why do we need private methods in an interface in Java 9?
An interface supports default methods since Java 8 version. Sometimes these default methods may contain a code that can be common in multiple methods. In those situations, we can write another default method and make code reusability. When the common code is confidential then it's not advisable to keep them in default methods because all the classes that implement that interface can access all default methods.
An interface can have private methods since Java 9 version. These methods are visible only inside the class/interface, so it's recommended to use private methods for confidential code. That's the reason behind the addition of private methods in interfaces.
Syntax
private void methodName() { // some statementscode }
Example
interface Operation { default void addition() { System.out.println("default method addition"); } default void multiply() { division(); System.out.println("default method multiply"); } private void division() { // private method System.out.println("private method division"); } } class PrivateMethodTest implements Operation { public static void main(String args[]) { PrivateMethodTest test = new PrivateMethodTest(); test.multiply(); } }
Output
private method division default method multiply
- Related Articles
- Can we use private methods in an interface in Java 9?
- 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 have a private method or private static method in an interface in Java 9?\n
- Private Methods in Java 9 Interfaces
- Why do we need generics in Java?
- Can we overload methods of an interface in Java?
- Why do we need inner classes in Java?
- @SafeVarargs annotation for private methods in Java 9?
- Can we write an interface without any methods in java?
- Can we override private methods in Java\n
- Why do we need a wrapper class in Java?
- What kind of variables/methods defined in an interface in Java 9?
- What happens if we overload default methods of an interface in java?
- Why do we need weakMaps in Javascript?

Advertisements