📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
public interface CustomInterface {
public abstract void method1();
public default void method2() {
System.out.println("default method");
}
public static void method3() {
System.out.println("static method");
}
}
Learn Java 8 at https://www.javaguides.net/p/java-8.html
Video
Java 9 - Private Methods in Interface
- The private interface method cannot be abstract.
- A private method can be used only inside interface.
- A private static method can be used inside other static and non-static interface methods.
- A private non-static method cannot be used inside private static methods.
package net.javaguides.corejava.java9;
/**
* Java 9 - private methods and private static method in interfaces
* @author Ramesh fadatare
*
*/
public class PrivateInterfaceMethod implements CustomInterface {
@Override
public void abstractMethod() {
System.out.println("abstract Method implementation");
}
public static void main(String[] args) {
CustomInterface customInterface = new PrivateInterfaceMethod();
customInterface.defaultMethod();
customInterface.abstractMethod();
CustomInterface.staticMethod();
}
}
interface CustomInterface {
public abstract void abstractMethod();
public
default void defaultMethod() {
privateMethod(); //private method inside default method
privateStaticMethod(); //static method inside other non-static method
System.out.println("default method");
}
public static void staticMethod() {
privateStaticMethod(); //static method inside other static method
System.out.println("static method");
}
private void privateMethod() {
System.out.println("private method");
}
private static void privateStaticMethod() {
System.out.println("private static method");
}
}
Java 9 Private Interface Method Example
CustomCalculator.java – Interface
import java.util.function.IntPredicate;
import java.util.stream.IntStream;
public interface CustomCalculator
{
default int addEvenNumbers(int... nums) {
return add(n -> n % 2 == 0, nums);
}
default int addOddNumbers(int... nums) {
return add(n -> n % 2 != 0, nums);
}
private int add(IntPredicate predicate, int... nums) {
return IntStream.of(nums)
.filter(predicate)
.sum();
}
}
Main.java – Class
public class Main implements CustomCalculator {
public static void main(String[] args) {
CustomCalculator demo = new Main();
int sumOfEvens = demo.addEvenNumbers(1,2,3,4,5,6,7,8,9);
System.out.println(sumOfEvens);
int sumOfOdds = demo.addOddNumbers(1,2,3,4,5,6,7,8,9);
System.out.println(sumOfOdds);
}
}
20
25
Related Java 9 Posts
- Java 9 Private Methods in Interface with Examples - Learn how to use private methods in interface with examples.
- Java 9 List.of() Method - Create Immutable List Example - In this post, I show you how to create an immutable list using Java 9 provided List.of() static factory method.
- Java 9 Set.of() Method - Create Immutable Set Example - In this post, I show you how to create an immutable Set using Java 9 provided Set.of() static factory method.
- Java 9 Map.ofEntries() Method - Create Immutable Map Example - In this post, I show you how to create an immutable Map using Java 9 provided Map.ofEntries() static factory method.
- Java 9 - Stream API Improvements with Examples - In this article, we will learn what are the Stream API improvements made in Java 9 with an example.
- Java 9 - Optional Class Improvements with Examples - Learn Optional class improvements with examples.
Comments
Post a Comment
Leave Comment