An interface is a position kind in Java. It is like a class. It is a compilation of conceptual methods. A class applies an line, thereby inherit the abstract processes of the line.
Along with conceptual techniques, an edge may also hold constants, default techniques, static processes, and nested types. The way bodies live only for default systems and inert methods.
Writing a line is comparable to writing a group. However, a class explains the characteristics and deeds of an item. In addition, an line encloses behaviors that a class realizes.
if not the class that realizes the line is theoretical, all the ways of the line need to be described in the class.
An line is like to a class in the subsequent ways −
However, an line is diverse from a class in numerous ways, including −
The line keyword is utilized to state an line. Here is a easy example to declare an boundary −
Following is an example of an interface:
/* File name : NameOfInterface.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 have the following properties −
Methods in an interface are implicitly public.
Example
/* File name : Animal.java */ interface Animal { public void eat(); public void travel(); }
while a class applies a line, you can believe of the group as symbolic agreement, agreeing to do the exact performances of the interface. If a group does not execute all the behaviors of the interface, the class must declare itself as abstract.
A class utilizes the realize keyword to implement a line. The implements keyword emerges in the class assertion subsequent extends part of the declaration.
Example
/* File name : MammalInt.java */ public class MammalInt 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(); } }
This will produce the following result:
Mammal eats Mammal travels
While overriding processes described in interfaces, there are numerous rules to be pursued −
When implementation lines, there are numerous rules −
A line can expand a new interface in an identical means that a class can enlarge another class. The extended keyword is utilized to extend a line, and the childline inherits the systems of the parent line.
The subsequent Sports line is extended by Hockey and Football interfaces.
// Filename: Sports.java public interface Sports { public void setHomeTeam(String name); public void setVisitingTeam(String name); } // Filename: Football.java public interface Football extends Sports { public void homeTeamScored(int points); public void visitingTeamScored(int points); public void endOfQuarter(int quarter); } // Filename: Hockey.java public interface Hockey extends Sports { public void homeGoalScored(); public void visitingGoalScored(); public void endOfPeriod(int period); public void overtimePeriod(int ot); }
The Hockey line has four techniques, but it accedes to two from Sports; thus, a class that applies Hockey requires to apply entire six methods. Also, a class that applies to Football requires to describe the three techniques from Football and the two methods from Sports.
A Java class can only lengthen one parent division. Multiple inheritances are not authorized. Lines are not classes, though, and a line can extend additional than one parent interface.
The extended keyword is employed once, and the parent borders are stated in a comma-separated list.
For instance, if the Hockey edge extended both Sports and incident, it would be stated as −
Example
public interface Hockey extends Sports, Event
The most ordinary employ of extending interfaces happens when the parent line does not have any methods. For case, the MouseListener interface in the java.awt.event package expanded java.util.EventListener, which is defined as −
Example
package java.util; public interface EventListener {}
An interface with no ways in it is submitted to as a tagging line. There are two essential design reasons of tagging interfaces −
Generates a general parent − As by the EventListener interface, which is enlarged by dozens of other lines in the Java API, you can utilize a tagging line to make a widespread parent among a cluster of interfaces. For example, when an line expands EventListener, the JVM identifies that this particular line is going to be utilized in an event designation scenario.
Adds a data kind to a class − this condition is where the word, tagging approaches from. A class that applies a tagging line does not require to classify any methods, but the class turn out to be an boundary type through polymorphism.
Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.