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?
Discussions
EJB programming & troubleshooting: java.lang.Object is not the root of all objects in Java
-
java.lang.Object is not the root of all objects in Java (5 messages)
- Posted by: Marco Ingco
- Posted on: October 10 2002 13:08 EDT
Threaded Messages (5)
- java.lang.Object is not the root of all objects in Java by David Jones on October 10 2002 13:37 EDT
- java.lang.Object is not the root of all objects in Java by David Jones on October 10 2002 13:40 EDT
- java.lang.Object is not the root of all objects in Java by Anki Nelaturu on October 10 2002 02:33 EDT
- java.lang.Object is not the root of all objects in Java by Marco Ingco on October 10 2002 15:12 EDT
- java.lang.Object is not the root of all objects in Java by Dave Wolf on October 10 2002 03:57 EDT
- java.lang.Object is not the root of all objects in Java by David Jones on October 10 2002 13:40 EDT
-
java.lang.Object is not the root of all objects in Java[ Go to top ]
- Posted by: David Jones
- Posted on: October 10 2002 13:37 EDT
- in response to Marco Ingco
Isin't the issue the fact that you are throwing an EqualityException from equals?
-
java.lang.Object is not the root of all objects in Java[ Go to top ]
- Posted by: David Jones
- Posted on: October 10 2002 13:40 EDT
- in response to David Jones
Although I am presuming EqualityException is not a Runtime exception -
java.lang.Object is not the root of all objects in Java[ Go to top ]
- Posted by: Anki Nelaturu
- Posted on: October 10 2002 14:33 EDT
- in response to David Jones
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??
-
java.lang.Object is not the root of all objects in Java[ Go to top ]
- Posted by: Marco Ingco
- Posted on: October 10 2002 15:12 EDT
- in response to David Jones
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. -
java.lang.Object is not the root of all objects in Java[ Go to top ]
- Posted by: Dave Wolf
- Posted on: October 10 2002 15:57 EDT
- in response to Marco Ingco
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