sf9251
sf9251

Reputation: 288

Java Interface Clarification

I am confused with a line in Oracle Java docs here under the heading Interfaces In Java, which says:

Method bodies exist only for default methods and static methods.

As we cannot define method body in interface, I am confused if this line has some other meaning. Appreciate if somebody can help me in understanding this.

Upvotes: 4

Views: 93

Answers (2)

A4L
A4L

Reputation: 17595

Default methods and static methods are new features of java 8. Prior to java 8 it was not possible to define then in an interface.

Default methods have for example the benefit of changing extending interfaces (no in the inheritance sense) with other new methods for wich you have to may a default body so that existing implementations a not broken.

Static methods in interfaces have for example the benefit of sparing the need for the creation of other classes (utility/helper classes for example) of with the only pupose is to work on instances instances of that interface.

Upvotes: 1

Eran
Eran

Reputation: 393771

In Java 8, interfaces may contain default implementations for their methods, as well as implemented static methods.

Upvotes: 6

Related Questions