Prerequisite : Interfaces in Java
In Java SE 7 or earlier versions, an interface can have only:
- Constant variables
- Abstract methods
We can’t provide implementations of methods in interfaces.
public interface GFG{ String a = "Geeksforgeeks is the best." ; void hello(String a); void world( int x); } |
Java SE 8:
We can write method implementations in Interface from Java SE 8 and on-wards. We need to use “default” keyword to define them as shown below.
In Java SE 8 and later versions, an interface can have only four kinds of things:
- Constant variables
- Abstract methods
- Default methods
- Static methods
- Constant variables
- Abstract methods
- Default methods
- Static methods
- Private methods
- Private Static methods
public interface GFG{ String b = "Shubham is a brilliant coder." ; default void hello(String a){ System.out.println( "Hello" ); } static void world( int x){ System.out.println( "World" ); } void bye(); } |
Java SE 9:
In Java SE 9 and on-wards, we can write private methods in Interfaces using ‘private’ access modifier as shown below (like other private methods).
In Java SE 9 and later versions, an interface can have:
public interface GFG{ String b = "Shubham is a brilliant coder." ; default void hello(String a){ System.out.println( "Hello" ); } static void world( int x){ System.out.println( "World" ); } void bye(); private void great( long v){ } } |
This article is contributed by Shubham Juneja. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.