Java SE 8: Implementing Default Methods in Interfaces
Overview
Purpose
This tutorial covers how to use default methods in an interface
in the Java Platform, Standard Edition 8 (Java SE 8) environment.
Time to Complete
Approximately 45 minutes
Introduction
Prior to Java SE 8, interfaces in Java could contain only method
declarations and no implementations, and any nonabstract class
implementing the interface had to provide the implementation. This
limitation made it almost impossible to extend the existing
interfaces and APIs. To overcome this limitation, a new concept,
called default methods, is introduced in Java SE 8. The default
methods are fully implemented methods in an interface, and they
are declared by using the keyword default. Because
the default methods have some default implementation, they help
extend the interfaces without breaking the existing code.
In this tutorial, you learn how to use default methods in
interfaces:
Create a Java SE 8 project
Extend interfaces without the default method
Develop implementation classes
Extend the interface
Extend interfaces with the default method
Understand the inheritance rules of the default methods
Test the project
Hardware and Software Requirements
The following is a list of hardware and software requirements:
Download and install the latest JDK from this link (Java SE 8u11 recommended).
Download and install NetBeans 8 with Java SE from this link.
JUnit is an optional installation and is not required for this
tutorial.
Prerequisites
Before starting this tutorial, you should:
Have installed the required software.
Ensure that NetBeans is running.
Creating a Java Project
In this section, you create a Java SE 8 application in the
NetBeans IDE.
The DefaultMethods project is created in
NetBeans.
Extending Interfaces Without Default Methods
In this section, you develop an interface, MyService,
and add an abstract method, myDuties(). You develop
two implementation classes, Developer and Tester,
which implement the MyService interface. You develop
a Java class, RoleTester, to test the application.
Finally, you extend the MyService interface by
adding another method, extraDuties().
Developing the Interface
In this section, you develop a MyService
interface and add an abstract method, myDuties().
On the Projects tab, right-click DefaultMethods
and select New > Other.
The myDuties() method of the specific
classes are invoked for both Developer and
Tester classes.
Extending the MyService
Interface
In this section, you extend the MyService
interface by adding another abstract method, extraDuties().
The application breaks. That is, a compiler error is generated
when this method is added to the interface, because the new
method should be implemented by all implementation classes.
Therefore, the implementation classes, Developer
and Tester, have to implement this new extraDuties()
method.
Perform the following steps to extend the MyService
interface:
On the Projects tab, double-click MyService.java
to open it in the editor.
The compiler error was generated because the Developer
and Tester classes do not implement the extraDuties()
method defined in the MyService interface.
Before Java SE 8, interfaces in Java could contain only method
declarations and no implementations, and any nonabstract class
implementing the interface had to provide the implementation.
This limitation made it almost impossible to extend the
existing interfaces and APIs. To overcome this limitation, in
the next section, you will use default methods in the
interface.
Extending Interfaces with Default Methods
In this section, you add a default method to the MyService
interface. You modify the extraDuties method as a
default method by adding a keyword, default, to the
method. Next you modify the Developer class to
implement the default extraDuties method. You modify
the RoleTester class by invoking the default method
on instances of the Tester and Developer
classes and observe the output.
Modify MyService.java. Perform the following
steps:
Add the default keyword to the method
signature.
default public void extraDuties()
{
System.out.println("perform extra duties");
}
View Image
The Developer class is providing its own
implementation of extraDuties(), whereas, on the
other side, the Tester class did not provide any
implementation for the extraDuties()method. The Tester
implementation invoked the default method of the extraDuties()
provided by the interface.
Implementing Inheritance Rules of Default Methods
In this section, you implement one of the inheritance methods of
default methods. That is, if the method names conflict, the
default method should be treated as the default. The concrete
class must provide its own implementation of the method.
You develop another interface, MyNewService, with a
default method, extraDuties(). You modify the Developer
and Tester classes to implement the two interfaces,
MyService and MyNewService, and each
interface declares a default method with the same name, extraDuties().
The result is a compiler error to avoid the confusion of which
default method to be invoked. Finally, you modify the classes to
implement the default method.
Developing the MyNewService
Interface
On the Projects tab, right-click DefaultMethods
and select New > Other.
The compiler generates the following error,
"Duplicate default methods named extraDuties
with the parameters () and () are inherited from the
types MyNewService and MyService".
The solution for this error is to implement
the extraDuties() method in the
implementing class,Developer.
Implement the extraDuties()method as
follows:
public void extraDuties() {
System.out.println("I will perform extra duties for my
new service");
The compiler generates the following error,
"Duplicate default methods named extraDuties
with the parameters () and () are inherited from the
types MyNewService and MyService".The
solution for this error is to implement the extraDuties()
method in the implementing class,Tester.
Implement the extraDuties()method as
follows:
public void extraDuties() {
System.out.println("I will perform extra duties for
my new service");