In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the difference between Java and C++ subclass on the reduction of accessibility of parent function coverage". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
The topic of "reducing the accessibility of subclasses of Java and C++ to parent functions" may seem academic, but it is indeed a problem that is easy to ignore. This article strives to elaborate on the difference between this issue in Java and C++.
Let's first introduce what is the "reduced accessibility of subclasses to parent class function overrides". For inheritance, a subclass can override the "virtual function" of the parent class-although there is no term virtual function in Java, all functions in Java can be treated as virtual functions, because all functions in Java can be overridden by subclasses. Here we only borrow the meaning of the noun "virtual function" and do not delve into the details of the language. Both Java and C++ allow you to change the accessibility of a function when overridden. The so-called "accessibility" is modified by public, protected, private and other access control characters to control whether the function can be accessed. The usual order of accessibility is (since there is no concept of package in C++, the package access control character will not be considered for the time being, which does not affect the discussion here):
Public > protected > private
Take Java as an example:
Class Base {protected void sayHello () {System.out.println ("Hello in Base");}} class Child extends Base {public void sayHello () {System.out.println ("Hello in Child");}}
Note: the sayHello () function here. In the parent class Base, this function is decorated with protected access control characters. The subclass changes it to public, which won't be a problem. When a subclass overrides a parent function, it is usually not a problem to expand accessibility.
When the accessibility of the subclass to the parent function overrides is reduced, Java and C++ adopt different strategies.
First, taking Java as an example, take a look at the following code:
Class Base {public void sayHello () {System.out.println ("Hello in Base");}} class Child extends Base {private void sayHello () {System.out.println ("Hello in Child");}}
In the above code, there is a compilation error on the highlighted line 8-this code cannot be compiled at all! Java does not allow subclasses to reduce accessibility when overriding the parent class function. As for the reason, we can use an example to illustrate it. For example, we write the following code outside the class:
Base base = new Base (); base.sayHello (); base = new Child (); base.sayHello ()
If the previous code can be compiled, it is possible that when base points to new Base (), sayHello () is accessible, but when base points to new Child (), sayHello () cannot! In Java's view, this is a contradiction and should be avoided, so Java stipulates that we cannot write the above code from the compiler's point of view.
There is a difference in the situation in respect of Clearing County. Take a look at the example of C++:
Class Base {public: virtual void sayHello () {std::cout f (); / / error: DVO virtual void sayHello f () is private}
In response, the C++ standard gives the following explanation:
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:
Access control is checked during the call, that is, whoever calls the function checks to see if anyone can access the function.
The accessibility of a member function in a class is generally unknown, that is, when checking accessibility, you cannot know whether the function is public or private when it is defined, so it is not possible to check accessibility accordingly.
For this reason, the caller of C++ seems to be able to "skillfully" call a function that would otherwise be inaccessible through some technical transformation. A more practical example is that 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 code of Qt.
In summary, when subclasses override parent functions, Java strictly limits that subclasses cannot reduce the accessibility of functions, but C++ does not. Personally, from the perspective of software engineering, there is no doubt that the provisions of Java have more engineering significance, and the calls of functions are more consistent. C++ 's standard will significantly simplify the compiler implementation, but it is not a good reference for engineering.
That's all for the introduction of the difference between Java and C++ subclasses on reducing the accessibility of parent function overrides. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.