Single root structure of Java

Source: Internet
Author: User

In object-oriented program design, because of the introduction of C + + is particularly prominent question is: All classes should ultimately inherit from a separate base class. In Java (like almost any other OOP language), the answer to this question is yes, and the name of the ultimate base class is simple, an "Object". This "single root structure" has many advantages.
All objects in a single structure have a common interface, so they all end up in the same type. Another scenario (just like C + +) is that we can't guarantee that everything belongs to the same basic type. From a backward-compatible perspective, this scenario can be better coordinated with the C model and can be considered less restrictive. But for the holidays we want to do pure object-oriented programming, we have to build our own structure to get the same convenience as built into other OOP languages. You need to add a variety of new libraries that we want to use, as well as other incompatible interfaces. Of course, this also requires extra effort to get the new interface to work with its own design (and possibly multiple inheritance). Is it worth paying for the extra "flexibility" of C + +? Of course, if you really need-if you are already a C expert, if you have a hard feelings about C-then it's really worth it. But if you're a novice, it may be easier to replace a Java-like solution for the first time you get to this type of design.
All objects in a single structure, such as all Java objects, can be guaranteed to have some specific functionality. In our own system, we know that we can perform some basic operations on each object. A single root structure, combined with all objects created in the memory heap, can greatly simplify the transfer of parameters (which is a complex concept in C + +).
With a single root structure, we can more easily implement a garbage collector. The necessary support for this can be installed in the underlying class, and the garbage collector can send the appropriate message to any object within the system. Without such a single root structure, and the system manipulates objects through a handle, the way to implement the garbage collector is very different and there are many obstacles.
Because the Run-time type information must exist in all objects, there is never a situation where the type of an object cannot be judged. This is particularly important for system-level operations, such as unauthorized controls, and can be more flexible when designing.
But you may also have the question, since you have made such a good talk, why C + + does not use a single root structure? In fact, this is a result of early trade-offs between efficiency and control. A single structure can lead to some limitations in programming. And more importantly, it makes it more difficult for the new program to be compatible with the original C code. Although these limits can really cause problems only on specific occasions, C + + ultimately decides to abandon the single root structure for maximum flexibility. Java does not have this problem, it is a new design of a language, do not have to maintain the existing language "backward compatibility." So naturally, like most object-oriented programming languages, a single root structure was quickly implemented in the Java design scheme.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.