Java 8: Interface interview question and answers

In this article, we will cover some of the interview questions with their justification on Java 8 interface

These are most frequently asked interview question from OOPS concepts

Java 8 Interface questions:

Q) What are default methods in Java 8?

–> With the release Java 8, the new cool feature is added i.e.; if any new method need to be added then provide default implementation for this new method inside interface itself

–> This new method will be prefixed with “default” keyword and known as default method

–> In Java 8, default methods are alternatively referred as Virtual Extension methods or defender methods

–> Example for default method

InterfaceInJava8.java

package in.bench.resources.itf.example;

// only public & abstract are permitted 
public interface InterfaceInJava8 {

	// old abstract method
	void displayAbstractMethod(); // by default, this is public and abstract

	// default method with concrete implementation from Java 8
	default void displayDefaultMethod() {
		System.out.println("InterfaceInJava8 : default method impl inside interface");
	}
}

 

Q) Is it ok to define default methods in Java 8 without “default” keyword inside interface?

–> No, for defining default method inside interface “default” keyword is must and it should prefix method declaration

–> Without prefixing default keyword results in compilation error

–> Compile-time error: Abstract methods do not specify a body

–> Reason: without default keyword, compiler consider it as abstract method and as said abstract methods doesn’t have body

9_Interface_interview_no_default_keyword

 

Q) Whether multiple inheritance possible i.e.; implementing 2 or more interfaces?

–> Java allows multiple inheritance through interfaces i.e.; a class can implement 2 or more interfaces

–> Post Java 8 release,

–> With the introduction of default methods, ambiguity problem might arise when both interfaces has same method with exactly same signature

–> Read more about Java 8 default and static methods in Interface in detail

 

Q) What happens in multiple inheritance from Java 8?

–> In Java 8, class can implement 2 or more interfaces and this might arise ambiguity problem with the introduction default method inside interface

–> Ambiguity problem arises because; both interfaces can have same method with exactly same signature

–> Read more about Java 8 default and static methods in Interface in detail

 

Q) What happens, if a class implements 2 interfaces having exactly same “default” method with same signature?

–> Results in ambiguity problem with compiler throwing error

–> There are 2 interfaces having same default methods and a class implements both this interfaces and results in ambiguity problem

DemoInterfaceA.java

10_Interface_interview_multiple_inheritance_ambiguity_problem_A

DemoInterfaceB.java

11_Interface_interview_multiple_inheritance_ambiguity_problem_B

TestMultipleInheritance.java

12_Interface_interview_multiple_inheritance_ambiguity_problem_Test_class

–> Compile-time error: Duplicate default methods named displayDefaultMethod with the parameters () and () are inherited from the types DemoInterfaceB and DemoInterfaceA

–> Read more about Java 8 default and static methods in Interface in detail

 

Q) How can we resolve ambiguity problem in Java 8 while implementing multiple Interfaces?

–> To resolve ambiguity problem in Java 8, override the conflicting method

–> Now, if we want to invoke default method from any of the interfaces then call using super keyword

–> For example, <interfaceName>.super.<defaultMethodName>

13_Interface_interview_multiple_inheritance_ambiguity_problem_solution

 

Q) How to invoke one of the interface’s default method while implementing two interfaces?

–> Using super keyword

–> Syntax: <interfaceName>.super.<defaultMethodName>

 

Q) How to come overcome, multiple inheritance problem in Java 8?

–> Override the default method in the implementation class

–> Altogether provide new implementation or

–> Invoke either one of the default method using super keyword

–> For example, <interfaceName>.super.<defaultMethodName>

–> Read more about Java 8 default and static methods in Interface in detail

 

Q) What happens, if a class implements 2 interfaces having exactly same method with same signature (consider one as default and another abstract)?

–> Compilation fails with error saying conflicting method

–> Compile-time error: The default method displayDefaultMethod() inherited from DemoInterfaceA conflicts with another method inherited from DemoInterfaceB

–> To overcome this error, override this conflicting method and provide new implementation or invoke default method’s implementation using super keyword

14_Interface_interview_multiple_inheritance_conflicting method

 

Q) Can we declare static method inside interface?

–> Yes, we can declare starting from Java 8

 

Q) Is it ok, to define static method inside interface?

–> Till Java 7 version,

–> Defining any concrete method (method with body) inside interface will throw compilation error, even if it’s static method

–> Post Java 8 release,

–> Static methods are allowed to define inside interface

–> This is new feature added in Java 8 which will act as a helper method

–> Read more about Java 8 default and static methods in Interface in detail

 

Q) How can we access static methods inside Interface?

–> Using interface name

–> Syntax: <interfaceName>.<variableName>

 

References

http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html#static
https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

 

Read Also:

Happy Coding !!
Happy Learning !!

Java: Initialization blocks
Interview Question and Answers on Java Interface