在上一个教程中,我们讨论了抽象类,它用于实现部分抽象。与抽象类不同,接口用于完全抽象。抽象是一个过程,您只显示“相关”数据并“隐藏”用户不必要的对象细节(参见:抽象)。在本指南中,我们将介绍 java 中的接口,我们使用它的原因以及在 Java 编程中使用接口时必须遵循的规则。
Java 中的接口是什么?
接口看起来像一个类,但它不是一个类。接口可以像类一样拥有方法和变量,但接口中声明的方法默认是抽象的(只有方法签名,没有正文,请参阅: Java 抽象方法)。此外,在接口中声明的变量是public static最后默认。我们将在本指南后面详细介绍。
Java 中的接口有什么用?
如上所述,它们用于完全抽象。由于接口中的方法没有正文,因此必须先由类实现它们才能访问它们。实现接口的类必须实现该接口的所有方法。此外,java 编程语言不允许扩展多个类,但是您可以在类中实现多个接口。
语法:
通过指定关键字interface声明接口。例如:
interface MyInterface{/* All the methods are public abstract by default* As you see they have no body*/public void method1();public void method2();}
Java 中的接口示例
这是类实现接口的方式。它必须提供在接口中声明的所有方法的主体,或者换句话说,您可以说该类必须实现接口的所有方法。
你知道吗? 类
implements接口但是接口extends的另一个接口。
interface MyInterface{/* compiler will treat them as:* public abstract void method1();* public abstract void method2();*/public void method1();public void method2();}class Demo implements MyInterface{/* This class must have to implement both the abstract methods* else you will get compilation error*/public void method1(){System.out.println("implementation of method1");}public void method2(){System.out.println("implementation of method2");}public static void main(String arg[]){MyInterface obj = new Demo();obj.method1();}}
输出:
implementation of method1
您可能还想阅读: 抽象类和接口之间的区别
接口和继承
如上所述,接口不能实现另一个接口。它必须扩展其他接口。请参阅下面的示例,其中我们有两个接口Inf1和Inf2。Inf2扩展了Inf1所以如果类实现了Inf2,它必须提供Inf2和Inf1接口的所有方法的实现。
在此处了解有关继承的更多信息: Java 继承
interface Inf1{public void method1();}interface Inf2 extends Inf1 {public void method2();}public class Demo implements Inf2{/* Even though this class is only implementing the* interface Inf2, it has to implement all the methods* of Inf1 as well because the interface Inf2 extends Inf1*/public void method1(){System.out.println("method1");}public void method2(){System.out.println("method2");}public static void main(String args[]){Inf2 obj = new Demo();obj.method2();}}
在这个程序中,类Demo只实现接口Inf2,但是它必须提供接口Inf1的所有方法的实现,因为接口Inf2扩展了Inf1。
Java 中的标记或标记接口
空接口称为标记或标记接口。例如,Serializable,EventListener,Remote(java.rmi.Remote)是标记接口。这些接口中没有任何字段和方法。在这里阅读更多相关信息。
嵌套接口
在另一个接口或类中声明的接口称为嵌套接口。它们也被称为内部接口。例如,集合框架中的Entry接口在Map接口内声明,这就是我们不直接使用它的原因,而是我们这样使用它:Map.Entry。
要点:以下是关于接口的关键要点:
1)我们无法在 java 中实例化接口。这意味着我们无法创建接口的对象
2)接口提供完全抽象,因为它的方法都没有。另一方面,抽象类提供了部分抽象,因为它可以具有抽象和具体(带有正文的方法)方法。
3)类使用implements关键字来实现接口。
4)虽然在接口的任何方法的类中提供实现,但它需要被公开提及。
5)实现任何接口的类必须实现该接口的所有方法,否则应该将该类声明为abstract。
6)接口不能声明为私有,受保护或瞬态。
7)所有接口方法默认为abstract和public。
8)接口中声明的变量默认为public,static和final。
interface Try{int a=10;public int a=10;public static final int a=10;final int a=10;static int a=0;}
以上所有陈述都是相同的。
9)接口变量必须在声明时初始化,否则编译器将抛出错误。
interface Try{int x;//Compile-time error}
上面的代码将抛出编译时错误,因为变量x的值在声明时未初始化。
10)在任何实现类中,您都不能更改在interface中声明的变量,因为默认情况下它们是public,static和final。这里我们实现了具有变量x的接口Try。当我们尝试设置变量x的值时,我们得到了编译错误,因为变量x默认是公共静态final,并且最终变量无法重新初始化。
class Sample implements Try{public static void main(String args[]){x=20; //compile time error}}
11)接口可以扩展任何接口但不能实现它。类实现接口和接口扩展接口。
12)类可以实现任何数量的接口。
13)如果两个接口中存在两个或多个相同的方法并且一个类实现两个接口,则该方法的实现一次就足够了。
interface A{public void aaa();}interface B{public void aaa();}class Central implements A,B{public void aaa(){//Any Code here}public static void main(String args[]){//Statements}}
14)类不能实现两个具有相同名称但返回类型不同的方法的接口。
interface A{public void aaa();}interface B{public int aaa();}class Central implements A,B{public void aaa() // error{}public int aaa() // error{}public static void main(String args[]){}}
15)变量名称冲突可以通过接口名称解决。
interface A
{
int x=10;
}
interface B
{
int x=100;
}
class Hello implements A,B
{
public static void Main(String args[])
{
/* reference to x is ambiguous both variables are x
* so we are using interface name to resolve the
* variable
*/
System.out.println(x);
System.out.println(A.x);
System.out.println(B.x);
}
}
java 中接口的优点:
使用接口的优点如下:
- 在不打扰实现部分的情况下,我们可以实现实现的安全性
- 在 java 中,不允许多重继承 ,但是您可以使用接口来使用它,因为您可以实现多个接口。
