logo

Java Interfaces


Show

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 −

  • An line can have any number of processes.
  • An border is written in a sleeve with a .java extension, with the forename of the line corresponding the name of the file.
  • The byte system of a line emerges in a .class file.
  • Crossing points emerge in packages, and their equivalent bytecode file has to be in a index arrangement that matches the wrap up name.

However, an line is diverse from a class in numerous ways, including −

  • You never immediate an line.
  • An line never hold any constructors.
  • Entire of the ways in an line are abstract.
  • An interface never holds example fields. The only meadows that can emerge in an edge must be stated both inert and final.
  • An line is not expanded by a class; it is applied by a class.
  • An line can expand multiple crossing points.

Declaring Interfaces

The line keyword is utilized to state an line. Here is a easy example to declare an boundary −

Example

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 −

  • An edge is conceptual. You never require making use of the abstract keyword as declaring an interface.
  • Each way in an interface is too unreservedly conceptual, so the abstract keyword is not necessitated.

Methods in an interface are implicitly public.

Example

/* File name : Animal.java */
interface Animal {
   public void eat();
   public void travel();
}

Implementing Interfaces

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:

Output

Mammal eats
Mammal travels

While overriding processes described in interfaces, there are numerous rules to be pursued −

  • Checked exemptions should not be stated on implementation techniques other than the ones announced by the line method or subclasses of those stated by the interface method.
  • The cross of the line process and the equal revisit type or subtype should be continued when overriding the processs.
  • An implementation group itself can be theoretical and if so, boundary methods require not be implemented.

When implementation lines, there are numerous rules −

  • A class can apply more than one boundary at a time.
  • A class can expand only one class, except implement numerous interfaces.
  • An border can expand another line, in a comparable way as a class can expand another class.

Extending Interfaces

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.

Example

// 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.

Extending Multiple Interfaces

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

Tagging Interfaces

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.