Static Interface Methods Flashcards
What is the difference between a static method in a class and a static method in an interface !
The difference between a static method in a class and a static method in an interface is that a static method in an interface is not inherited by any class that implements the interface
Give static interface method rules !
Here are the static interface method rules you need to be familiar with:
- Like all methods in an interface, a static method is assumed to be public and will not compile if marked as private or protected.
- To reference the static method, a reference to the name of the interface must be used.
- in Java 8 and later versions, if you declare a static method inside an interface, you are required to provide a method body for it. This applies to both static methods and default methods (which were introduced in Java 8).
What happens if a class implements two interfaces containing static methods with the same signature ?
A class that implements two interfaces containing static methods with the same signature will still compile at runtime, because the static methods are not inherited by the subclass and must be accessed with a reference to the interface name.