
Table of Contents
Java 8
Java 8 is the biggest change to the language since its inception, bigger than Java 5. It introduces many new language features including lambda expressions and method references.
New Features
Default and static Interface methods
Lambda Refrences
Method references
New Libraries
Streams
date/time
Concurrency updates
JVM Changes
PermGen Disappears
Default Static Methods
Pre-Java 8 interfaces only contain
– contacts
– method signatures
Post-Java Interfaces can contain default methods
– methods marked by keyword default
– with method body
public interface A {
void method1();
default void method2() {
System.out.println("Default Method");
}
}
public class Myclass implements A {
@Override
public void method1() {
}
}
A class can implement zero or more interfaces. It must provide the implementation for each method in those interfaces. This is fine except when the interface is updated but the class is not intended for update.
In this case, the class will not compile – as it does not provide an implementation for the new method.
This is where the use of default methods comes in
In the above interface, method 2 is marked as default hence during implementation user does not need to provide the implementation for the same.
Multiple Interfaces can have the same default methods
Java class can implement one or more interfaces. The same default method signature may be present in more than one interface. In such cases implementing class will not compile unless it overrides the duplicate method.
interface IntC {
void method1();
default void method2() {
System.out.println("Default Method- Interface C");
}
interface IntB {
void method1();
default void method2() {
System.out.println("Default Method- Interface B");
}
class ImplementationClass implements IntC, IntB {
@Override
public void method1() {
}
@Override
public void method2() {
System.out.println("Default Method- Class");
}
}
As shown in the above example Interface B and C both have method 1 as the default method. ImplemenatatonClass needs to provide override method 2 to compile.
Interface method can be called if Required
In some cases, it may be useful to call one or more of the default implementations of a default method – even when there are multiple versions in the inheritance hierarchy. This can be achieved by prefixing the call to super type with the interface providing the required implementation
public interface InterfaceB {
void method1();
default void method2() {
System.out.println("Default Method- Interface B");
}
}
public interface InterfaceC {
void method1();
default void method2() {
System.out.println("Default Method- Interface C");
}
}
public class Myclass implements InterfaceB,InterfaceC {
@Override
public void method1() {
}
@Override
public void method2() {
InterfaceB.super.method2();
}
}
In the above example, my class implements two interfaces both of which have method 2 as the default method. in this case, the implementation of any interface can be called by adding a prefix as super.
Static Interface Methods
In Java 8 it is possible to define methods in the interface. These methods with the keyword static as part of their signature (and are by default public ).
Static Interface methods are like static class methods(here they belong to the Interface only)
A static method belongs only to an Interface type, it is therefore only possible to invoke static methods by reference to the interface in which they are defined; and not via any classes that implement that Interface.
It is also not possible to override the method in the implementing class (as it does not see the static method).
public interface StaticInterface {
int method1();
static int methodstatic() {
return 10;
}
}
Interface with Static method
Leave a Reply