Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Interface

Java Interface

Interface (English: Interface), in the JAVA programming language, is an abstract type and a collection of abstract methods. An interface is usually declared with the keyword interface. A class inherits the abstract methods of an interface by implementing it.

An interface is not a class. Writing an interface is similar to writing a class, but they belong to different concepts. A class describes the attributes and methods of an object. An interface contains the methods that a class must implement.

Unless the class implementing the interface is an abstract class, the class must define all the methods in the interface.

An interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class. In addition, in Java, an interface type can be used to declare a variable; it can be a null pointer or be bound to an object that implements this interface.

Similarities Between Interfaces and Classes:

Differences Between Interfaces and Classes:

Interface Characteristics

Differences Between Abstract Classes and Interfaces

Note: After JDK 1.8, interfaces can have static methods and method bodies.

Note: After JDK 1.8, interfaces are allowed to contain methods with concrete implementations, called "default methods", which are modified with the default keyword. For more information, refer to Java 8 Default Methods.

Note: After JDK 1.9, methods can be defined as private, so that some reused code is not exposed. For more information, refer to Java 9 Private Interface Methods.


Declaration of an Interface

The syntax format for declaring an interface is as follows:

[

visibility

]

interface

interface name

[

extends

other interface names

]

{

//

Declare variables

//

Abstract method

}

The Interface keyword is used to declare an interface. Below is a simple example of an interface declaration.

NameOfInterface.java file code:

/*

File name: NameOfInterface.java

*/

import

java

.

lang

.*;

//

Import package

public

interface

NameOfInterface

{

//

fields of any type that are final, static

//

Abstract method

}

An interface has the following characteristics:

Example

Animal.java file code:

/*

File name: Animal.java

*/

interface

Animal

{

public

void

eat

(

)

;

public

void

travel

(

)

;

}


Implementation of an Interface

When a class implements an interface, the class must implement all the methods in the interface. Otherwise, the class must be declared as an abstract class.

A class implements an interface using the implements keyword. In the class declaration, the implements keyword is placed after the class declaration.

The syntax for implementing an interface can use this formula:

Interface syntax:

...

implements

interface name

[

, other interface name, other interface name..., ...

]

...

Example

MammalInt.java file code:

/*

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

(

)

;

}

}

The compilation and execution results of the above example are as follows:


Mammal eats
Mammal travels

When overriding a method declared in an interface, note the following rules:

When implementing an interface, also note some rules:


Inheritance of Interfaces

One interface can inherit from another interface, which is similar to inheritance between classes. Interface inheritance uses the extends keyword, and the subinterface inherits the methods of the parent interface.

The Sports interface below is inherited by the Hockey and Football interfaces:

//

File name: Sports.java

public

interface

Sports

{

public

void

setHomeTeam

(

String

name

)

;

public

void

setVisitingTeam

(

String

name

)

;

}

//

File name: Football.java

public

interface

Football

extends

Sports

{

public

void

homeTeamScored

(

int

points

)

;

public

void

visitingTeamScored

(

int

points

)

;

public

void

endOfQuarter

(

int

quarter

)

;

}

//

File name: 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 interface declares four methods of its own and inherits two methods from the Sports interface, so a class implementing the Hockey interface needs to implement six methods.

Similarly, a class implementing the Football interface needs to implement five methods, two of which come from the Sports interface.


Multiple Inheritance of Interfaces

In Java, multiple inheritance of classes is illegal, but interfaces allow multiple inheritance.

In multiple inheritance of interfaces, the extends keyword only needs to be used once, followed by the inherited interfaces, as shown below:

public

interface

Hockey

extends

Sports

,

Event

The above code fragment is a legally defined subinterface. Unlike classes, interfaces allow multiple inheritance, and Sports and Event can define or inherit the same methods


Marker Interface

The most common marker interface is an interface that contains no methods.

A marker interface is an interface without any methods or properties. It merely indicates that its class belongs to a specific type, allowing other code to test and permit certain operations.

The role of a marker interface: simply put, it is like tagging (stamping) an object so that the object gains certain privileges.

For example, the java.util.EventListener interface inherited by the MouseListener interface in the java.awt.event package is defined as follows:

package

java

.

util

;

public

interface

EventListener

{

}

An interface without any methods is called a marker interface. Marker interfaces are mainly used for the following two purposes: