接口在Java内部扩展Object类吗?

huangapple go评论58阅读模式
英文:

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语言规范》的条目。

Java语言规范 - 4.3.1. 对象

“一个对象是一个类实例或一个数组......”

关于“类实例创建表达式”的术语,来自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() {

    }
};

huangapple
  • 本文由 发表于 2023年6月18日 18:52:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定