
- 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 advantages of private methods in an interface in Java 9?
In Java 9, an interface can also have private methods. Apart from static and default methods in Java 8, this is another significant change as it allows the re-usability of common code within the interface itself.
In an interface, there is a possibility of writing common code on more than one default method that leads to code duplication. The introduction of private methods avoids this code duplication.
Advantages of private methods in an interface
- Avoiding code duplication.
- Ensuring code re-usability.
- Improving code readability.
Syntax
interface interfacename { private methodName(parameters) { // statements } }
Example
interface Test { default void m1() { common(); } default void m2() { common(); } private void common() { System.out.println("Tutorialspoint"); } } public class PrivateMethodTest implements Test { public static void main(String args[]) { Test test = new PrivateMethodTest(); test.m1(); test.m2(); } }
Output
Tutorialspoint Tutorialspoint
- Related Articles
- What are the rules for private methods in an interface in Java 9?
- Can we use private methods in an interface in Java 9?
- Why do we need private methods in an interface in Java 9?
- Private Methods in Java 9 Interfaces
- What kind of variables/methods defined in an interface in Java 9?
- @SafeVarargs annotation for private methods in Java 9?
- Can we have a private method or private static method in an interface in Java 9?\n
- What are the modifiers allowed for methods in an Interface in java?
- What are the advantages of Modules in Java 9?
- Can we overload methods of an interface in Java?
- 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?
- What are the new methods added to an Optional class in Java 9?
- Can the abstract methods of an interface throw an exception in java?

Advertisements