Java 9 Private Methods in Interface

This article will help you to understand “java 9 private methods in interface.”

Java 9 is going to release with Private Method in Interface. Let’s go through the history of interface till Java 9.

Interface | Java 7

In Java SE 7 or earlier versions, an interface can have two kinds of feature.

  • Constant Variables
  • Abstract Methods

These interface methods MUST be implemented by classes which choose to implement the interface.

Limitations: We can not provide implementation of any method in the interface. There is not any resolution with Java 7. We must go to abstract classes.

Inteface | Java 8

To overcome limitations for Java 7, Java 8 introduces Default methods and Static methods.

We can write method implementations in Interface from Java SE 8 and onwards. We need to use default keyword to define them.

In Java SE 8 and later versions, an interface have two more features.

  • Constant Variables
  • Abstract Methods
  • Default Methods
  • Static Methods

Limitations: If we want to provide a common method for all implementation method which we do not want to expose it outside. We must have to declare a common method as public. It increases redundancy.

Inteface | Java 9

Java 9 has the perfect solution for all above limitations.

We can write private methods in Interfaces using private access modifier.

In Java SE 9 and later versions, an interface have two more features.

  • Constant Variables
  • Abstract Methods
  • Default Methods
  • Static Methods
  • Private Methods
  • Private Static Methods

There is a difference between private and abstract modifiers.

  • private method is for fully implemented method. Sub Class cannot inherit and override private methods.
  • abstract method is for no-implementation method. Sub Class should inherit and override private methods.

Private methods are useful or accessible only within that interface only. We cannot access or inherit private methods from an interface to another interface or class.

Private methods can be static or instance. In both cases, the private method is not inherited by sub-interfaces or implementations.

That’s it all about Private Methods in Interface – Java 9. Please visit 8 New Features of Java 9 for more Java 9 features.

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *