Why java.lang.Object is not abstract? [duplicate]

Without the designers of java.lang.Object telling us, we have to base our answers on opinion. There’s a few questions which can be asked which may help clear it up.

Would any of the methods of Object benefit from being abstract?

It could be argued that some of the methods would benefit from this. Take hashCode() and equals() for instance, there would probably have been a lot less frustration around the complexities of these two if they had both been made abstract. This would require developers to figure out how they should be implementing them, making it more obvious that they should be consistent (see Effective Java). However, I’m more of the opinion that hashCode(), equals() and clone() belong on separate, opt-in abstractions (i.e. interfaces). The other methods, wait(), notify(), finalize(), etc. are sufficiently complicated and/or are native, so it’s best they’re already implemented, and would not benefit from being abstracted.

So I’d guess the answer would be no, none of the methods of Object would benefit from being abstract.

Would it be a benefit to mark the Object class as abstract?

Assuming all the methods are implemented, the only effect of marking Object abstract is that it cannot be constructed (i.e. new Object() is a compile error). Would this have a benefit? I’m of the opinion that the term “object” is itself abstract (can you find anything around you which can be totally described as “an object”?), so it would fit with the object-oriented paradigm. It is however, on the purist side. It could be argued that forcing developers to pick a name for any concrete subclass, even empty ones, will result in code which better expresses their intent. I think, to be totally correct in terms of the paradigm, Object should be marked abstract, but when it comes down to it, there’s no real benefit, it’s a matter of design preference (pragmatism vs. purity).

Is the practice of using a plain Object for synchronisation a good enough reason for it to be concrete?

Many of the other answers talk about constructing a plain object to use in the synchronized() operation. While this may have been a common and accepted practice, I don’t believe it would be a good enough reason to prevent Object being abstract if the designers wanted it to be. Other answers have mentioned how we would have to declare a single, empty subclass of Object any time we wanted to synchronise on a certain object, but this doesn’t stand up – an empty subclass could have been provided in the SDK (java.lang.Lock or whatever), which could be constructed any time we wanted to synchronise. Doing this would have the added benefit of creating a stronger statement of intent.

Are there any other factors which could have been adversely affected by making Object abstract?

There are several areas, separate from a pure design standpoint, which may have influenced the choice. Unfortunately, I do not know enough about them to expand on them. However, it would not suprise me if any of these had an impact on the decision:

  • Performance
  • Security
  • Simplicity of implementation of the JVM

Could there be other reasons?

It’s been mentioned that it may be in relation to reflection. However, reflection was introduced after Object was designed. So whether it affects reflection or not is moot – it’s not the reason. The same for generics.

There’s also the unforgettable point that java.lang.Object was designed by humans: they may have made a mistake, they may not have considered the question. There is no language without flaws, and this may be one of them, but if it is, it’s hardly a big one. And I think I can safely say, without lack of ambition, that I’m very unlikely to be involved in designing a key part of such a widely used technology, especially one that’s lasted 15(?) years and still going strong, so this shouldn’t be considered a criticism.

Having said that, I would have made it abstract ;-p

Summary
Basically, as far as I see it, the answer to both questions “Why is java.lang.Object concrete?” or (if it were so) “Why is java.lang.Object abstract?” is… “Why not?”.

Leave a Comment