Second, Java object-oriented (16) _ interface

2018-05-07

 

interface

 

1. What is an interface?

 

The interface only defines the specifications that should be followed, but does not care about the internal data of these classes and the implementation details of their functions.

From a program point of view, the interface only specifies the methods that the class must provide, thereby separating the specification and implementation, and enhancing the maintainability and extensibility of the system.

 

A Java interface is a declaration of a series of methods, which is a collection of some method characteristics. An interface has only method characteristics and no method implementation, so these methods can be implemented by different classes in different places, and these implementations can have different behaviors ( Function).

The interface and implementation class achieve true polymorphism.

 

The abstraction of multiple abstract classes is the interface.

The smallest program unit in Java is a class, and an interface is actually a special class.

An interface in Java represents a specification for defining a set of abstract methods.

 

Interface (English: Interface), in the JAVA programming language, is an abstract type and a collection of abstract methods. Interfaces are usually declared as interfaces. A class inherits the abstract methods of the interface by inheriting the interface.

An interface is not a class, the way of writing an interface is very similar to a class, but they belong to a different concept. A class describes the properties and methods of an object. An interface contains the methods to be implemented by the class.

Unless the class implementing the interface is an abstract class, the class defines 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, interface types can be used to declare a variable, they can be a null pointer , or be bound to an object implemented by this interface.

------------------------------------------------------------------------------

 

Second, the definition and use of the interface

 

The Interface keyword is used to declare an interface.

Interface declaration syntax:

  [public ] interface interface name [ extends other class names ] {

    // declare variables final, static fields

     // abstract method

   }

 

After the interface is compiled successfully, it has a bytecode file just like the class.

 

Members present in the interface:

  1) There is no constructor in the interface. So interfaces cannot create objects, i.e. cannot be instantiated.

  2) Member variables defined in the interface: they are essentially global static constants, which are decorated with public static final by default. Example: public static final string NAME = "Dragon 17";

  3) The methods defined in the interface are all public abstraction. Generally we don't use modifiers. Example: public abstract void wolk();

  4) The inner classes defined in the interface are all public static inner classes. By default, public static is used to decorate inner classes.

------------------------------------------------------------------------------------------------------------------------------------------

 

Third, the characteristics and inheritance of the interface

 

  1. An interface does not have a constructor, nor can it explicitly define a constructor, nor can it be instantiated.

  2. Interfaces can only inherit interfaces, not classes, and interfaces support multiple inheritance (classes are single-inherited).

     [modifier] interface interface name extends interface 1, interface 2

  3. The methods in the interface must be defined as abstract, and the default modifier is public abstract .

  4.接口的字段必须是全局静态常量,默认修饰符是public static final

  5.接口的内部类必须是静态的,默认修饰符是public static

 

接口和接口之间只能是继承关系。接口使用extends关键字实现继承。

接口和实现类只能是实现关系(特殊的继承关系)。类使用implements关键字实现接口。

-------------------------------------------------------------------------------------------------------------------

 

四、接口的实现

 

当类实现接口的时候,类要实现接口中所有的方法。否则,类必须声明为抽象的类。

此时我们得提供类,让类去实现接口同时重写接口中的方法,从而实现接口中所定义的方法。

类使用implements关键字实现接口。在类声明中,Implements关键字放在class声明后面。

实现一个接口的语法,可以使用这个公式(一个类可以实现多个接口继承,从而也弥补了类的单继承问题):

  [修饰符] class 类名称 [extends 父类名]  implements 接口1,接口2,...{ }

 

接口和实现类只能是实现关系(特殊的继承关系)。类使用implements关键字实现接口。

所以可以理解为:接口是实现类的父类,实现类是接口的子类

  接口 变量 = 创建实现类对象;//体现了多态思想

 

注意:因为接口的方法都是公共抽象的,所以实现类重写接口方法的时候,必须使用public修饰。

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------

 

五、接口和抽象类的区别

 

相同点:

  1)都可以被继承,用于被其他类实现或继承。

  2)都不能实例化

  3)都可以定义抽象方法,其实现类/子类都必须覆盖这些抽象方法

不同点:

  1)接口没有构造方法,抽象类有构造方法。

  2)抽象类可以包括普通方法和抽象方法,接口只能包含抽象方法(Java8之前)。

  3)一个类只能有一个直接父类(可能是抽象方法),接口是多继承的并且只支持一个类实现多接口。

  4)抽象类和接口中的成员变量、方法、内部类的默认访问权限都分别不一样。

------------------------------------------------------------------------

 

六、面向接口编程

 

多态的好处;

把实现类的对象赋给接口类型变量,屏蔽了不同实现类之间的实现差异,从而可以做到通用编程。

 

参考:http://www.runoob.com/java/java-interfaces.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525017&siteId=291194637