Java: Rationale of the Object class not being declared abstract

后端 未结 10 1073
粉色の甜心
粉色の甜心 2020-12-06 04:54

Why wasn\'t the java.lang.Object class declared to be abstract ?

Surely for an Object to be useful it needs added state or behaviour, an Object class is

相关标签:
10条回答
  • 2020-12-06 05:11

    I am not sure this is the reason, but it allows (or allowed, as there are now better ways of doing it) for an Object to be used as a lock:

    Object lock = new Object();
    
    ....
    
    
    synchronized(lock)
    {
    }
    
    0 讨论(0)
  • 2020-12-06 05:16

    Ande, I think you are approaching this -- pun NOT intended -- with an unnecessary degree of abstraction. I think this (IMHO) unnecessary level of abstraction is what is causing the "problem" here. You are perhaps approaching this from a mathematical theoretical approach, where many of us are approaching this from a "programmer trying to solve problems" approach. I believe this difference in approach is causing the disagreements.

    When programmers look at practicalities and how to actually implement something, there are a number of times when you need some totally arbitrary Object whose actual instance is totally irrelevant. It just cannot be null. The example I gave in a comment to another post is the implementation of *Set (* == Hash or Concurrent or type of choice), which is commonly done by using a backing *Map and using the Map keys as the Set. You often cannot use null as the Map value, so what is commonly done is to use a static Object instance as the value, which will be ignored and never used. However, some non-null placeholder is needed.

    Another common use is with the synchronized keyword where some Object is needed to synchronize on, and you want to ensure that your synchronizing item is totally private to avoid deadlock where different classes are unintentionally synchronizing on the same lock. A very common idiom is to allocate a private final Object to use in a class as the lock. To be fair, as of Java 5 and java.util.concurrent.locks.Lock and related additions, this idiom is measurably less applicable.

    Historically, it has been quite useful in Java to have Object be instantiable. You could make a good point that with small changes in design or with small API changes, this would no longer be necessary. You're probably correct in this.

    And yes, the API could have provided a Placeholder class that extends Object without adding anything at all, to be used as a placeholder for the purposes described above. But -- if you're extending Object but adding nothing, what is the value in the class other than allowing Object to be abstract? Mathematically, theoretically, perhaps one could find a value, but pragmatically, what value would it add to do this?

    There are times in programming where you need an object, some object, any concrete object that is not null, something that you can compare via == and/or .equals(), but you just don't need any other feature to this object. It exists only to serve as a unique identifier and otherwise does absolutely nothing. Object satisfies this role perfectly and (IMHO) very cleanly.

    I would guess that this is part of the reason why Object was not declared abstract: It is directly useful for it not to be.

    0 讨论(0)
  • 2020-12-06 05:17

    As always, Guava comes to help: with http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html Stuff here can be used to kill nulls / Object instances for "a not-null placeholder" from the code.

    There are entirely seperated questions here:

    • why did not they make Object abstract?
    • how much disaster comes after if they decide to make it abstract in a future release?
    0 讨论(0)
  • 2020-12-06 05:20

    There should be a reason to make a class abstract. One is to prevent clients from instantiating the class and force them into using only subclasses (for whatever reasons). Another is if you wish to use it as an interface by providing abstract methods, which subclasses must implement. Probably, the designers og Java saw no such reasons, so java.lang.Object remains concrete.

    0 讨论(0)
  • 2020-12-06 05:21

    An Object is useful even if it does not have any state or behaviour specific to it.

    One example would be its use as a generic guard that's used for synchronization:

    public class Example {
        private final Object o = new Object();
    
        public void doSomething() {
            synchronized (o) {
                // do possibly dangerous stuff
            }
        }
    }
    

    While this class is a bit simple in its implementation (it isn't evident here why it's useful to have an explicit object, you could just declare the method synchronized) there are several cases where this is really useful.

    0 讨论(0)
  • 2020-12-06 05:23

    You can instantiate Object for synchronization locks:

    Object lock = new Object();
    
    void someMethod() {
      //safe stuff
      synchronized(lock) {
         //some code avoiding race condition
      }
    }
    
    void someOtherMethod() {
      //safe code
      synchronized(lock) {
        //some other stuff avoiding race condition
      }
    }
    
    0 讨论(0)
提交回复
热议问题