Latest web development tutorials

Java Interface

Interface (English: Interface), in the JAVA programming language is an abstract type, is a collection of abstract methods, interfaces typically interface to the statement. A class by inheriting interface mode, thereby to inherit abstract methods interface.

The interface is not a class, and the class approach to the preparation interface is very similar, but they belong to different concepts. Class describes the object properties and methods. Method class to implement the interface contains.

Unless the implementation of the interface class is abstract, or else to define the class interface all methods.

Interface can not be instantiated, but it can be implemented. A class that implements the interface, all the methods described in the interface must be implemented, otherwise it must be declared as an abstract class. In addition, in Java, the interface type is used to declare a variable, they can become a null pointer, or is bound to an object of this interface.

An interface similar to a class point:

  • An interface can have multiple methods.
  • Interface .java file is saved in the end of the file, the file name using the interface name.
  • Bytecode file interface is saved in a file ending in .class.
  • Interface corresponding bytecode file with the package name must match the directory structure.
Interface and class distinction:
  • Interface can not be used to instantiate objects.
  • The interface is not the constructor.
  • All interface methods must be abstract methods.
  • Interfaces can not contain member variables, in addition to static and final variables.
  • Interface is not inherited by the class, but the class is to be realized.
  • Interface supports multiple inheritance.

Interface Declarations

Interface declaration syntax is as follows:

[可见度] interface 接口名称 [extends 其他的类名] {
        // 声明变量
        // 抽象方法
}

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

/* 文件名 : NameOfInterface.java */
import java.lang.*;
//引入包

public interface NameOfInterface
{
   //任何类型 final, static 字段
   //抽象方法
}

Interface has the following features:

  • The interface is implicitly abstract, when declaring an interface when not necessary to use the abstract keyword.
  • Each interface method is implicitly abstract, abstract declaration does not require the same key sub.
  • Interface methods are public.

Examples

/* 文件名 : Animal.java */
interface Animal {

   public void eat();
   public void travel();
}

Implementation of the interface

When the class implements the interface class interface all methods to achieve. Otherwise, the class must be declared as an abstract class.

Use implements keyword class that implements the interface. In the class declaration, Implements keyword behind class declaration.

Implement an interface syntax, you can use this formula:

... implements 接口名称[, 其他接口, 其他接口..., ...] ...

Examples

/* 文件名 : 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 above examples compiled results are as follows:

Mammal eats
Mammal travels

When overriding methods declared in the interface, you need to pay attention to the following rules:

  • Class when implementing methods of the interface, you can not throw a mandatory exception, only in the interface, or inherit the abstract class interface thrown the mandatory exception.
  • Class when rewriting To maintain a consistent method name, and should stay the same or compatible return type.
  • If the implementation of the interface class is abstract, then there is no need to implement the method of the interface.

At the time of implementation of the interface, but also pay attention to some rules:

  • A class can implement multiple interfaces.
  • A class can only inherit one class, but can implement multiple interfaces.
  • An interface can inherit another interface, and classes that inherit comparison between similar.

Inheritance Interface

An interface can inherit another interface, more similar between classes and inheritance. Inheritance interface extends keyword sub-interface inherits the parent interface methods.

Hockey and Football interfaces The following interfaces are inherited Sports:

// 文件名: Sports.java
public interface Sports
{
   public void setHomeTeam(String name);
   public void setVisitingTeam(String name);
}

// 文件名: Football.java
public interface Football extends Sports
{
   public void homeTeamScored(int points);
   public void visitingTeamScored(int points);
   public void endOfQuarter(int quarter);
}

// 文件名: Hockey.java
public interface Hockey extends Sports
{
   public void homeGoalScored();
   public void visitingGoalScored();
   public void endOfPeriod(int period);
   public void overtimePeriod(int ot);
}

Hockey Interface himself declares four methods, interface inherits two methods from Sports, so to realize the need to implement the class interface Hockey six methods.

Similarly, the interface implementation class Football needs to implement five methods, including two from the Sports interfaces.


Multiple inheritance interface

In Java, multiple inheritance class is not legal, but the interface allows multiple inheritance.

In the interface of multiple inheritance extends keyword just once, in the subsequent follow-inheritance interfaces. As follows:

public interface Hockey extends Sports, Event

The above program fragment is legally defined sub-interfaces, unlike classes that interface allows multiple inheritance, and Sports and Event may define or inherit the same method


Marker interface

The most common inherited interface does not contain any methods of the interface.

It identifies the interface is no interface methods and properties. It merely indicates that it belongs to a particular type of class for other code to test allowed to do something.

Identifies the role of the interface: simple image of an object that is to make a mark (build a stamp), or make an object has a certain privilege.

For example: java.awt.event package MouseListener interface inheritance java.util.EventListener interfaces are defined as follows:

package java.util;
public interface EventListener
{}

There is no way the interface is called marker interface. A marker interface is mainly used for two purposes:

  • The establishment of a common parent interfaces:

    As EventListener interface, which is composed of dozens of other Interface Extension Java API, you can use a marker interface to establish a set of parent interface interface. For example: when an interface inherits EventListener interface, Java Virtual Machine (JVM) to know the interface to be used for a program of events agent.

  • Adding to a class data type:

    This situation is a tagging interface original purpose, the class that implements the marker interface does not need to define any interface methods (as marker interface is simply no way), but the class into an interface type through polymorphism.