What is Interface in Java?

Multiple inheritance cannot be achieved in java. To overcome this problem Interface concept is introduced.

An interface is a template which has only method declarations and not the method implementation.

Example: 

public abstract interface IManupulation{ 
    //Interface declaration 
    public abstract void add(); //method declaration 
    public abstract void subtract(); 
}

  • All the methods in the interface are internally public abstract void.
  • All the variables in the interface are internally public static final that is constants.
  • Classes can implement the interface and not extends.
  • The class which implements the interface should provide an implementation for all the methods declared in the interface.

public class Manupulation implements IManupulation{ 
    //Manupulation class uses the interface 
    public void add(){ 
    
    }
    public void subtract(){
    
    }
}

Top 50+ Java Interview Questions with Answers

We covered nearly 50 + primary Java interview questions in this tutorial for fresh and experienced candidates.  Q.1- What is… Read More

4 years ago