Virtual functions, pure virtual functions, abstract classes, interfaces (java_c++_c#)

Source: Internet
Author: User
Tags abstract definition

In OOP programming languages, polymorphism is an important OOP idea. But the multi-state syntax support is inseparable from the virtual function. Each time to put what virtual function, abstract class confused, this time to tidy up!

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------

In Java, there is no concept of virtual function, the default function is virtual function, dynamic binding is the default behavior of Java, in Java syntax if you do not want the function has a virtual function, you can use the final keyword into a non-virtual function.

In Java there is the concept of abstract functions, using the abstract definition, which is similar to the definition of pure virtual functions in C + +.

In Java, an abstract function is defined without implementation, its implementation is provided by subclasses, and a class containing abstract functions must be declared as abstract classes.

The interface in Java contains only constants and abstract methods, and interfaces are similar to pure virtual base classes in C + +. Interfaces in Java are designed to implement multiple inheritance. Interfaces and abstract classes in Java cannot be instantiated.

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------

Common functions in C + + can be overridden after inheritance, but to support polymorphism, it must be a virtual function or a pure virtual function. A virtual function can be implemented by adding the virtual key before the function declaration, whereas a pure virtual function is declared as virtual void funtion () =0

In C + +, a class can be instantiated as long as there is a pure virtual function, but there is no need to declare the class abstract in C + +.

In C + +, there is no definition of an interface, but a pure virtual base class can be understood as an interface.

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------

The syntax in C # is like a combination of C + + and Java, where a function must be declared as a virtual function (which can be implemented) or an abstract function (not implemented) to override in a subclass.

where abstract classes in C # can contain abstract methods and non-abstract methods, similar to other languages, abstract classes cannot be instantiated.

The interface in C # cannot be implemented as a member method, only in the class that implements the interface, and must be fully implemented, similar to the interface in Java.

Virtual functions, pure virtual functions, abstract classes, interfaces (java_c++_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.