Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java 8 Default Methods

Java 8 Default Methods

Java 8 New Features Java 8 New Features


Java 8 added default methods to interfaces.

Simply put, a default method means an interface can have an implemented method, and implementing classes do not need to implement that method.

We only need to add the default keyword before the method name to implement a default method.

Why have this feature?

First, the previous interface was a double-edged sword: the benefit was programming against abstraction rather than concreteness; the drawback was that modifying an interface required modifying all classes that implement it. The collection framework before Java 8 had no foreach method, and the usual solution one could think of was to add new methods and implementations to the relevant interfaces in the JDK. However, for already-released versions, it is impossible to add new methods to an interface without affecting existing implementations. Hence default methods were introduced. Their purpose is to solve the problem of interface modification being incompatible with existing implementations.

Syntax

The syntax of default methods is as follows:

public

interface

Vehicle

{

default

void

print

(

)

{

System

.

out

.

println

(

"

I am a car!

"

)

;

}

}

Multiple Default Methods

Consider the case where a class implements multiple interfaces, and these interfaces have the same default method. The following example illustrates how to resolve this situation:

public

interface

Vehicle

{

default

void

print

(

)

{

System

.

out

.

println

(

"

I am a car!

"

)

;

}

}

public

interface

FourWheeler

{

default

void

print

(

)

{

System

.

out

.

println

(

"

I am a four-wheeled vehicle!

"

)

;

}

}

The first solution is to create your own default method to override the interface's default method:

public

class

Car

implements

Vehicle

,

FourWheeler

{

default

void

print

(

)

{

System

.

out

.

println

(

"

I am a four-wheeled car!

"

)

;

}

}

The second solution is to use super to call the specified interface's default method:

public

class

Car

implements

Vehicle

,

FourWheeler

{

public

void

print

(

)

{

Vehicle

.

super

.

print

(

)

;

}

}


Static Default Methods

Another feature of Java 8 is that interfaces can declare (and provide implementations for) static methods. For example:

public

interface

Vehicle

{

default

void

print

(

)

{

System

.

out

.

println

(

"

I am a car!

"

)

;

}

//

Static methods

static

void

blowHorn

(

)

{

System

.

out

.

println

(

"

Honk!!!

"

)

;

}

}


Default Method Example

We can learn about the use of default methods through the following code; you can put the code in the Java8Tester.java file:

Java8Tester.java file

public

class

Java8Tester

{

public

static

void

main

(

String

args

[

]

)

{

Vehicle

vehicle

=

new

Car

(

)

;

vehicle

.

print

(

)

;

}

}

interface

Vehicle

{

default

void

print

(

)

{

System

.

out

.

println

(

"

I am a car!

"

)

;

}

static

void

blowHorn

(

)

{

System

.

out

.

println

(

"

Honk!!!

"

)

;

}

}

interface

FourWheeler

{

default

void

print

(

)

{

System

.

out

.

println

(

"

I am a four-wheeled vehicle!

"

)

;

}

}

class

Car

implements

Vehicle

,

FourWheeler

{

public

void

print

(

)

{

Vehicle

.

super

.

print

(

)

;

FourWheeler

.

super

.

print

(

)

;

Vehicle

.

blowHorn

(

)

;

System

.

out

.

println

(

"

I am a car!

"

)

;

}

}

Executing the above script produces the following output:


$ javac Java8Tester.java 
$ java Java8Tester
我是一辆车!
我是一辆四轮车!
按喇叭!!!
我是一辆汽车!

Java 8 New Features Java 8 New Features