6

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?

2
  • "sanity" would be worth serious consideration; multiple inheritance is often poorly understood and used inappropriately... Maybe that is even enough of a reason (using interfaces for anything else) Dec 29, 2011 at 15:36
  • 1
    this question is nice..i wonder y no upvotes for it..
    – Anirudha
    Jan 6, 2013 at 16:28

4 Answers 4

4

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.


3

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++.

1
  • Here with singly rooted hierarchy I mean every class is inherited by object class, I do not mean multiple inheritance. From your response, does that mean the only advantage of object class is that all classes get those four default methods? Jan 6, 2012 at 11:47
1

It provides a base for everything. For example in C# the Object class is the root which has methods such as ToString() and GetType() which are very useful, if you're not sure what specific objects you will be dealing with.

Also - not sure if it would be a good idea, but you could create Extension Methods on the Object class and then every instance of every class would be able to use the method.

For example, you could create an Extension Method called WriteToLogFile(this Object o) and then have it use reflection on the object to write details of it's instance members to your log. There are of course better ways to log things, but it is just an example.

2
  • Thanks for your response. Here with singly rooted hierarchy I mean every class is inherited by object class, I do not mean multiple inheritance. From your response, does that mean the only advantage of object class is that all classes get those four default methods? Jan 6, 2012 at 11:42
  • That's exactly what it means, yes.
    – PeteGO
    Jan 30, 2012 at 0:20
0

Single rooted hierarchy enables platform developer to have some minimum knowledge about all objects which simplifies development of other libraries which can be used on all other objects.

Think about Collections without GetHashCode(), Reflection without GetType() etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.