• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Why Object class is not abstract

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Kartik Patel
    Ranch Hand
    Posts: 73
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Peter,
    Thanks for reply.
    Here is what I understood from the solutions given by you:
    1.
    Object o=new Object();
    synchronized(o)
    {

    }

    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();
    m.put(o,"ONE");
    Object o1=new Object();
    m.get(o1);

    Than what is the use of having Object o as key, If I can not get value out of map
     
    Peter Chase
    Ranch Hand
    Posts: 1970
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic