Differences between virtual and abstract in asp.net, virtualabstract

Source: Internet
Author: User

Differences between virtual and abstract in asp.net, virtualabstract

1. Virtual Method)

Virtual keywords are used to modify methods in the base class. Virtual instances can be used in two scenarios:
Case 1: The virtual method is defined in the base class, but the virtual method is not overwritten in the derived class. In the call to a derived class instance, the virtual method uses the method defined by the base class.
Case 2: The virtual method is defined in the base class, and then override is used in the derived class to override the method. In the call to a derived class instance, this virtual method uses a derived override method.

When a method is declared as Virtual, It is a Virtual method until you use ClassName variable = new ClassName (); to declare an instance of a class, it does not exist in real memory. This keyword is frequently used in class inheritance to provide support for Polymorphism of class methods.

Ii. Abstract Method)
Abstract keywords can only be used to modify methods in abstract classes without specific implementation. Abstract methods must be implemented using the override keyword in the derived class.

Abstract method declaration is a method that must be overwritten by a derived class, and an abstract class is used for inheritance. It can be seen as a virtual method that does not implement the body. If the class contains abstract methods, classes must be defined as abstract classes, whether or not they contain other general methods. abstract classes cannot have entities.

 

Iii. Polymorphism

 

The implementation of polymorphism in C # is divided into two types: compile-time polymorphism and Runtime polymorphism.

|-During compilation, polymorphism is implemented through the overloading of multiple methods in a class. during compilation, the system determines which method to call based on the passed parameters;

|-Runtime polymorphism is implemented through virtual functions (virtual functions) and abstract methods. The Derived classes use override virtual functions or abstract methods to achieve Runtime polymorphism.

 

Iv. abstract usage

C # code Replication
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{Public abstract class Book {// abstract method, excluding the subject. The class where the abstract method is located must be an abstract class. The derived class must implement this method public abstract void Introduce ();} public class javatek: book {// The abstract method must be implemented. Note! The override keyword public override void Introduce () {Console. writeLine ("I'm Java") ;}} public class test {public test () {javatek = new javatek (); javatek. introduce (); // call Introduce () Book = new javatek (); book. introduce (); // call Introduce ()} public static void Main () {test t = new test () ;}} in javatek ();}}}

 

V. virtual usage and override usage

 

C # code Replication
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{Public abstract class Book {public virtual void Introduce () {Console. writeLine ("I'm book");} public virtual void SayHi () {Console. writeLine ("Hi, I'm book") ;}} public class javatek: Book {public override void Introduce () {Console. writeLine ("I'm Java");} // note that this method does not have the override parent class method public void SayHi () {Console. writeLine ("Hi, I'm Java") ;}} public class test {public test () {javatek = new javatek (); Book book = new javatek (); javatek. introduce (); // The Introduce () book in the javatek will be called. introduce (); // call Introduce () javatek in javatek. sayHi (); // The SayHi () book in the javatek will be called. sayHi (); // call SayHi ()} public static void Main () {test t = new test () ;}} in the Book ();}}}

 

Vi. Differences between virtual and abstract include

 

(1) virtual modifier methods must be implemented (even if only a pair of braces are added). abstract modifier methods cannot be implemented. For example, if the virtual modifier method is not implemented:

(2) virtual can be overwritten by the quilt class, while abstract must be overwritten by the quilt class. If the virtual modification method is rewritten, override must be added before (this tells the compiler that you want to rewrite the virtual method) and must be implemented; otherwise, the compilation fails;

(3) If the class member is abstract modified, the abstract must be added before the class, because only abstract classes can have abstract methods.

(4) An abstract class instance cannot be created. It can only be inherited and cannot be instantiated. For example, BaseTest2 base2 = new BaseTest2 (); a compilation error occurs: the abstract class or interface cannot be used to create an instance.
 

(5) in C #, if you want to override a method in the subclass, you must add virtual before the parent class method and override before the subclass method, this avoids the programmer from accidentally overwriting the parent class method in the subclass.
 

(6) abstract methods must be rewritten, and virtual methods must be implemented (even if they are defined in abstract classes ).


Which of the following heroes told me about the differences and usage of virtual, abstract, and override in c #? I am very grateful. I am a beginner. I am very confused when I just learned this section.

Virtual declares A method as A virtual method so that the derived class can override this method. Generally, the created method cannot be rewritten. For example, there is A method in Class.
Protected void method (){
Original code ....;
}
Class B is inherited from Class A. Class B can call the method () method of Class A, but cannot change the method body Code. However, when class A uses virtual declarations, this method is: protected virtual void method (), Class B can override this method by using override
Protected override void method (){
New Code ....;
}
Virtual can be used in base classes and abstract classes.
The abstract declaration method is an abstract method. The abstract method has no code body and only one method name is declared:
Protected abstract void method ();
When the abstract declaration method is used, its derived class must override this method. For example, Class B is declared in abstract class A and inherited from abstract class, override must be used to override this method in Class B.
Protected override void method (){
New Code ....;
}
Abstract can only be used in abstract classes.
Override is the keyword used by the derived class to override the parent class method (including the virtual method, abstract method, and interface method). If you want to override the method, you can use the override declaration.

C # use abstract and virtual

1. abstract methods can only be declared in abstract classes. Virtual methods are not.
Abstract methods must be rewritten in a derived class, but virtual methods do not.
2. abstract METHODS cannot declare method entities,
Abstract public void SD ();
You can use the virtual method.
Public virtual void sdf ()
{
Console. WriteLine ("");
}
3. The virtual method can realize polymorphism, but the abstract method cannot...

Related Article

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.