Java 9 Even More Good – Part 1

We have very good feature in java 8 that we can have default method

here is the example but the problem is that after using default any parent class can access this method and we all knows that we can not use private while using method

public interface InJava8 {

default boolean evenSum(int… numbers) {
return sum(numbers) % 2 == 0;
}

default boolean oddSum(int… numbers) {
return sum(numbers) % 2 == 1;
}

// we don’t want this to be public;
// but how else do we resuse?
default int sum(int[] numbers) {
return IntStream.of(numbers).sum();
}

}

 

 

but java 9 has resolved this problem by providing the feature of
private method and has removed the burden of using default keyword
here is the example

public interface InJava9 {

// as above

private int sum(int[] numbers) {
return IntStream.of(numbers).sum();
}

}

 

Stay tuned for more cool update like and share if you found it good

Published by nareshjavatips

Java Edicted

Leave a comment