Since Interface cannot inherit Class, then how Object class methods available to the interface reference in java?
Every interface type is specified to implicitly declare all the public Object
methods. That is how the Java Language Specification deals with this formality.
9.2. Interface Members
...
- If an interface has no direct superinterfaces, then the interface implicitly declares a
public abstract
member methodm
with signatures
, return typer
, andthrows
clauset
corresponding to each public instance methodm
with signatures
, return typer
, andthrows
clauset
declared inObject
, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
Does an interface by default extend Object?
Then from where the equals method come, is the interface also extends the super class Object ?, if that true how an interface can extends a class?
The Java Language Specification deals with this explicitly.
From section 9.2:
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.
Basically, this is so that you can use equals
, hashCode
etc - because the way that the Java language is specified means that any concrete implementation of the interface will be a class, and that class must ultimately be a subclass of Object
, so the members will definitely be present.
To put it another way, while the interface itself doesn't extend Object
, it is known that any implementation will.
Here the class A no need to implements the method toString() as it's present in Object class. Then what is the objective of defining those method in collection interface as they can't force there implementation class to implement those method.
Usually this is just done for clarity, e.g. to document what is expected of an implementation in terms of the members declared in Object
.
Interfaces implicitly declaring public methods of Object class?
What is the purpose of this design?
Because you want to be able to call all the Object
methods ( toString
, equals
, hashCode
, and such) on every object of any type.
interface Foo {
}
Foo foo = ...;
foo.equals(otherFoo);
Doesn't matter if I actually declared the equals
method in the interface.
Object Class Inheritance with Interfaces
The logic is that every Java class is a descendant of Object
, irrespective of any interfaces it implements. Thus any reference can be upcast to Object
.
The mechanics that apply to your case are spelled out in §5.5.1. Reference Type Casting and §5.5.3. Checked Casts at Run Time of the JLS. The details are a bit complicated, since there's both a compile-time and a run-time components. The relevant quote is:
Here is the algorithm to check whether the run-time type
R
of an object is assignment compatible with the typeT
...If
R
is an interface:
- If
T
is a class type, thenT
must beObject
(§4.3.2), or a run-time exception is thrown.
Are Interfaces Object ?
The intuitive answer is that regardless of what interface you refer to, the object implementing the interface must be a subclass of Object
.
Section 9.2 of the JLS specifically defines this behaviour: http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.2
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.e. all interfaces are assumed to contain method signatures corresponding to methods in the Object
class.
Related Topics
Return a Value from Asynctask in Android
How to Pass a String from One Activity to Another
How to Find Java Heap Size and Memory Used (Linux)
How to Make Rjava Use the Newer Version of Java on Osx
How to Create a Directory in Java
Execute Another Jar in a Java Program
Comparing Two Integer Arrays in Java
How to Randomize Two Arraylists in the Same Fashion
Covariance, Invariance and Contravariance Explained in Plain English
Transitive Dependencies Not Resolved for Aar Library Using Gradle
Onclicklistener Not Responding
Ubuntu: Openjdk 8 - Unable to Locate Package
How to Get Rjava 0.9-3 to Work on Os X 10.7.4 with Oracle Java 1.7
Difference Between _Java_Options, Java_Tool_Options and Java_Opts
Java' Is Not Recognized as an Internal or External Command