Tuesday, April 10, 2007

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.


venu on June 6, 2007 at 4:24 AM said...

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

Dharm on June 6, 2007 at 5:14 AM said...

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!!

Mandeep Singh, Delhi said...

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.

Anonymous said...

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

vino on June 20, 2011 at 7:10 PM said...

when the program is unreachable garbage collector ll be invoked.

Anonymous said...

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.


Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker