英文:
Does interface in java extends object class internally?
问题
如您所见,下面的代码可以重写对象类中的equals方法:
@FunctionalInterface
接口 abc
{
void meth1();
default void meth2(){
};
boolean equals(Object obj);
}
根据上述代码,Java中的接口是否在内部扩展对象类?
英文:
As you can see the below code where we can override equals method from an object class:
@FunctionalInterface
interface abc
{
void meth1();
default void meth2(){
};
boolean equals(Object obj);
}
So according to the above code does interface in Java extends object class internally?
答案1
得分: 2
根据上面的代码,Java中的接口是否在内部扩展了Object类?
不是的。接口根本不能扩展类,而java.lang.Object
是一个类,而不是一个接口。
如您所见,下面的代码中,我们可以从Object类中覆盖equals方法。
Java语言规范明确指出,如果您按照以下步骤操作,任何接口都被视为函数式接口:
- 删除其中与
java.lang.Object
中精确匹配的任何签名,无论是否默认。 - 删除具有默认实现的任何内容。
- 忽略内部类型和字段。
您是否仅剩下一个方法定义?
那么它就是一个函数式接口。@FunctionalInterface
不是必需的;这仅仅是可由编译器检查的文档,如果接口不符合上述要求,编译器会报错(它是:如果一切正常,则什么也不做,否则无法编译)。
忽略掉j.l.Object已经定义的所有方法是Java语言规范的一部分,不是某种内部的“扩展了Object”的情况。不,javac
中的代码明确进行了检查。
英文:
> So accroding to the above code does interface in java extends object class internally?
No. interfaces cannot extend classes at all, and java.lang.Object
is a class, not an interface.
> As you can see the below code where we can override equals method from an object class.
The Java Language Specification explicitly calls out that any interface is considered a functional interface if you follow this list of steps:
- remove any signatures in it, defaulted or not, that match precisely anything defined in
java.lang.Object
. - remove anything that has a
default
implementation. - Disregard inner types and fields.
Are you left with precisely one method definition?
Then it is a functional interface. @FunctionalInterface
isn't required; that is merely compiler-checkable documentation, which causes the compiler to error out if the interface does not fit the above requirements. (It's a: Do nothing if all is well, fail to compile otherwise).
The fact that you disregard all methods that j.l.Object already defines is part of the JLS - it isn't some internal 'oh it extends object'. No, the code in javac
explicitly checks.
答案2
得分: -1
根据上述代码,Java中的接口是否在内部扩展了Object类?
与所有人的回答相反,我相信我确实在某处看到过这个。虽然这并不意味着他们是错误的,这取决于你对“内部”是什么意思。
这是《Java语言规范》的条目。
“一个对象是一个类实例或一个数组......”
关于“类实例创建表达式”的术语,来自15.9,
“ClassInstanceCreationExpression:
UnqualifiedClassInstanceCreationExpression
ExpressionName . UnqualifiedClassInstanceCreationExpression
Primary . UnqualifiedClassInstanceCreationExpression
UnqualifiedClassInstanceCreationExpression:
new [TypeArguments] ClassOrInterfaceTypeToInstantiate ( [ArgumentList] ) [ClassBody]
ClassOrInterfaceTypeToInstantiate:
{Annotation} Identifier {. {Annotation} Identifier} [TypeArgumentsOrDiamond]”
根据ClassOrInterfaceTypeToInstantiate的定义,编译器将接口解释为对象。或者,更好的说法是,解释器将接口安排为对象。
本质上,这是指匿名内部类。
英文:
> "... So according to the above code does interface in Java extends object class internally?"
Contrary to everyone's responses, I believe I did read this somewhere.
Although, this doesn't mean they are wrong—it depends on what you mean by "internally".
Here is the Java Language Specification entry.
Java Language Specification – 4.3.1. Objects.
> "An object is a class instance or an array. ...
>
> ... A class instance is explicitly created by a class instance creation expression (§15.9)."
So, in terms of a "class instance creation expression", from 15.9,
> "ClassInstanceCreationExpression:
> UnqualifiedClassInstanceCreationExpression
> ExpressionName . UnqualifiedClassInstanceCreationExpression
> Primary . UnqualifiedClassInstanceCreationExpression
>
> UnqualifiedClassInstanceCreationExpression:
> new [TypeArguments] ClassOrInterfaceTypeToInstantiate ( [ArgumentList] ) [ClassBody]
>
> ClassOrInterfaceTypeToInstantiate:
> {Annotation} Identifier {. {Annotation} Identifier} [TypeArgumentsOrDiamond]"
As ClassOrInterfaceTypeToInstantiate defines it, the compiler will interpret the interface as an object.
Or, maybe a better way to say it is, the interpreter is going to schedule the interface as an object.
Essentially, this is referring to the anonymous inner-class.
abc abc = new abc() {
@Override
public void meth1() {
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论