What is the difference between a virtual method and an abstract method?

Source: Internet
Author: User
Abstract methods are implemented only when declarations are not implemented, and must be implemented in subclasses. Virtual methods are sound and implemented, and can be overwritten in subclasses, or the default Implementation of parent classes can be used without overwriting.

Virtual methods are implementedCode

No abstract method,

Abstract classes cannot be instantiated. They can only be instantiated to implement the derived classes of all abstract methods.

An abstract method is a virtual method.
The abstract method is not implemented. Its existence is only a unified interface for the derived class. The derived class should implement this method.
If you write a base class, it will never be implemented, you should define one or more methods in this class
Abstract method.

Abstract methods are implemented only when declarations are not implemented, and must be implemented in subclasses. Virtual methods are sound and implemented, and can be overwritten in subclasses, or the default Implementation of parent classes can be used without overwriting.

Add one point

Only abstract methods can be declared in abstract classes.

Learning

In fact, you must first understand that the virtual method is closely related to polymorphism. The virtual method allows the derived class to completely or partially rewrite the method of this class. You need to write the method body. Abstract classes can contain abstract methods and general methods. abstract classes cannot be new. abstract methods are just a definition and there is no method body, that is, there is no {}. Do not write content in them. Both of them are similar in that they are overwritten.

 

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.