The interface (English: Interface) is an abstract type in the JAVA programming language and is a collection of abstract methods. Interfaces are usually declared as interfaces. A class inherits the abstract methods of the interface by inheriting the interface.
Interfaces are not classes. The way you write interfaces is very similar to that of classes, but they belong to different concepts. The class describes the properties and methods of the object. The interface contains methods to be implemented by the class.
Unless the class implementing the interface is an abstract class, this class defines all the methods in the interface.
The 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, interface types can be used to declare a variable, they can be a null pointer, or be bound to an object implemented in this interface.
Interfaces and classes are similar:
- An interface can have more than one method.
- The interface file is saved in a file ending in .java. The file name uses the interface name.
- The bytecode file for the interface is saved in a file at the end of .class.
- The corresponding bytecode file for the interface must be in the directory structure that matches the package name.
The difference between interface and class:
- Interfaces cannot be used to instantiate objects.
- There is no constructor for the interface.
- All methods in the interface must be abstract methods.
- Interfaces cannot contain member variables except static and final variables.
- The interface is not inherited by the class, but is to be implemented by the class.
- The interface supports multiple inheritance.
Interface features
- Each method in the interface is also implicitly abstract. The methods in the interface are implicitly specified as public abstract (only public abstract, other modifiers will report an error).
- The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, and compilation errors are reported with private modifications).
- The methods in the interface cannot be implemented in the interface. The methods in the interface can only be implemented by the class that implements the interface.
The difference between abstract classes and interfaces
- 1. The methods in an abstract class can have method bodies that can implement the specific functions of methods, but methods in interfaces cannot.
- 2. The member variables in the abstract class can be of various types, and the member variables in the interface can only be public static final types.
- 3. Interfaces can't contain static code blocks and static methods (statically decorated methods), while abstract classes can have static code blocks and static methods.
- A class can inherit only one abstract class, and a class can implement multiple interfaces.
Interface declaration
The syntax of the interface's declaration syntax is as follows:
[ Visibility ] interface interface name [ extends other class name ] {
// declare variable // Abstract method }
// declare variable // Abstract method }
The Interface keyword is used to declare an interface. The following is a simple example of an interface declaration.
NameOfInterface.java file code:
/* File name: NameOfInterface.java */
import java . lang .*; // Introduce packages public interface NameOfInterface {
// any type final, static field // Abstract method }
import java . lang .*; // Introduce packages public interface NameOfInterface {
// any type final, static field // Abstract method }
- The interface is implicitly abstract. When you declare an interface, you do not have to use the abstract keyword.
- Each method in the interface is also implicitly abstract and does not require the abstract keyword when it is declared .
- The methods in the interface are all public.
Examples
Animal.java file code:
/* filename: Animal.java */
interface Animal {
public void eat ( ) ; public void travel ( ) ; }
interface Animal {
public void eat ( ) ; public void travel ( ) ; }
Interface implementation
When a class implements an interface, the class implements all the methods in the interface. Otherwise, the class must be declared as an abstract class.
The class implements the interface using the implements keyword. In the class declaration, the Implements keyword is placed after the class declaration.
To implement the syntax of an interface, use this formula:
Animal.java file code:
... implements interface name [ , other interface name, other interface name ..., ... ] ...
Examples
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(); } }
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 above example compiled and run results are as follows:
Mammal eats Mammal travels
When you override the methods declared in an interface, you need to pay attention to the following rules:
- When a class implements an interface method, it cannot throw a mandatory exception. It can only throw this mandatory exception in the interface or in an abstract class that inherits the interface.
- Classes must maintain consistent method names when overriding methods, and should maintain the same or compatible return value types.
- 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, there are also some rules to be aware of:
- A class can implement multiple interfaces at the same time.
- A class can only inherit one class, but it can implement multiple interfaces.
- An interface can inherit another interface, which is similar to inheritance between classes.
Interface inheritance
An interface can inherit another interface, and the inheritance is similar between classes. The inheritance of the interface uses the extends keyword, and the subinterface inherits the parent interface.
The following Sports interface 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 visitTeamScored ( int points ) ; public void endOfQuarter ( int quarter ) ; }
// Filename: Hockey.java public interface Hockey the extends Sports {
public void homeGoalScored ( ) ; public void visitingGoalScored ( ) ; public void endOfPeriod ( int period ) ; public void overtimePeriod ( int OT ) ; }
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 visitTeamScored ( int points ) ; public void endOfQuarter ( int quarter ) ; }
// Filename: Hockey.java public interface Hockey the extends Sports {
public void homeGoalScored ( ) ; public void visitingGoalScored ( ) ; public void endOfPeriod ( int period ) ; public void overtimePeriod ( int OT ) ; }
The Hockey interface declares four methods by itself, inheriting two methods from the Sports interface. Thus, the class that implements the Hockey interface needs to implement six methods.
Similarly, the class that implements 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 the multiple inheritance of an interface, the extends keyword only needs to be used once, followed by an inherited interface. As follows:
public interface Hockey extends Sports , Event
The above program fragment is a legally defined subinterface. Unlike a class, an interface allows multiple inheritance, while Sports and Event may define or inherit the same method.
Tag interface
The most commonly used inheritance interface is an interface that does not contain any methods.
A tag interface is an interface that has no methods or properties. It merely indicates that its class belongs to a specific type that other code can test to allow it to do something.
Tag interface role: a simple image is to say that an object is marked (marked), so that the object has one or more 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 {
}
}
Interfaces without any method are called tag interfaces. The tag interface is mainly used for the following two purposes:
- Create a public parent interface:Just like the EventListener interface, which is a Java API that is extended by dozens of other interfaces, you can use a tag interface to build the parent interface of a set 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 situation is the initial purpose of the tag interface. Classes that implement the tag interface do not need to define any interface methods (because the tag interface has no way at all), but the class becomes an interface type through polymorphism.
Comments
Post a Comment