Introduction to the difference between Java and C + + subclasses in reducing the accessibility of parent class function coverage

preface

The topic "the problem of reducing the accessibility of subclasses in Java and C + + to the coverage of parent functions" seems more academic, but it is indeed an easy problem to ignore. This paper tries to elaborate the difference between Java and C + +.

Let's first introduce what is "accessibility reduction of subclass over parent function coverage". For inheritance, subclasses can override the "virtual function" of the parent class -- although there is no term virtual function in Java, all functions in Java can be regarded as virtual functions, because all functions in Java can be overridden by subclasses. Here, we only borrow the meaning of the term "virtual function", and do not delve into the details of the language. Both Java and C + + allow you to change the accessibility of functions when overridden. The so-called "accessibility" refers to the use of public, protected, private and other access control characters for modification to control whether functions can be accessed. Generally, the order of accessibility is (since there is no package concept in C + +, package access control characters are not considered temporarily, which does not affect the discussion here):

Take Java as an example:

Note: the sayhello () function here. In the parent class base, this function is decorated with a protected access control character. The subclass will use public instead, which will not have any problem. When a subclass overrides a parent function, it is usually not a problem to expand accessibility.

Java and C + + adopt different strategies when the accessibility of subclass coverage of parent function is reduced.

First, take Java as an example to see the following code:

In the above code, the highlighted line 8 will have a compilation error - this code can't be compiled at all! Java does not allow subclasses to narrow accessibility when overriding parent functions. As for the reason, we can use an example to illustrate. For example, we write the following code outside the class:

If the previous code can be compiled, there is a possibility that when the base points to new base (), sayhello () can be accessed, but when the base points to new child (), sayhello () cannot be accessed! In the view of Java, this is a contradiction and should be avoided. Therefore, Java stipulates that we can't write the above code from the perspective of compiler.

For C + +, the situation is different. Take C + + as an example:

This code is completely correct in C + +. Note that subclasses here reduce accessibility when overriding parent functions. If you don't see any problem, we can write the following code outside the class:

The call on line 2 failed because sayhello () is private in child and cannot be called externally. However, when we use static_ When cast casts a child into a base object, things change -- sayhello () is public for base, so it can be called normally.

In view of this, the following examples can be found in the access to virtual functions section of the member access control chapter of the C + + standard:

The explanation given by the C + + standard is:

Access is checked at the call point using the type of the expression used to denote the object for which the member function is called ( B* in the example above). The access of the member function in the class in which it was defined (D in the example above) is in general not kNown.

There are two main points in simple translation:

Because of this, C + + callers seem to be able to "skillfully" call functions that cannot be accessed through some skillful transformations. A more practical example is: in QT, the QObject:: event() function is public, while the event() function of its subclass QWidget is changed to protected. Specifically, you can read the relevant codes of QT.

To sum up, when subclasses override parent functions, Java strictly restricts subclasses and cannot reduce function accessibility, but C + + does not. Personally, from the perspective of software engineering, Java regulations undoubtedly have more engineering significance, and function calls are more consistent. The C + + standard will obviously simplify the compiler implementation, but it is not a good reference for engineering.

PS: the official version of C + + standard needs to be purchased, but the draft can be downloaded for free. The download address of C + + standard draft can be found on the following page: @ L_ 502_ 0@

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>