阅读Java的官方Doc,总结如下。

What is Interface

An interface is a reference type, similar to a class, that can contain only

  1. constants (implicitly public, static, final)
  2. method signatures (no method body, no braces)
  3. default methods (has method body)
  4. static methods (has method body)
  5. nested types

Keypoints

Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

A class can implement multi interfaces.

An interface can extend multi interface.

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

Default Methods

A pretty clear application scenario here.

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.

When you extend an interface that contains a default method, you can do the following:

  1. Not mention the default method at all, which lets your extended interface inherit the default method.
  2. Redeclare the default method, which makes it abstract.
  3. Redefine the default method, which overrides it.

Special Case

Look at following codes. ContentVisitor is an interface.

private final ContentVisitor visitor = new ContentVisitor() {
public void onStartDocument() {
throw new IllegalStateException();
}

Did we create an instance of interface?

No. We can't instantiate an interface. Actually, what these codes do is to define an anonymous class that implements the interface, and instantiate that class.

It's the same with following codes.

private final class AnonymousContentVisitor implements ContentVisitor {
public void onStartDocument() {
throw new IllegalStateException();
}
} private final ContentVisitor visitor = new AnonymousContentVisitor();

Interfaces的更多相关文章

  1. 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)

    坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...

  2. 【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X

    I got the following error message when trying to open a network interface for capture using Wireshar ...

  3. CentOS / Redhat : Configure CentOS as a Software Router with two interfaces

    CentOS / Redhat : Configure CentOS as a Software Router with two interfaces   Linux can be easily co ...

  4. 错误:java.util.Map is an interface, and JAXB can't handle interfaces.

    问题: 在整合spring+cxf时报错java.util.Map is an interface, and JAXB can't handle interfaces. 解决方法: 将服务端的serv ...

  5. Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ”

    Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ” 解决方法: 今天在电脑上安装了WIR ...

  6. iis 使用 LocalDB 报错:provider: SQL Network Interfaces, error: 50

    在使用asp.net core读取localdb数据库时,报以下错误: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 S ...

  7. System.Data.SqlClient.SqlException: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)

    A network-related or instance-specific error occurred while establishing a connection to SQL Server. ...

  8. Go Data Structures: Interfaces

    refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...

  9. TypeScript - Interfaces

    简介 关注于数据值的 ‘shape’的类型检查是TypeScript核心设计原则.这种模式有时被称为‘鸭子类型’或者‘结构子类型化’. . 在TypeScript中接口interfaces的责任就是命 ...

  10. ubuntu /etc/network/interfaces 中配置虚拟链路

    ubuntu /etc/network/interfaces 中配置虚拟链路 平常做一些关于网络的测试时,像一些需要在二层上运行的功能,一个网卡不足够的情况下,可使用 ip link 工具加一些虚拟的 ...

随机推荐

  1. UIWebView加载html 图片大小自适应的方法汇总

    方法一 处理HTMLString的方法: NSString *htmls = [NSString stringWithFormat:@"<html> \n" " ...

  2. Sequence(组合数学,集合不同元素的个数)

    Sequence [组合数学] 时间限制: 3 Sec  内存限制: 128 MB 提交: 138  解决: 52 [提交][状态][讨论版] 题目描述 在某个夜黑月高的晚上,!!!,原谅我编不下去了 ...

  3. 实现Android 动态载入APK(Fragment or Activity实现)

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38565345 近期由于项目太大了.导致编译通只是(Android对一个应用中的方法个数貌似有 ...

  4. ADT下载地址整理

    參考以下文章 http://developer.android.com/tools/sdk/eclipse-adt.html 整理了官网的下载地址 http://dl.google.com/andro ...

  5. mevan引入容联云通讯jar

    首先从官网下载jar 然后拷贝到lib目录下 最后在pom.xml中这样写 <dependency> <groupId>cn.com</groupId> <a ...

  6. 自定义PopupWindow动画效果

    public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...

  7. C#自定义List类

    代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  8. snappydb 依赖的jar包

    最近看学习了一下snappydb,因为我用的还是Eclipse但是github上的是as项目所以就考虑用jar包来使用. github地址:https://github.com/nhachicha/S ...

  9. Android-版本与api对应关系图

    Code name Version API level Lollipop 5.1 API level 22 Lollipop 5.0 API level 21 KitKat 4.4 - 4.4.4 A ...

  10. 东软实训4-JDBC连接数据库

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...