java.lang.Object is not the root of all objects in Java

Discussions

EJB programming & troubleshooting: java.lang.Object is not the root of all objects in Java

  1. I'm sorry guys, I know this is a J2EE discussion, but I can't help it.
    This posting is not really a question, just to tickle our imagination. I think that java.lang.Object is really not the root of all objects in Java. I think there's a super-interface that both java.lang.Object implements and all other interfaces extend.

    This thought came to my mind because when I try to create an simple interface where I have an equals() method that throws an exception, a compilation error occurred that it cannot override the equals() method of the java.lang.Object. I don't even think that that error is accurate. I am not overriding Object. Or am I?
    Example:
     
    public interface EqualityTest {
        public boolean equals(Object obj) throws EqualityException;
    }

    My colleague told me that this error is because the class that will implement this interface will have that problem. So even before you create a class that will implement your interface, the compiler already catches that. Hmmm... but why such an inaccurate error message? And what if I only wanted to create that interface without thinking of having it implemented?

    What do you guys think?
  2. Isin't the issue the fact that you are throwing an EqualityException from equals?

  3. Although I am presuming EqualityException is not a Runtime exception
  4. Hey its surprising

    Just for testing I throwed IOException. This is the error I got.
    Test.java:2: equals(java.lang.Object) in Test cannot override equals(java.lang.Object) in java.lang.Object; overridden method does not throw java.io.IOException
        public boolean equals(Object obj) throws java.io.IOException;

    Can not override equals(..) in Object????
    means all interfaces also extends java.lang.Object????

    So finally, interfaces can also extend a class (may be at its first level)?? ;-)

    Am I confusing or got confused??

  5. Actually, the main issue here is that somehow, the interface extends something that the Object class might also be implementing... not the exception itself.

    I could have made this:

       public void clone();

    and this one will also give a compilation error that's somehow similar to the earlier one.

    The main issue is why the compiler would check the Object class whereas the interface has got nothing to do with Object.
  6. The issue is not that there is some hidden super interface that even Object extends it is simply that as an interface with no super interface it will implicitly include all members from equals, unless you define those methods with an identical signature and throws clause.

    To quote from the Java language specification section 6.4.3:

    <6.4.3>
    "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."
    </6.4.3>

    Dave Wolf
    Personified Technologies LLC