Java Interface

  接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现的方法。
  除非实现接口的类是抽象类,否则该类要定义接口中的所有方法。
  接口无法被实例化,但是可以被实现。一个实现接口的类,必须实现接口内所描述的所有方法,否则就必须声明为抽象类。
  只有interfaces可以多继承。interfaces是抽象类。

接口与类的区别:

接口不能用于实例化对象。
接口没有构造方法。
接口中所有的方法必须是抽象方法。
接口不能包含成员变量,除了 static 和 final 变量。
接口不是被类继承了,而是要被类实现。
接口支持多继承。

接口特性

接口中的所有属性 默认的修饰符是 public static final。
接口中的所有方法 默认的修饰符是 public abstract。
接口中每一个方法也是隐式抽象的,接口中的方法会被隐式的指定为 public abstract(只能是 public abstract,其他修饰符都会报错)。
接口中可以含有变量,但是接口中的变量会被隐式的指定为 public static final 变量(并且只能是 public,用 private 修饰会报编译错误)。
接口中的方法是不能在接口中实现的,只能由实现接口的类来实现接口中的方法。

接口声明

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

接口的实现

1
2
3
...implements 接口名称[, 其他接口名称, 其他接口名称..., ...] ...
//For Example
public class MammalInt implements Animal{}

接口的多继承

在Java中,类的多继承是不合法,但接口允许多继承。
在接口的多继承中extends关键字只需要使用一次,在其后跟着继承接口。

1
public interface Hockey extends Sports, Event

Tips

1.抽象类和接口都不能实例化。(也就是不能初始化)
2.如果您不提供任何方法实现,请使用接口代替抽象类。(因为抽象类也可以不完成方法,所以用接口更好)
3.A class can implementmany interfaces, but extendsonly one superclass.
4.Both abstract classes and interfaces cancontain constants, which will be inherited by classes that extendor implementthem, respectively.

以下代码是说明interface是如何多继承的。

1
2
3
interface Father{ int age = 30; void wash(); } 
interface Mother{ long bank_account = 100000; void cook(); }
interface Child extends Father, Mother{ void cry(booleantear); }

Childinherits from Fatherand Motherand has the following:
intage = 30; (!!)
long bank_account= 100000;
void wash();
void cook();
void cry(booleantear);

Name Conflicts

•What happens if Fatherinterface and Motherinterface contain same named methodsand variables(constants)?

–Same named methods:

  •If they have different parameters, then Childinterface has both (this is same as overloading).
  •If they differ by only return type, then error.
  •If the two methods are identical, only keep one.

–Same named constants:

we keep both constants. To refer to them, use parent interface nameas prefix.
  •Example: –If both Fatherand Mothercontain an agevariable, then Child interface contains both. –To refer to them, we use: Father.ageor Mother.age.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public interface TestInterface1 { 
default void show() {
System.out.println("Default TestInterface1");
}
}
public interface TestInterface2 {
default void show() {
System.out.println("Default TestInterface2");
}
}
public class TestClass implements TestInterface1, TestInterface2 {
public void show() {
TestInterface1.super.show();
TestInterface2.super.show();
}

public static void main(String[] args) {
TestClass d = new TestClass();
d.show();
}
}

TestInterface1.super.show();
TestInterface2.super.show();
如果一个类同时实现接口A和B,接口A和B中有相同的default方法,这时,该类必须重写接口中的default方法。
为什么要重写呢?是因为,类在继承接口中的default方法时,不知道应该继承哪一个接口中的default方法。

在interface中,声明default可以直接完成函数。
如果一个类同时实现接口A和B,接口A和B中有相同的default方法,这时,该类必须重写接口中的default方法。

好好理解一下这里的super,没有是不行的。
如果实现类中要访问接口中的成员,不能使用super关键字。因为两者之间没有显示的继承关系,况且当接口中的成员成员属性是静态的。可以使用接口名直接访问。


interface里不允许使用修饰符protected
protected对同一包内的类和所有子类可见。

1
2
3
4
5
6
7
8
9
10
11
public interface MyConstants {
int r = 4;
int s = 6;

// INSERT CODE HERE
final double circumference = 2*Math.PI*r;
// int total = total + r + s; //Error, No declare total value.
int AREA = r*s;
// public static MAIN = 15; //Error, NO int or double, etc.
// protected int CODE = 31337; // Can not be protected!
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!