Advantage of singly rooted class hierarchy

后端 未结 4 593
抹茶落季
抹茶落季 2020-12-17 04:49

Excuse me if this is a silly question, but I want to understand all the advantages of singly rooted class (object) hierarchy in language like .Net, Java.

I can thin

相关标签:
4条回答
  • 2020-12-17 05:28

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

    0 讨论(0)
  • 2020-12-17 05:30

    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 GetHasCode, Reflection without GetType etc.

    0 讨论(0)
  • 2020-12-17 05:39

    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.


    0 讨论(0)
  • 2020-12-17 05:47

    It provides a base for everything. For example in C# the Object class it 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.

    0 讨论(0)
提交回复
热议问题