Differences between abstract classes and virtual methods

Source: Internet
Author: User

Abstract class Definition:
Its function is to produce subclasses while giving the applies class some specific properties and methods.
The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can only be the parent class of another class. A member that is marked as abstract or contained in an abstract class must be implemented by a subclass of the abstract class.

Characteristics:
1. Abstract classes cannot be instantiated;
2. Abstract classes can contain abstract methods and abstract accessors;
3. Cannot be modified with sealed modifier;
4. A non-abstract class derived from an abstract class must include all inherited abstract methods and the implementation of the abstract accessor.

Summarize:
~ Abstract methods are implicit virtual methods;
~ Only the use of abstract method declarations in abstract classes is allowed;
~ because the abstract method declaration does not provide the actual implementation, there is no method body; The method declaration ends with a semicolon, and there is no curly brace "{}" after the signature, and the implementation is provided by an overriding method, which is a member of the abstract class;
~ using static or virtual modifiers in an abstract method declaration is wrong;
~ except in the syntax of Declaration and invocation, the behavior of abstract attributes is the same as that of abstract methods;
~ Using the abstract modifier on a static property is an error;

~ in a derived class, you can override an abstract inherited property by including a property declaration that uses the override modifier. Abstract class Definition:
Its function is to produce subclasses while giving the applies class some specific properties and methods.
The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can only be the parent class of another class. A member that is marked as abstract or contained in an abstract class must be implemented by a subclass of the abstract class.

usingSystem;Abstract classa{ Public Abstract voidF (); protected int_x;  Public Abstract intX {Get; Set; }}classb:a{ Public Override voidF () {} Public Override intX {Get{return_x;} Set{_x=value;} }}classtest{Static voidMain () {b b=NewB (); b.x=Ten;    Console.Write (b.x); }}

Virtual method Definition:
Simply put, a virtual method is a method that can be overridden by a quilt class, and if a subclass overrides a virtual method, the runtime uses the rewritten logic and, if there is no Override, uses the logic of the virtual method in the parent class;
The virtual keyword is used to decorate a method, property, indexer, or event declaration and to allow these objects to be overridden in a derived class.

usingSystem; classA { Public   voidF () {Console.WriteLine ("A.F")   ; }      Public   Virtual   voidG () {Console.WriteLine ("A.G")   ; }     }       classb:a {New    Public   voidF () {Console.WriteLine ("B.f")   ; }      Public   Override   voidG () {Console.WriteLine ("B.G")   ; }     }       classTest {Static   voidMain () {b b=NewB (); A A=b;       A.F ();       B.f ();       A.G ();       B.G (); }} The output is: A.F b.f b.g b.g

In fact, the most important thing is that the abstract method can not be instantiated, the subclass must be mandatory to overwrite its methods. The virtual method provides the choice to override the virtual method in the base class that can be overridden without overwriting.

The summary comparison is as follows:

The difference between an abstract method and a virtual method:

~ The difference between an abstract method and a virtual method is that the virtual method has an implementation part and provides an option for the derived class to overwrite the method, whereas the abstract method does not provide the implementation part, forcing the derived class to override the method (otherwise the derived class cannot be a concrete class);
The ~abstract method can only be declared in an abstract class, but the virtual method is not;
The ~abstract method must be overridden in a derived class, while virtual does not have to;
The ~abstract method cannot declare a method entity, but a virtual method can.

Differences between abstract classes and virtual methods

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.