Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can instance of interface access method of Object class?

Tags:

java

interface Intf {
}

class A implements Intf {
}

class Test {    
    public static void main(String[] args) {
        Intf obj = new A();
        obj.toString();
    }
}

A friend had shown me this code, I could not explain it to him...

We know that methods defined in 'referred' object can only be run on an instance. As we can see no method is defined by Intf but obj (which refers Intf) is able to call toString() method of Object.class

I consoled him saying that everything is an Object in Java (though we get no autofill option in eclipse IDE against Intf)

like image 917
anotherNovice1984 Avatar asked Jun 03 '11 11:06

anotherNovice1984


People also ask

Can we have object class methods in interface?

Core Java bootcamp program with Hands on practiceYes, you can. If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.

Which methods of an object can be accessed via an interface that it implements?

When each class implements these two interfaces and their methods, you can access the methods of these interfaces by casting the objects to instances of the interface types. You don't need to know exactly what class a given object is of, as long as you know what interface it implements.

Can an object be an instance of an interface?

No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete.

How do you access a method using an object?

You also use an object reference to invoke an object's method. You append the method's simple name to the object reference, with an intervening dot operator (.). Also, you provide, within enclosing parentheses, any arguments to the method. If the method does not require any arguments, use empty parentheses.


2 Answers

As we can see no method is defined by Intf

Actually, there is an implicitly declared toString method in Intf.

Every interface (that doesn't explicitly extend another interface) has an implicit method declaration for each public method in Object.

This is explained in detail in the Java Language Specification, § 9.2 Interface Members.

9.2 Interface Members

[...]

  • If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

[...]

like image 168
aioobe Avatar answered Oct 22 '22 11:10

aioobe


I'm not sure what your question is, because you're right.

An interface is merely a contract, saying that classes that implement it are required to specify certain methods. In this case, the Intf interface is a no-op since it places no additional requirements on implementors.

Thus your example is functionally identical to

class A {}

Would you be surprised that you can call obj.toString() for that class, when you know that all classes extend java.lang.Object and inherit its methods?

Even in cases where a more involved interface is involved, they simply place more requirements on what a class has to implement. However, all classes ultimately inherit from Object and thus have those methods defined on them.

like image 2
Andrzej Doyle Avatar answered Oct 22 '22 10:10

Andrzej Doyle