The difference and usage of abstract and virtual in C #

Source: Internet
Author: User

The abstract method, as its name implies, is abstracted.

1, the abstract method is not realized, it must be shaped like:

public abstract void Init ();


2. A class that has an abstract method must modify an abstract keyword to become an abstraction class, but conversely, abstract classes do not necessarily have abstract methods, such as I write a non-abstract class, but this class I do not want to let people directly instantiate, and only let people inherit, I can turn him into an abstract class, Although there is no abstract method inside of him. Shaped like:

abstract class Testabstractsuper


3, abstract class can not be instantiated, can only be inherited.

4. Subclasses of an abstract class must implement each abstract method.

5, abstract class in addition to the abstract method can also have a common method.

6, the constructor of the abstract class can be omitted, the compiler will automatically add, but not an abstract method, but only a normal constructor.

Summary: A brief summary, abstract class can not be directly instantiated, he can have N (n>=0) abstract methods, these abstract method subclasses must be implemented.

Besides virtual, he has several features:

1. The method that declares virtual does not need to change the declaration of class, he only has influence on this method.

2, only virtual method can be overridden class.

3, subclasses can not ouverride the virtual method of the parent class, in this case he is like the normal method of the parent class.

Summary: A simple summary, the virtual keyword is to tell the subclass, this method can be override, but not mandatory.

On the code:

[CSharp] View plain copy
_______________________________________abstract____________________________________________//


Parent class-Abstract class:

[CSharp] View plain copy
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Diagnostics;

Namespace Testappallhere
{
Abstract class Testabstractsuper
{
Public Testabstractsuper ()
{
Debug.WriteLine ("This is the construction method of the parent class");
}

public abstract void Init ();

public void Hello ()
{
Debug.WriteLine ("This is a non-abstract method of the parent class");
}
}
}

Subclass of Abstract class:
[CSharp] View plain copy
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Diagnostics;

Namespace Testappallhere
{
Class Testabstractsub:testabstractsuper
{
Public Testabstractsub ()
{
Debug.WriteLine ("This is the construction method of the subclass");
}

public override void Init ()
{
Debug.WriteLine ("This is the method of subclass rewriting");
}

}
}

Calling code:
[CSharp] View plain copy
<span style= "White-space:pre" > </span> testabstractsub sub = new Testabstractsub ();
Sub. Init ();
Sub. Hello ();

Output:

[CSharp] View plain copy
This is how the parent class is constructed
This is how the subclass is constructed
This is the method that the subclass overrides
This is a non-abstract method of the parent class


[CSharp] View plain copy
_______________________________________virtual____________________________________________//

Parent class:
[CSharp] View plain copy
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Diagnostics;

Namespace Testappallhere
{
Class Testvirtualsuper
{
Public Testvirtualsuper ()
{
Debug.WriteLine ("This is the construction method of the parent class");
}
public virtual void Init ()
{
Debug.WriteLine ("This is the virtual method of the parent class");
}
}
}

Sub-class:
[CSharp] View plain copy
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Diagnostics;

Namespace Testappallhere
{
Class Testvirtualsub:testvirtualsuper
{
Public Testvirtualsub ()
{
Debug.WriteLine ("This is the construction method of the subclass");
}
___________________ does not override this virtual method and can run as usual, calling the parent class's virtual method _________________________
___________________ overrides the parent class after the virtual method does not run, but overrides, of course, can use base. To invoke the parent class's method __________
public override void Init ()
{
Base. Init ();
Debug.WriteLine ("This is the overriding method for subclasses");
}

}
}
Calling code:
[CSharp] View plain copy
<span style= "White-space:pre" > </span> testvirtualsub sub = new Testvirtualsub ();
Sub. Init ();

Output:
[CSharp] View plain copy
This is how the parent class is constructed
This is how the subclass is constructed
This is the overriding method for subclasses

The difference and usage of abstract and virtual in 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.