+ 7

What modifiers are allowed for methods in an interface ?

3rd Aug 2017, 11:17 AM
Varun Bhardwaj
Varun Bhardwaj - avatar
3 Réponses
+ 12
Only public and abstract modifiers are allowed for methods in interfaces.
3rd Aug 2017, 11:21 AM
Jihad Naji
Jihad Naji - avatar
+ 9
With the release of Java 8, the authors have introduced two new types of methods to an interface: default and static methods. A default method is a method defined within an interface with the 'default' keyword in which a body method is provided. Clases that implement an interface may then override this default implementation, but may also use the default by not implementing the method at all. A default method may only be declared with in an interface and not with in a class or abstract class. Like all methods in an interface, a default method is assumed to de public and will not compile if marked as private or protected. Java 8 also includes support for static methods within interfaces. To reference the static method, a reference to the name of the interface must be used. For example: NameOfInterface.getMethod() Because you need to provide the name of the interface in order to use a static method, implementing multiple interfaces which have static methods with the same signature will still compile. All abstract, default and static methods in an interface are implicitly public, so you can omit the public modifier.
12th Aug 2017, 5:43 AM
BasiaCode
BasiaCode - avatar
0
when sup class declares
29th May 2023, 6:08 PM
mora ze
mora ze - avatar