Abstract Functions in Java and virtual functions in C ++

Source: Internet
Author: User

1: Java does not have the concept of virtual functions, but has the concept of abstract functions. abstract keywords are used to indicate that Abstract Functions in Java must be in abstract classes, and abstract functions cannot have function bodies, abstract classes cannot be instantiated. They can only implement Abstract Functions by their subclasses. If a subclass of an abstract class is still an abstract class, this subclass does not need to implement Abstract Functions of its parent class.

2: C ++ has the concept of virtual functions, which are expressed by virtual keywords. Each class has a virtual function table, the virtual function table will first inherit from the parent class to obtain the virtual function table of the parent class. If the child class is rewritten with the virtual function of the parent class (no matter whether the rewritten function is a virtual function ), which virtual function to call is determined based on the actual object (whether or not the pointer belongs to the current class or not, it can be a parent type ), if the pointer points to an object of any type, it calls a class-defined virtual function. Each class has only one virtual function table, and all objects share this table.

The function polymorphism of C ++ is implemented through virtual functions.

3: In C ++, if a function is not a virtual function, a function is called based on the current pointer type, rather than the object type pointed to by the pointer.

4: in Java, if a function is not an abstract function, but a common function, it is implemented by default similar to the virtual function in C ++, that is, to call a function, it is determined based on the type of the object to which the current Pointer Points, rather than based on the pointer type. This is exactly the opposite of a common function in C ++. That is, virtual functions are automatically implemented in Java.

C ++ Java
Virtual functions -------- common functions
Pure virtual functions -------- Abstract Functions
Abstract class -------- abstract class
Virtual base class -------- Interface

Pure virtual functions: these functions are designed to ensure the direct ing between the program structure and the application domain structure. They cannot be used to declare objects. They are abstract classes. Classes with pure virtual functions are called abstract classes. abstract classes can be inherited and used by quilt classes. The implementation of pure virtual functions must be provided in the subclass. If the subclass does not provide the implementation of this pure virtual function, this subclass is also an abstract class. Only when the subclass does not have a pure virtual function can the subclass be used to declare the object! Abstract classes can also be used to declare pointers or references, or to declare functions. Classes with abstract class features include constructor and destructor, all of which are protected classes. If the implementation of pure virtual functions is not provided, it cannot be called directly or indirectly in the constructor or destructor of its class. The implementation of pure virtual functions can be defined outside the class declaration.

5. c ++ generally declares destructor as virtual functions. Because Virtual functions can be dynamically bound, that is, whether to call a function depends on the object to which the pointer is currently directed, rather than the pointer type. If C ++ does not declare the Destructor as a virtual function, it has a subclass that overwrites the virtual function. When the parent class Pointer Points to a subclass object, when you call a destructor, you can only call the destructor of the parent class, but cannot call the destructor of the subclass. Therefore, you generally declare the Destructor as a virtual function, implement dynamic binding. Of course, if a class does not contain virtual functions, this often indicates that it is not intended to be used as a base class. When a class is not intended as a base class, it is usually a bad idea to declare the Destructor as virtual.
The standard string type does not contain virtual functions. If the string is inherited as the base class, a problem occurs.
In short: The polymorphism base class should declare the virtual destructor. If a class has any virtual functions, it should have a virtual destructor. If it is not designed for base classes or not for polymorphism, such classes should not declare virtual destructor.

Related Article

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.