Summary of abstract classes and Virtual Methods]

Source: Internet
Author: User
Abstract class definition:


Its function is to generate sub-classes and give them some specific attributes and methods.
Abstract modifiers can be used with classes, methods, attributes, indexers, and events. Use the abstract modifier in the class declaration to indicate that a class can only be the base class of other classes. Members marked as abstract or included in an abstract class must be implemented through classes derived from the abstract class.

Features:
1. abstract classes cannot be instantiated;
2. abstract classes can contain abstract methods and abstract accessors;
3. the abstract class cannot be modified with the sealed modifier, which means that the abstract class cannot be inherited;
4. The flying abstract class derived from the abstract class must include all the inherited abstract methods and the implementation of the abstract accessors.

Summary:
~ The abstract method is an implicit virtual method;
~ Only abstract methods can be declared in abstract classes;
~ The abstract method declaration does not provide actual implementation, so there is no method body. The method declaration ends with a semicolon and there is no braces "{}" after the signature. the implementation is provided by a unique method, which is a non-abstract class member;
~ It is incorrect to use the static or virtual modifier in the abstract method declaration;
~ Except for the differences in the declaration and call syntax, abstract attributes behave the same way as abstract methods;
~ It is incorrect to use the absteact Modifier on static attributes;

~ In a derived class, you can override abstract inheritance attributes by including the attribute declaration using the override modifier.

 

using System;abstract class A{    public abstract void F();    protected int _x;    public abstract int X    {        get;        set;    }}class B:A{    public override void F()    {            }    public override int X    {        get{return _x;}        set{_x=value;}    }}class Test{    static void Main()    {        B b=new B();        b.X=10;        Console.Write(b.X);    }}
View code Virtual method definition:

 


Simply put, a virtual method is a method that can be rewritten by the quilt class. If the subclass overrides the virtual method, the re-written logic will be used during the runtime. If not, the logic of the virtual method in the parent class is used;
Virtual keywords are used to modify methods, properties, indexers, or event declarations, and allow them to be rewritten in a derived class.

Using system; Class A {public void F () {console. writeline ("a.f");} Public Virtual void g () {console. writeline (". G ") ;}} Class B: A {New Public void F () {console. writeline ("B. f ");} public override void g () {console. writeline ("B .g") ;}} class test {static void main () {B = new B (); A = B;. F (); B. F ();. G (); B. G () ;}} output: a.f B. f B .g

 

In fact, the most important thing is that the abstract method cannot be instantiated. to subclass, it must overwrite its method forcibly. The virtual method provides a choice that can overwrite and inherit the Virtual Methods in the base class.

Summary and comparison:

Differences between abstract and Virtual Methods:

~ The difference between an abstract method and a virtual method is that a virtual method has an implementation part and provides an option for a derived class to overwrite the method. On the contrary, an abstract method does not provide an implementation part, forces the override method of a derived class (otherwise, the derived class cannot be a specific class );
~ Abstract METHODS can only be declared in abstract classes, but virtual methods are not;
~ Abstract methods must be rewritten in a derived class, but virtual methods do not;
~ Abstract METHODS cannot declare method entities, but virtual methods can.

 

Transferred from peterluo's technical blog

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.