1.What is an interface in Java.

  • Before Java 8 interfaces are pure abstract classes which allow us to define public static final variables and public abstract methods(by default).
  • In java 8 introduced static methods and default methods in interfaces. 
  • We can develop interfaces by using "interface" keyword.  
  • A class will implements all the methods in an interface.



  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x=12;
  5. void show(); 

  6. }

  1. package com.instanceofjava;
  2. Class A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }

  7. }



  1. package com.instanceofjava;
  2. interface Java8Interface{
  3.  
  4. int x=12;
  5. void show(); 

  6.   
  7. default void display(){
  8.  
  9. System.out.println("default method of interface");
  10.  
  11. }
  12.  
  13. Static void print(String str){
  14.  
  15. System.out.println("Static method of interface:"+str);
  16.  
  17. }
  18.  
  19.  
  20. }




2.What will happen if we define a concrete method in an interface?
  • By default interface methods are abstract.
  • if we declare any concrete method in an interface compile time error will come.
  • Error:Abstract methods do not specify a body

Interface concrete method in java


3.Can we create non static variables in an interface?

  • No.We can not create non static variables in an interface.
  • If we try to create non static variables compile time error comes.
  • By default members will be treated as public static final variables so it expects some value to be initialized.

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error


  5. }


4.What will happen if we not initialize variables in an interface.
  • Compile time error will come because by default members will be treated as public static final variables so it expects some value to be initialized.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error: The blank final field y may not have been initialized


  5. }
5.Can we declare interface members as private or protected?
  • No.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. private int x; // compile time error: Illegal modifier for the interface field Sample.x; only
  5. public, static & final are permitted
  6. protected int a; // compile time error: Illegal modifier for the interface field Sample.a; only
  7. public, static & final are permitted

  8. }


7.When we need to use extends and implements?
  • A class will implements an interface.
  • A class will extends another class.
  • An interface extends another interface.

6.Can we create object for an interface?

  • NO. We can not create object for interface.
  • We can create a variable fro an interface

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  5. }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }
  7. public static void main(String args[]){
  8.  
  9.  JavaInterface obj= new JavaInterface(); // Error: Cannot instantiate the type JavaInterface
  10.  
  11. }
  12. }

7.Can we declare interface as final?

  • No. Compile time error will come.
  • Error: Illegal modifier for the interface Sample; only public & abstract are permitted
declare interface final in java



8.Can we declare constructor  inside an interface?

  • No. Interfaces does not allow constructors.
  • The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.

final interface interview freshers


9.What will happen if we are not implementing all the methods of an interface in class which implements an interface?

  • A class which implements an interface should implement all the methods (abstract) otherwise compiler will throw an error.
  • The type Example must implement the inherited abstract method JavaInterface.show() 
  • If we declare class as abstract no need to implement methods. 
  • No need of overriding default and static methods.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  • }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface { // The type Example must implement the inherited
  3. abstract method JavaInterface.show()
  4.  
  5. public static void main(String args[]){
  6.  

  7. }
  8. }

10.How can we access same variables defined in two interfaces implemented by a class?

  • By Using corresponding interface.variable_name we can access variables of corresponding interfaces.
11.If  Same method is defined in two interfaces can we override this method in class implementing these interfaces.
  • Yes implementing the method once is enough in class.
  • A class cannot implement two interfaces that have methods with same name but different return type.

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

8 comments for Top 10 interview questions on java interfaces

Select Menu