Home » Core Java » Interface Interview Questions

Interface Interview Questions

Top 10 Interface Interview Questions::
In this post I am going to explain the top interface related questions.My advice to the reader is to read the compile time error description for better understanding.

1.  What is Interface in Java ?
Ans :: Click here to see Answer
2.  Can we declare interface as protected or private ?

Ans :: No ,We can not declare interface as protected or private or final.Final limits to override its method,so we can not declare interface as final.If we try to do so,we will get compile time error.Only public and abstract are permitted while declaring an interface.If we try to do so we get compile time error.


3.  How to declare a variable in interface ?Can we use private,protected and static while declaring a variable in interface.
Ans :: We can not declare any variable with private,protected access modifier in an interface.If we do not use any keyword or access modifier ,by default variable of an interface is public static final.Since interface variable is by default final ,we need to initialize it at the time of declaration.

4.  Can we create object of interface and Can we declare constructor inside and interface ?
Ans :: Since interfaces are 100% abstract ,we can not create object of interface.Since object creation is prohibited in interface,we can not declare constructor inside interface.If we try to do any of these,we get compile time error.

5.  How to access same variable defined in two different interfaces implemented by a class?
Ans :: We can easily access two different variables defined in two interfaces implemented by a class just by using {interface_name}.{varibale_name}

Output ::

6. If we declare same method in two interfaces can we override this method in a class implementing these interfaces ?Explain in detail ?
Ans :: Yes, a class can override same method declared in two interfaces,but the limitation is method signature should be exactly same and class can override the method only once.

Output ::

If we change method’s signature in one of the interface ,we can compile time error.

7.  Can an interface extend another interface ?
Ans :: Yes an interface can extend another interface.Since interface is 100 % abstract it can not implement any other interface and can not extend another concrete or abstract class.
Rules ::
A concrete class can implement an interface.
A concrte class can extends another class.
An abstract class can implement an interface.
An abstract class can extends another class.
An interface can extend another interface.
8.  Explain Default method and static method inside interface in Java 8 ?
Ans :: Prior to Java 8 interfaces were 100% abstract classes and we define variables as public static final variables and methods as public abstract methods(by default).
In java 8 static and default concrete methods are introduced in interfaces.

Leave a comment

Your email address will not be published. Required fields are marked *

12 − seven =