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
)
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.
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.
No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete.
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.
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.[...]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With