0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Why does it matter? – Sweeper Jun 18 '23 at 10:55
  • No, it doesn't, am `interface` isn't a class. Of course you can declare an `equals` method in it - but it's pretty much a no op, since every class implementing the interface will have that method anyway. – daniu Jun 18 '23 at 10:56
  • "As you can see the below code where we can *override* equals method from an object class." that is now what is happening here. You are *re-declaring* that method, not *overriding* it. You are not providing code/body of that method which would *override* (replace) previous code/body from java.lang.Object#equals method. To provide body for method in interface such method must be marked as `default` (or static) but that is not allowed for this case as explained in [Java8: Why is it forbidden to define a default method for a method from java.lang.Object](https://stackoverflow.com/q/24016962) – Pshemo Jun 18 '23 at 11:08

2 Answers2

2

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
-1

"... 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() {

    }
};
Reilas
  • 3,297
  • 2
  • 4
  • 17