Home > Java > javaTutorial > Can Interfaces Call Methods from the `Object` Class in Java?

Can Interfaces Call Methods from the `Object` Class in Java?

Mary-Kate Olsen
Release: 2024-10-27 16:02:02
Original
655 people have browsed it

Can Interfaces Call Methods from the `Object` Class in Java?

Do Interfaces Inherit from Object Class in Java?

In Java, interfaces are declared entities that define a contract for a class to implement. Unlike classes, interfaces do not directly inherit from the Object class.

However, despite not explicitly inheriting from Object, interfaces can still call methods defined in the Object class. This is because interfaces implicitly declare public abstract methods for each public method in the Object class.

For instance, consider the following code:

<code class="java">public class Test {
    public static void main(String[] args) {
        Employee e = null;
        e.equals(null);
    }
}

interface Employee {
}</code>
Copy after login

In this code, even though Employee is an interface and does not explicitly inherit from Object, the Java compiler recognizes that an interface implicitly declares the equals method, allowing it to be called on an Employee instance.

This behavior is specified in the Java Language Specification, Section 9.2, which states that an interface without any direct superinterfaces implicitly declares a public abstract member method for each public instance method declared in Object. This ensures that interfaces can interact with the Object class and its methods without explicitly inheriting from it.

The above is the detailed content of Can Interfaces Call Methods from the `Object` Class in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template