Discussion on different implementation mechanisms of hiding and rewriting Java and C + +

Source: Internet
Author: User

First, the article for the reason

I now use more than C + +, but Java has also written a lot, Java and C + + is very similar, but dig some, Java and C + + is very big, take just another blog virtual function and a multi-state, the inside feel a lot different, at least "rewrite" In the two languages, the understanding is different ~ ~ with the Friends of the discussion, decided to thoroughly smoothed the question, because this is the discussion, so there are different ideas welcome to make and comment.

Ii. definition of "override" override

What a common concept of "rewriting".

1, first of all, "override" (override) is completely equal to "overwrite", perhaps because of the translation of the problem, is a very pit father problem

2, and the definition of Override in C + + and Java are different ~ ~

This defines "overrides" in C + +:

Override refers to a derived class function overriding a base class function:
(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) The same parameters, different parameters are not called rewriting, is simply two functions;
(4) The base class function must have the virtual keyword .

Pay special attention to the last point ~ ~ This is different from the hidden key point

This defines "overrides" in Java:

(1) In subclasses, methods that inherit from the base class can be overridden as needed, and method overrides are also known as methods.
(2) The overridden method and the overridden method must have the same method name, argument list, and return type.
(3) The overriding method cannot use more restrictive access than the overridden method. If you need a method from the parent class, you can use the Super keyword, which references the parent class of the current class.

There is nothing, there is no virtual keyword in Java , so there is no "rewrite" and "hidden" differences, Java rewrite must be hidden in the parent class method, to access the Super keyword can be used, so Java inside only Override

Iii. in-depth understanding of Hidden and Override in C + +

See the definition has been found to be different, now to see the implementation of Hidden and override in C + + mechanism.

When it comes to the C + + override, it's important to talk about hidden, which is absolutely separate in C + +.

3.1 Hidden

C + + hidden definition (a function of a derived class masks a base class function with the same name):

(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (note not to be confused with overloading)

(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay)

This is the class capacity mentioned in the previous blog post, so what is the implementation mechanism ?

When invoking a class's member function, the compiler looks up the definition of the function along the inheritance chain of the class, and if found, stops the lookup, so if a derived class and a base class have the same name (regardless of whether the parameter is the same), And the compiler finally chooses a function in the derived class, then we say that the member function of the derived class "hides" the member function of the base class, which means it prevents the compiler from continuing to look up the definition of the function ....

Back to the hidden definition, the virtual keyword has been mentioned earlier and is located in the same name as the derived class and base class, with the same parameter function as the overridden relationship, so the hidden relationship is possible only as follows:

1) must be in a derived class and a base class, respectively

2) must have the same name

3) When the parameters are different, they do not constitute a cover relationship, so whether or not the virtual function is not important at this time.

When the parameters are the same time to see the virtual keyword, there is the cover relationship, no time is to hide the relationship

3.2 Override

Overrides refer to a virtual function of a derived class that overrides a function with the same name and the same parameter as the base class.

Since it is linked to a virtual function, it shows that this is a polymorphic support feature, so-called overlay refers to the use of a base class object pointer or reference when accessing the virtual function will be based on the actual type of the function called, so the member function of the derived class can "overwrite" the base class member functions.

Note: It is also an important feature of derived classes that only the same name and the same arguments have the virtual keyword and the functions of the derived class and the base class .
Also, because it is linked to polymorphism, it can only be used when using a pointer to a class object or a reference.

In a word: overriding functions are virtual functions, and vice versa

Iv. code Examples

Accreditations, on the code ~ ~

The Java code is as follows:

 Public  class polytest{     Public Static void Main(string[] args) {//Up type conversionCat cat =NewCat ();        Animal Animal = cat; Animal.sing ();//down type conversionAnimal A =NewCat ();        Cat C = (cat) A;        C.sing (); C.eat ();//Compilation error        //Use a parent class reference to call a method that does not exist in the parent class        //animal a1 = new Cat ();        //a1.eat ();        //Compilation error        //down type conversion can only be directed to the object type        //animal a2 = new Cat ();        //cat c2 = (Dog) A2;}}class animal{ Public void Sing() {System.out.println ("Animal is singing!"); }}class Dog extends animal{ Public void Sing() {System.out.println ("Dog is singing!"); }}class Cat extends animal{ Public void Sing() {System.out.println ("Cat is singing!"); } Public void Eat() {System.out.println ("Cat is eating!"); }}

Run out of the results as follows:

As can be seen, Java even if the second set of "Parent class strong rotor class", output is still a subclass of things, and not like the late binding of C + +.

The C + + code is as follows:

#include <iostream>using namespace STD;classanimal{ Public:Virtual voidSing () {printf("Animal is singing!\n"); }};classCat: Publicanimal{ Public:voidSing () {printf("Cat is singing!\n"); }voidEat () {printf("Cat is eating!\n"); }};intMainvoid) {Animal A;    Cat *CP = (CAT *) &a;    Cp->sing (); Cp->eat ();//Compilation error//Cat C;//Animal a = C;//A.eat ();    return 0;}

The results of the operation are as follows:

C + + Here is a good embodiment of the late binding features ~ ~ ~ Even if the cast, pointing to still is the address of the base class object, so through the virtual function list or can find the implementation of the base class ~

V. Summary

Java and C + + in the hidden, rewrite the difference is introduced here, just online markdown did not save, opened the previous editor, almost thought that this article, code a lot of words ah, scared silly ... It seems to be more imported to the local ~ ~ HAHAHA

-end-

Reference documents

[1] http://bbs.chinaunix.net/thread-643467-1-1.html
[2] Http://www.cnblogs.com/mengdd/archive/2012/12/25/2832288.html

Copyright NOTICE: Welcome Reprint, note The source is good! If you do not like please leave a message to explain why again step on Oh, thank you, I can also know the reason, continuous progress!!

Discussion on different implementation mechanisms of hiding and rewriting Java and C + +

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.