Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of singly rooted class hierarchy

I want to understand all the advantages of singly rooted class (object) hierarchy in languages like .NET, Java.

I can think of one advantage. Let's say I have a function which I want to accept all data types (or references thereof). Then in that case instead of writing a function for each data type, I can write a single function:

public void MyFun(object obj)
{
     // Some code
}

What other advantages we get from such type of hierarchy?

like image 308
Atul Sureka Avatar asked Dec 29 '11 15:12

Atul Sureka


2 Answers

I'll quote some lines from a nice book - Thinking in Java by Bruce Eckel:


All objects in a singly rooted hierarchy have an interface in common, so they are all ultimately the same type. The alternative (provided by C++) is that you don’t know that everything is the same fundamental type. From a backward-compatibility standpoint this fits the model of C better and can be thought of as less restrictive, but when you want to do full-on object-oriented programming you must then build your own hierarchy to provide the same convenience that’s built into other OOP languages. And in any new class library you acquire, some other incompatible interface will be used. It requires effort (and possibly multiple inheritance) to work the new interface into your design. Is the extra “flexibility” of C++ worth it? If you need it—if you have a large investment in C—it’s quite valuable. If you’re starting from scratch, other alternatives such as Java can often be more productive.


All objects in a singly rooted hierarchy (such as Java provides) can be guaranteed to have certain functionality. You know you can perform certain basic operations on every object in your system. A singly rooted hierarchy, along with creating all objects on the heap, greatly simplifies argument passing.


A singly rooted hierarchy makes it much easier to implement a garbage collector (which is conveniently built into Java). The necessary support can be installed in the base class, and the garbage collector can thus send the appropriate messages to every object in the system. Without a singly rooted hierarchy and a system to manipulate an object via a reference, it is difficult to implement a garbage collector.


Since run-time type information is guaranteed to be in all objects, you’ll never end up with an object whose type you cannot determine. This is especially important with system level operations, such as exception handling, and to allow greater flexibility in programming.


like image 121
Sajib Mahmood Avatar answered Oct 19 '22 02:10

Sajib Mahmood


A single-rooted hierarchy is not about passing your objects to methods but rather about a common interface all your objects implement.

For example, in C# the System.Object implements few members which are inherited down the hierarchy.

For example this includes the ToString() which is used to get a literal representation of your object. You are guaranteed that for each object, the ToString() will succeed. At the language level you can use this feature to get strings from expressions like (4-11).ToString().

Another example is the GetType() which returns the object of type System.Type representing the type of the object the method is invoked on. Because this member is defined at the top of the hierarchy, the reflection is easier, more uniform than for example in C++.

like image 32
Wiktor Zychla Avatar answered Oct 19 '22 03:10

Wiktor Zychla