20

If you define a interface like below

interface I1{

}

The in any code section you can write like

I1 i1;
i1.equals(null);

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?

Suppose let the interface extends the super class Object , then if you see why the collection interface like Set thave define the equals() and hashCode() method ?. All the class extends the Object class so if you define any abstract method in a interface which present in Object class then who implement the interface they no need to implements those method. Like in below code

interface I1{
String toString();
}

class A implements I1{

}

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Krushna
  • 5,059
  • 5
  • 32
  • 49

3 Answers3

39

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you very much So we can say that interfcae do not extend the object class, What is the answer of my second questions about Set interface why they have define those method. – Krushna Dec 08 '12 at 09:11
  • @KrushnaCh.Dash: See the end of my answer. The documentation for `Set.hashCode` and `Set.equals` provide more details about the expected behaviour. – Jon Skeet Dec 08 '12 at 09:16
3

Every class implicitly extends Object and so inherits every (non-private) method of the Object class.

Every instance has a class and therefore has all of the method of Object.

Whether an instance implements an interface or not is completely irrelevant to this point.

Object is a class, and interfaces can not extend classes, so "no" - the interface doesn't inherit anything from any class.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

A Java Interface does not extend the java.lang.Object class but instances of objects that implement the interface extend the Object class otherwise if Java Interfaces allowed to extend the java.lang.Object class then Java would support multiple inheritance of which it does not in its language specification.

Consider the scenario below:

interface MyInterface
{ 
   // interface methods here
}
MyClass extend java.lang.Object implements MyInterface
{ 
   // override methods or interface method implementations
}

Now if MyInterface extends java.lang.Object class then that would mean the MyClass would also be extending the MyInterface interface by construction creating multiple inheritance. So makes sense that Java interfaces do not extend the java.lang.Object class as this would create chaos due to multiple inheritance.

Jose Mhlanga
  • 805
  • 12
  • 14