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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s