interface TestInterface{
public void sayHello();
}
class A implements TestInterface{
public void sayHello(){
System.out.println("Hello");
}
public void sayBye(){
System.out.println("Hello");
}
public String toString(){
return "Hello";
}
public static void main(String args[]){
TestInterface ti=new A();
ti.sayHello();
ti.sayBye();//error
ti.toString();//How toString method can be called as it is not the part of the interface contract.
}
}
如果接口没有直接的超接口,则该接口会隐式声明一个
public abstract
成员方法m,该成员方法m具有签名s,返回类型r,并且throws
子句t对应于每个public
实例方法m,其签名s,返回类型r和throws
子句t在中声明Object
,除非abstract
接口显式声明具有相同签名,相同返回类型和兼容的throws子句的方法。
因此,Object
类似的公共方法toString
在所有接口中都隐式声明。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句