Skip to content
Tags

Java 8 Default Methods

July 25, 2013

Prior to Java 8, all interface methods could only be abstract because they had to be defined in any class that implemented the interface. Now Java 8 provides us 2 new ways to have an implementation in the interface: default methods and static methodsBoth of these are defined in JSR 335.

Default Interface Methods

When a class implements an interface with a default method, that class it NOT required to override that method. If the implementing class decides to override the default method, it can still access that behavior with this idiom:

InterfaceName.super.m()

The InterfaceName of the interface with the default method, and m for the name of the default method. Default interface methods can also use the a this expression: its type is the type of the enclosing interface.

Static Interface Methods

Static Interface Methods do NOT get inherited by the implementing class. So these methods can only be used from the interface type on which the static method was defined.

Advertisements

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: