Java 8 — Default and Static Method in Interface | Code Factory

Default Methods

  • Until 1.7 version onwards inside interface we can take only public abstract methods and public static final variables (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 method with the keyword “default” as follows
default void m1() {
System.out.println("Default Method");
}
  • Interface default methods are by-default available to all implementation classes. Based on requirement implementation class can use these default methods directly or can override.
package com.codeFactory.defaultAndStatic;interface Interf {
default void m1() {
System.out.println("Default Method");
}
}
public class Test implements Interf{
public static void main(String... args) {
Test t = new Test();
t.m1();
}
}
Default Method
package com.codeFactory.defaultAndStatic;interface Interf {
default void m1() {
System.out.println("Default Method");
}
}
public class Test implements Interf{
public void m1() {
System.out.println("My own implementation");
}
public static void main(String... args) {
Test t = new Test();
t.m1();
}
}
My own implementation
  • Default methods also known as defender methods or virtual extension methods.
  • The main advantage of default methods is without effecting implementation classes we can add new functionality to the interface (backward compatibility)
interface Interf {
default int hashCode() {
return 10;
}
}

Default method vs multiple inheritance :

package com.codeFactory.defaultAndStatic;interface Left {
default void m1() {
System.out.println("Left.m1()");
}
}
interface Right {
default void m1() {
System.out.println("Right.m1()");
}
}
public class Test implements Left, Right {
public void m1() {
System.out.println("Test.m1()");
Left.super.m1();
Right.super.m1();
}
public static void main(String... args) {
Test t = new Test();
t.m1();
}
}
Test.m1()
Left.m1()
Right.m1()

Difference between Interface with Default Methods and Abstarct 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.
package com.codeFactory.defaultAndStatic;interface Interf {
public static void sum(int a, int b) {
System.out.println(a + b);
}
}
public class Test implements Interf {
public static void main(String... args) {
sum(10, 20); // X
Test t = new Test();
t.sum(10, 20); // X
Test.sum(10, 20); // X
Interf.sum(10, 20); // ✓
}
}
  • 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 in the implementation class, it’s valid but not overriding.

Interface static methods wrt (with respect to) overriding :

interface Interf {
public static void m1() { }
}
public class Test implements Interf {
public static void m1() { }
}
interface Interf {
public static void m1() { }
}
public class Test implements Interf {
public void m1() { }
}
interface Interf {
public static void m1() { }
}
public class Test implements Interf {
private static void m1() { }
}
  • From 1.8 version onwards we can write main() method inside interface and hence we can run interface directly from the command prompt.
interface Interf {
public static void main(String... strings) {
System.out.println("Main Method");
}
}
Main Method

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Visual Python Debugger for Jupyter Notebooks You’ve Always Wanted

Mobile Instagram on desktop as an independent Application.

Weekly Development Update

Deploy NodeJS Project on Heroku

Introduction to Data Visualization with the Matplotlib library

Fill between example result

An Introduction to .NetCore & Microservices

6 ways to integrate AWS serverless services into your existing application

Building a simple pixel match test infrastructure for your needs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

My Review of Spring: Spring MVC

Java 17: Abstract Classes

Singleton Design Pattern

5 Tips for Designing RESTful API URI