Default Methods

Until 1.7 version onwards inside interface we can take public abstract method and public static final variable (every method present inside interface is always public and abstract whether we are declaring or not).

Every variable declared inside interface is always public static final whether we are declaring or not,but from 1.8 version onwards in addition to these,we can declare default concrete methods also inside interface,which are also known as defender methods. We can declare default methods with the keyword “default” as follows.

Interface default methods are by-default available to all implementation classes,based on the requirement implementation class can use these default methods directly or can override.

Default method also known as defender methods or virtual extension methods. The main advantage of default method is without affecting implementation classes we can add new functionality to the interface(backward compatibility).

Note: We can’t override object class methods as default methods inside interface otherwise we get compile time error.

 

Compile Time Error

Reason:

Object class methods are by-default available to every java class hence it’s not required to bring through default methods.

Default method VS Multiple inheritance:

Two interfaces can contain default method with same signature then there may be a chance a ambiguity problem to the implementation class .to overcome this problem compulsory we should override default method in the implementation class otherwise we get compile time error.

Example1:

Example2:

Example3:

How to override default method in the implementation class?

In the implementation class we can provide complete new implementation or we can call any interface method as follows.

interface name.super.method1()

Example:

Difference between interface with default methods and abstract class ?

Even though we can add concrete methods in the form of default methods to the interface, it won’t be equal to abstract class. Interface with default method are not equal to abstract class.

Static methods inside interface

From 1.8 version onwards in addition to default methods we can write static methods also inside interface to define utility functions. Interface static methods by-default not available to the implementation classes hence by using implementation class reference we can’t call interface static methods. We should call interface static methods by using interface name.

As interface static methods by default not available to the implementation class, overriding concept is not applicable. Based on our requirement we can define exactly same method ¡n the implementation class, it’s valid but not overriding.

Example:1

 

Its valid but not overriding

Example:2

 

This is valid but not overriding

Example:3

 

This is valid but not overriding

From 1 .8 version onwards we can write main method inside interface and hence we can run interface.

In Command Prompt:

javac Interf.java javaInterf

Java 8 Features Predicates