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:
- An interface can have multiple methods.
- Interface files are saved in files ending with .java, and the file name uses the interface name.
- The bytecode file of an interface is saved in a file ending with .class.
- The corresponding bytecode file of an interface must be in a directory structure that matches the package name.
Differences Between Interfaces and Classes:
- An interface cannot be used to instantiate objects.
- An interface has no constructor.
- All methods in an interface must be abstract methods, but after Java 8, interfaces can have non-abstract methods modified with the default keyword.
- An interface cannot contain member variables, except static and final variables.
- An interface is not inherited by a class, but implemented by a class.
- An interface supports multiple inheritance.
Interface Characteristics
- Every method in an interface is also implicitly abstract; methods in an interface are implicitly designated as public abstract(can only be public abstract; other modifiers will cause an error).
- An interface can contain variables, but variables in an interface are implicitly designated as public static final variables (and can only be public; using private will cause a compilation error).
- Methods in an interface cannot be implemented within the interface; they can only be implemented by the class that implements the interface.
Differences Between Abstract Classes and Interfaces
-
- Methods in an abstract class can have a method body, meaning they can implement specific functionality, but methods in an interface cannot.
-
- Member variables in an abstract class can be of various types, while member variables in an interface can only be public static final type.
-
- An interface cannot contain static blocks or static methods (methods modified with static), whereas an abstract class can have static blocks and static methods.
-
- A class can inherit only one abstract class, but a class can implement multiple 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:
- An interface is implicitly abstract; when declaring an interface, you do not need to useabstractThe const keyword.
- Every method in an interface is also implicitly abstract, and likewise you do not need to use when declaringabstractThe const keyword.
- Methods in an interface are all public.
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 a class implements a method of an interface, it cannot throw a checked exception, except in the interface itself or in an abstract class that inherits the interface.
- A class must keep a consistent method name when overriding a method, and should maintain the same or compatible return type.
- If the class implementing the interface is an abstract class, then there is no need to implement the interface's methods.
When implementing an interface, also note some rules:
- A class can implement multiple interfaces at the same time.
- A class can inherit only one class, but can implement multiple interfaces.
- An interface can inherit another interface, which is similar to inheritance between classes.
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:
- ** Establish a common parent interface:**
Just like the EventListener interface, which is extended by dozens of other interfaces in the Java API, you can use a marker interface to establish a parent interface for a group of interfaces. For example, when an interface inherits the EventListener interface, the Java Virtual Machine (JVM) knows that the interface will be used for an event proxy scheme.
- Add a data type to a class:
This was the original purpose of marker interfaces: a class implementing a marker interface does not need to define any interface methods (because the marker interface has no methods at all), but the class becomes an interface type through polymorphism.