In-depth analysis of virtual functions and pure virtual functions (differences between C ++ and Java virtual functions)

Source: Internet
Author: User

C ++
Virtual Functions
1. Definition: A member function declared as virtual in a base class and redefined in one or more Derived classes [1]
2. Syntax: virtual function return type function name (parameter table) {function body}
3. Purpose: Implement polymorphism. by pointing to the base class pointer of the derived class, access the member function with the same name in the derived class, that is, allow the subclassOverride parent class method with the same name.
The role of a virtual function is to implement dynamic association, that is, to dynamically select an appropriate member function in the running stage of the program. After defining a virtual function, you can redefine the virtual function in the derived class of the base class. The newly defined function in the derived class should have the same number of parameters and parameter type as the virtual function (that is, the same method, the function name cannot be the same .). To implement unified interfaces and different definition processes. If the virtual function is not redefined in the derived class, it inherits the virtual function of its base class.

When the program finds the keyword virtual before the virtual function name, it automatically uses it as a dynamic concatenation processing, that is, it dynamically selects the appropriate member function when the program is running. Virtual functions are a manifestation of C ++ polymorphism. Dynamic Association stipulates that virtual functions can only be called by referring to the pointer of the base class or the reference of the base class object. The format is as follows:
1. pointer variable name to the base class-> virtual function name (real parameter table)
2. Reference name of the base class object. virtual function name (real parameter table)
By using virtual functions, we can flexibly bind resources dynamically, at a certain cost. If the function (method) of the parent class is unnecessary or cannot be implemented, you can set this function (method) to virtual function name = 0 if it depends entirely on the subclass. For example: virtual void fun () = 0. We call such a function (method) a pure virtual function. If a class contains pure virtual functions, this class is called an abstract class.
Summary:If a subclass wants to override the method of the parent class, the member method of the parent class must be virtual, that is, the method must be a virtual function.

Java
In java, all methods are virtual by default. As long as the methods are not declared as final type, they must be virtual functions and do not need to be declared as virtual for methods. Mentioned In <core java2: volum I>: "In Java, you do not need to declare a method as virtual. dynamic binding is the default behavior. if you do not want a method to be virtual, you tag it as final ". Therefore, we found that in java, subclasses can override (override) the parent class method, while the parent class does not declare virtual.

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.