1. Overview
In this article, we’ll have a quick look at some of the most interesting new features in Java 8.
2. Default and Static Methods
2.1 Default Method
Default methods are declared using the new default keyword. These are accessible through the instance of the implementing class and can be overridden.
Let’s add a default method to our Employee interface.
default String getAddress() { return "This is default address"; }
Assume that this interface is implemented by the class EmployeeImpl. For executing the default method an instance of this class should be created:
Employee emp = new EmployeeImpl(); String overview = emp.getAddress();
2.2 Static Method
Consider the following method of the interface (let’s call this interface Employee):
static String address() { return "XYZ"; }
The static order() method is available only through and inside of an interface. It can’t be overridden by an implementing class.
To call it outside the interface the standard approach for static method call should be used:
String address = Employee.address();