wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why Object class is not abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Object class is not abstract" Watch "Why Object class is not abstract" New topic

Why Object class is not abstract

Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Why Object class is not abstract? There are no private methods which will be required?
In other words, in which cases I would do following:
Object o=new Object();

Why would I instantiate Object class?

Chop your own wood, and it will warm you twice. - Henry Ford
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Two uses for a plain Object: -

  • As a synchronisation object. You just want some object on which to synchronise, but you never call any methods on it, so a plain Object will do.
  • As a "handle" used as a Map key. You never want to call any methods on the handle, just use it as a key, so a plain Object will do.

  • There may be more.

    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    Kartik Patel
    Ranch Hand

    Joined: Sep 12, 2005
    Posts: 73
    Thanks for reply.
    Here is what I understood from the solutions given by you:
    Object o=new Object();


    Does it make sense? I am creating an object and covering it with Sync block. Its as good as not having sync block because I am taking a lock on the different object and may be in sync block, I am changing value for some other object.

    2. If I store o (created in first Example) as key to Map and say, I have stored some value against that key. While getting value I need to pass very same object as Hashcode method of Object class will create new hashcode for each new object. I won't be able to get my value from Map until and unless I pass exactly same key(in this case o)

    So this won't work:
    Map m=new HashMap();
    Object o1=new Object();

    Than what is the use of having Object o as key, If I can not get value out of map
    Peter Chase
    Ranch Hand

    Joined: Oct 30, 2001
    Posts: 1970
    In both your cases, the objects you use are only known about in the method you show. As you say, in that case, they are legal but pointless examples.

    For synchronisation, a plain Object would typically be used as a final or static final member of a class. That way, the one Object can be seen by several pieces of code that need to synchronise together.

    For use of plain Object with a hash map, the Objects used as keys would be passed around the program as opaque handles. An example would be a factory method that creates some sort of useful object and puts it into a map for later retrieval. It might create a new Object as the key for the map, and return that object to the caller, so that the caller could later use it to request retrieval of the same map entry.
    I agree. Here's the link: http://aspose.com/file-tools
    subject: Why Object class is not abstract
    Similar Threads
    Abstract Inner Class
    JLS 6.4.3 The Members of an Interface Type
    why java's Object class not abstract ?