Header Ads

  • Breaking Now

    Why Java uses Singly rooted hierarchy?

    All objects in Java are inherited from same base class called 'Object'.In Java all objects have common interface to implement and it makes implementaion of Garbage collector lot easier in Java.The necessary implementaion is provided in base class , and the garbage collector can then send the necessary messages to every objectc in the system.Without singly rooted hierarchy,it would have been difficult to implement garbage collection feature.It enables lot of ease to programmers not to be bothered about memory management while development.It greatly simplifies argument passing amongst object too on the heap. As Java started from scratch and has no backward compatibility issues with any existing language, it was a logical choice to use the singly-rooted hierarchy in common with most other object-oriented programming languages.


    1. Are there any other reasons apart from Garbage collection for Java having singly rooted Hierarchy?

    2. Venu,
      Primarily making things simple as compared to C++,and Garbage collector implementation is the main one.It gives ease of development to programmers.As all Java objects are inherited from java.lang.Object class,it has following benefits:
      -All objects are assured of minimum set of attributes and behavior.
      -Necessary support for garbage collection is implemented in base class
      -Without a singly-rooted hierarchy and a system to manipulate an object via a handle, it is difficult to implement a garbage collector.

      I hope it clear things for you!!

    3. Mandeep Singh, DelhiMay 20, 2011 at 9:47 PM

      Hi Dharm,
      You explained it well but what is the implementation of garbage collection in Object class. I mean are there methods in the object class which provide garbage collection. I could not find any such methods.

      I know how garbage collection works in principle, but i want to know how it is related to the object class because of which we have a single rooted hierarchy in java.

    4. I am not clear with the answer, can you explain me with some examples

    5. when the program is unreachable garbage collector ll be invoked.

    6. The Object class has finalize() method which is called by the garbage collector.
      Any class can override this method and supply any code which deals with the resource release/clean up which is intended to be performed before the object is removed from memory by the GC.
      But, we generally can't rely on the finalize() method because there is no gurantee that the GC runs.


    Post Top Ad

    Post Bottom Ad