1

I was going through the Object class JAVA-docs and as we know Object is base class of all classes. But i was wondering, When Object class is added as super class? Is it compile time or runtime(believe ideally it should be at compile time)?

Also as i have read JVM automatically checks if a class is inheriting from parent class then it will not add Object as super class to avoid Inheritance-diamond problem but what will happen in case of abstract class/ inner classes? Where will be Super class-Object will be added ?

While creating any new built in objects example HashMap we can see the internal working from the source, Similarly is it possible to see this functionality anywhere in Java source code or it is done by compiler internally ?

Harish
  • 269
  • 1
  • 9
  • This is possibly a question for [softwareengineering.se]. – Dev-iL Feb 19 '17 at 10:01
  • 1
    It is not possible for class inheritance to introduce a "diamond problem" in Java because every class other than `Object` has exactly one direct superclass. `Object` has no superclass, being the root of all class hierarchies. As the answer below hints, for any class the compiler determines when `Object` needs to be the direct superclass. Have you read the Java Language Specification (JLS) or the Java Tutorial? These explain it, the former in some detail. It's good to be familiar with the documentation, and to use it. – Lew Bloch Feb 19 '17 at 10:24
  • @Dev-iL when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Feb 19 '17 at 12:15

1 Answers1

5

If you don't specify a super class, the default is java.lang.Object. And the super class is determined at compile time. In the case of an abstract (an inner or a static) class, without an explicit super type; the super type is java.lang.Object.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Is it possible to see this in any JVM source? or do i need to check the classLoaders? – Harish Feb 19 '17 at 07:08
  • @Harry `javap -v` on a `class` file and you'll see it in the byte code. Or you could call `getClass().getSuperclass().getName()` or read the JLS. – Elliott Frisch Feb 19 '17 at 07:17
  • 1
    Or you could read the documentation ... – Lew Bloch Feb 19 '17 at 10:25
  • 1
    @Harry the compiler set the parent of *any* class to `java.lang.Object`, unless it is the Object class or it extends another class. – Peter Lawrey Feb 19 '17 at 17:01
  • @Harish the fact that you can use java.lang.Object methods on a class without explicit super class tells you that it is as if you had written "extends Object" – Ingo Oct 29 '17 at 07:28
  • We could summarize this to the statement that *all* classes without an explicit super type have the super type `java.lang.Object`, except for `java.lang.Object` itself. It’s not clear what the last sentence, starting with “In the case of an `abstract` (an `inner` or a `static`) class, …” is supposed to say. It’s describing the same thing as the first sentence, just with with different words, but its leading phrase suggests there was same kind of difference for `abstract`, non-`abstract`, top level classes, inner classes, or nested classes, which doesn’t exist. – Holger Dec 20 '17 at 11:15