10 Replies Latest reply: Apr 2, 2009 3:55 AM by 807588 RSS

    Why isn't Java's Object class abstract?

    807588
      Why isn't java's Object class made as abstract class.
        • 1. Re: Why isn't Java's Object class abstract?
          807588
          So you can instantiate it.

          Which people often do, especially if they just want some Object but don't care about what it actually contains (for it's monitor, for instance).
          • 2. Re: Why isn't Java's Object class abstract?
            807588
            Is that only reason???
            • 3. Re: Why isn't Java's Object class abstract?
              EJP
              Is there a reason why it should be abstract?
              • 4. Re: Why isn't Java's Object class abstract?
                807588
                No.
                But what are different method in Object class, we generally do not override and also we rarely make the instance of the Object class. In that way, this can be created as abstract class, but for some reason it is made as concrete class.

                What are these deciding factors which make Object class as concrete???
                • 5. Re: Why isn't Java's Object class abstract?
                  796085
                  manoj.java wrote:
                  But what are different method in Object class, we generally do not override and also we rarely make the instance of the Object class. In that way, this can be created as abstract class, but for some reason it is made as concrete class.
                  Who rarely overrides them? I override them all the time. Especially toString(), equals() and hashCode().
                  What are these deciding factors which make Object class as concrete???
                  Like ejp said... why not? There's nothing about the Object class that means constructing one is a bad idea or is somehow incomplete. Plus, they're useful for synchronising on.
                  • 6. Re: Why isn't Java's Object class abstract?
                    807588
                    manoj.java wrote:
                    Why isn't java's Object class made as abstract class.
                    The only good reason I can think of is that, back in the days prior to Java 5 and the incorporation of java.util.concurrent, bare Objects are useful as thread synchronization monitors (wait(), notify(), notifyAll()).

                    --p                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                    • 7. Re: Why isn't Java's Object class abstract?
                      jschellSomeoneStoleMyAlias
                      manoj.java wrote:
                      No.
                      But what are different method in Object class, we generally do not override and also we rarely make the instance of the Object class. In that way, this can be created as abstract class, but for some reason it is made as concrete class.

                      What are these deciding factors which make Object class as concrete???
                      Have you used notify() and/or wait()?

                      In C#, created long after java, doesn't make it concrete either. Same reason, to provide something to control threads with.
                      • 8. This Thread is now moved
                        EJP
                        Note: This thread was originally posted in the [Reflections & Reference Objects|http://forums.sun.com/forum.jspa?forumID=4] forum, but moved to this forum for closer topic alignment.
                        • 9. Re: Why isn't Java's Object class abstract?
                          doremifasollatido
                          Example of use of Object from the JDK, which does not use it for its monitor:

                          java.util.HashSet is implemented by wrapping a java.util.HashMap. An Object is used as a dummy to represent the "value" for each element in the set. The "key" is the element you added to the HashSet. The dummy object is just an Object. It is declared as a private static final variable, so all instances of HashSet use the same "PRESENT" object. This saves memory, because there isn't a new object to go with each key--not even one that goes with each set. There is no reason for that dummy object to have any particular value, because the value isn't used for anything. It's just put into the HashMap so that the "key" can be included in the HashSet.
                          // Dummy value to associate with an Object in the backing Map
                          private static final Object PRESENT = new Object();
                          The concept of a HashSet could be implemented without use of a HashMap. But, doing it this way reduces the amount of duplicated code--the java.util.HashSet class is fairly short, because all of the implementation for "contains" and "add" and "remove" and other such things can be delegated to the underlying HashMap instance. HashMap's code contains full implementations of iterating through the values to check for "contains" or to make sure only unique objects are "add"ed or to "remove" the proper object.
                          • 10. Re: Why isn't Java's Object class abstract?
                            807588
                            Thanks doremifasollatido!!!!

                            I think this is something what i was looking for besides that synchronisation flow.