Sitemap

Java 8: default method

3 min readNov 26, 2023

--

default method is one of the hot topics for java8 interviews, So thought to write a blog on this. In this blog I am trying to cover almost all questions possible on default method, will keep updating, if I will learn something new. Enjoy!!

1.) What is default method which was introduced in java8 ?

It is a method which can be declared inside the interface, introduced in java8. Before Java8 all methods which is declared inside the interface were abstract only, but in java8 for default implementation new keyword default was introduced. Till java8 all methods declared inside the interface are public and from java9 interface started supporting private method which can be called from default method.

//before java8 only public abstract allowed
interface Level1
{
default void method1() //invalid
{
System.out.println("method-1");
}
public abstract void method2(); //valid
}
//from java8 below code is valid
interface Level1
{

default void method1() //valid
{
System.out.println("method-1");
}

public abstract void method2(); //valid
}

2.) Why there was need of default method ?

Before java8 whenever we want to introduce a new method inside the interface, all the implementation class should must provide the body(implementation) of newly introduced method, even if they have no requirement of newly introduced method.

interface Level1
{
void method1();// it is by default public abstract

}

class Class1 implements Level1
{
public void method1()
{
System.out.println("method-1");
}
}

class Class2 implements Level1
{
public void method1()
{
System.out.println("method-x");
}
}

Now lets assume new method method2 got introduced in new version, which is required inside the Class1 only, but the problem here is: even if we want to compile , It is necessary for Class2 to provide implementation for the same even if it is required only in Class1, which is horrible.

interface Level1
{
void method1();// it is by default public abstract
void method2();
}

class Class1 implements Level1
{
public void method1()
{
System.out.println("method-1");
}
public void method2()
{
System.out.println("method-2");
}
}

class Class2 implements Level1
{
public void method1()
{
System.out.println("method-x");
}
public void method2(){} //unnecessary code
}

To overcome the above issue default method was introduced. Where if new method is introduced in that case only those class needs to provide implementation which needs method2, but other class can ignore that.

interface Level1
{
public abstract void method1();
default void method2(){}; //default method
}

class Class1 implements Level1
{
public void method1()
{
System.out.println("method-1");
}
@Override
public void method2()
{
System.out.println("method-2");
}
}

class Class2 implements Level1
{
public void method1()
{
System.out.println("method-x");
}
//No implementation of method2
}

3.) default keyword used inside the interface is same as default access modifier ?

Get Neeraj Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

No, both are different. default keyword used inside the interface tells: “code inside curly braces{} of default method is default implementation ”, But default access modifier makes scope of class, constructor, variable, method, or data member package-private. i.e it can be accessed within the package. If no access modifier is used compiler consider that as default.

4.) What are all the keywords that are allowed inside interface in Java8 ?

public, static, default, abstract.

5.) These are the only keyword allowed inside interface ?

Till Java8 public, static, default, abstract are the only keywords allowed but from java9 onwards we are allowed to use private keywords/methods.

6.) How to achieve multiple inheritance while using default method inside the interface ?

When we are using default method inside the interface, inorder to achieve multiple inheritance, It is mandatory to override the default method.

interface Level1
{
default void method1()
{
System.out.println("Method-1");
}
}
interface Level2
{
default void method1()
{
System.out.println("Method-2");
}
}
class Class1 implements Level1, Level2
{
public static void main(String[] args)
{
System.out.println("Hi from main method");
}
}

Above code will leads to compilation error. To resolve the issue we need to override the method1 inside the Class1 . So fixed code is going to look like:

class Class1 implements Level1, Level2
{

@Override
public void method1() {
Level1.super.method1();
}

public static void main(String[] args)
{
System.out.println("Hi from main method");
}
}

References: https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

Feel free to comment, if any questions/suggestions.

Thanks for Reading!!

======================End of Document==========================

--

--

Neeraj Kumar
Neeraj Kumar

Written by Neeraj Kumar

Lead Software Engineer, Loves Java, Also talks about Security. Instagram: geekneeraj