Home >>Java Tutorial >Interface in Java
An interface in java is basically known as a blueprint of a class and possesses static constants and abstract methods. In other words it can be said that the interface in Java is a mechanism that is generally used to achieve abstraction. Along with the abstract methods, an interface in Java may also contain static methods, constants, nested types, and default methods. Method bodies generally are in existence only for the default and static methods. In other words, it can be understood as the interfaces may have abstract methods and variables but this cannot possess a method body.
There is a condition in the Java language that is until and unless the class that is implementing the interface is basically the abstract and all the methods of the interface in Java must need to be defined in the class.
There are various reasons that can define the similarities and various dissimilarities that exist between an interface and a class in Java language. Let’s look at the similarities first. An interface can consists of any number of methods. And it is generally written in a file with a .java extension that too with the name of the interface matching the name of the file in Java Language. Generally it happens that the byte code of an interface will appear in a .class file. The interfaces appear in the packages and there is a condition that their corresponding bytecode file must be in a directory structure that should match the package name.
Now, let's talk about the dissimilarities between the interface and a class in Java language. A programmer cannot instantiate an interface. An interface in Java generally does not consist of any constructors. All of the methods that are present in an interface are abstract. An interface cannot contain instance fields and the only fields that can be there in an interface must be declared as both static and final. An interface in Java is not generally extended by a class instead it is implemented by a class. An interface in Java can basically extend multiple interfaces.
In order to declare the interfaces, the interface keyword is generally used. Here is a simple example depicted below that will make you understand the procedure of declaring the interface in the Java:
import java.lang.*; // Any number of import statements public interface NameOfInterface { // Any number of final, static fields // Any number of abstract method declarations\ }
Interfaces in Java generally have some properties that are depicted below:
There are generally three main reasons that are responsible for the use of the interface in the Java language. The reasons are depicted below:
Whenever a class implements an interface, then it can be seen as the class as signing a contract, and agreeing to execute some of the specific behaviors of the interface in Java. In case, a class does not execute all the behaviors of the interface then the class must declare itself as an abstract.
A class generally uses the implements keyword in order to implement an interface. The implements keyword basically appears in the class declaration that will be followed the extends portion of the declaration.
Here is an example that will explain you the working of the interfaces in the Java Language:
public class Mammal implements Animal { public void eat () { System.out.println ("Mammal eats"); } public void travel () { System.out.println ("Mammal travels"); } public int noOfLegs () { return 0; } public static void main (String args[]) { MammalInt m = new MammalInt (); m.eat (); m.travel (); } }