C + + virtual function virtual, pure virtual function abstract and Java abstraction function, interface interface and abstract class comparison

Source: Internet
Author: User

Because both C + + and Java are object-oriented programming languages, their polymorphism is achieved by virtual functions and abstract functions, respectively.

The virtual function of C + + can be overridden in subclasses, and the invocation is judged by the actual object, not by the pointer type (the invocation of the normal function is based on the current pointer type). A pure virtual function is a function that is defined only in the parent function and not implemented, and cannot be used to declare an object or to be called an abstract class. Implementations of pure virtual functions can also be defined outside the class declaration. Abstract class in C + + refers to a class that has at least one pure virtual function, and if a class consists entirely of pure virtual functions and does not include any implementation, it is called pure virtual class.

The normal function in Java comes with the function of virtual function, which is judged by the type of the object pointed to by the pointer. There is no virtual keyword in Java, Java Tube virtual function is called abstract functions, the abstract class is named abstract class, without the concept of pure, but invented a kind of called interface interface to replace the pure virtual class. The difference between interface and abstract class is similar to the difference between a generic abstract class and a pure virtual class in C + +.

Abstract classes can only be used as base classes, cannot be defined objects, the implementation of their pure virtual functions in the derived class, if the derived class also does not give the implementation, then the derived class is an abstract class, only the pure virtual function implementation of the derived class to establish the object. This shows that abstract class is the rhythm of solitary birth, ah, never object.

Other aspects of the comparison are shown in this blog C + + virtual function vs. Java Abstract function comparison interface and abstract class comparison.

C + + virtual function virtual, pure virtual function abstract and Java abstraction function, interface interface and abstract class comparison

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.