Abstract and Virtual in C #

Source: Internet
Author: User
In C # learning, it is easy to confuse the use of virtual and abstract methods. Now let's discuss the differences between them. Both of them involve the use of override in a derived class.

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.

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.

Interfaces and abstract classes:

The essential difference is that abstract classes are incomplete classes and abstract objects, while interfaces are a kind of behavior specification.

C # is an object-oriented programming language. Every function belongs to a class.
Static: When a method is declared as Static, this method is a Static method, and the compiler will keep the implementation of this method during compilation. That is to say, this method belongs to the class but does not belong to any member. no matter whether the class instance exists or not, it will exist. Just like the entry function Static void Main, it can be called directly because it is a Static function.

Virtua: 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.
Overrride: indicates that this class is inherited from the Shape class.
The public override double Area attribute must exist in the shape, but we do not want to use the shape attribute here, so we need to rewrite it.
Virtual, abstract is to tell other classes that you want to inherit from. You can override this method or attribute of mine. Otherwise, it is not allowed.
A vivid example: Dad represents the base class (inherited class) and son represents the Child class (inherited class)
Dad told his son via virtual: "Child, you want to inherit my career, and you can continue to develop your own business"
My son told the world with override: "I didn't take my dad directly. He just gave me his path and I struggled for it myself"

Abstract: abstract method declaration is a method that must be overwritten by a derived class. abstract classes are used for inheritance. It can be considered a virtual method without an implemented body. If a class contains abstract methods, classes must be defined as abstract classes, whether or not they contain other general methods. abstract classes cannot have entities.
Example:
Interface: Used to declare an interface
1. Only some method conventions are provided, and method subjects are not provided. For example:
Public interface IPerson
{
Void getName (); // does not contain the method subject
}
2. methods cannot be modified using public abstract, without field variables or constructor.
3. The method can contain parameters. For example
Public interface IPerson
{
Void getAge (string s );
}
Example 1 ):
Public interface IPerson
{
IPerson (); // Error
String name; // Error
Public void getIDcard (); // Error
Void getName (); // right
Void getAge (string s); // right
}
Interface implementation class
1. Consistent with the format of the inherited class, such as public class Chinese: IPerson {}
2. Each method in the interface must be implemented.
Example 2: inheritance Example 1
Public class Chinese: IPerson
{
Public Chinese () {}// add structure
Public void getName () {}// implement getName ()
Public void getAge (string s) {}// implement getAge ()
}
Abstract: declaring abstract classes and abstract methods
1. The class of the abstract method must be an abstract class.
2. abstract classes cannot be directly instantiated and must be implemented by their derived classes.
3. the abstract method does not contain the method subject and must be implemented in override mode by the derived class. This is similar to the method in interface.
For example
Public abstract class Book
{
Public Book ()
{
}
Public abstract void getPrice (); // abstract method, excluding the subject
Public virtual void getName () // virtual method, which can overwrite
{
Console. WriteLine ("this is a test: virtual getName ()");
}
Public virtual void getContent () // virtual method, which can overwrite
{
Console. WriteLine ("this is a test: virtual getContent ()");
}
Public void getDate () // general method. If it is rewritten in a derived class, the new keyword must be used.
{
Console. WriteLine ("this is a test: void getDate ()");
}
}
Public class javatek: Book
{
Public override void getPrice () // implements the abstract method, which must be implemented
{
Console. WriteLine ("this is a test: javatek override abstract getPrice ()");
}
Public override void getName () // overwrite the original method, not required
{
Console. WriteLine ("this is a test: javatek override virtual getName ()");
}
}
The test is as follows:
Public class test
{
Public test ()
{
Javatek jbook = new javatek ();
Jbook. getPrice (); // call getPrice () in javatek ()
Jbook. getName (); // call getName () in javatek ()
Jbook. getContent (); // call getContent () in the Book ()
Jbook. getDate (); // call getDate () in Book ()
}
Public static void Main ()
{
Test t = new test ();
}
}
Virtual: Mark the method as virtual
1. override this method in a derived class
2. Objects can also be called without Overwriting
3. The method without this mark (or any other mark) needs to be hidden from the original method by new when rewriting.
Abstract and virtual: override keywords are used for method rewriting.
The Interface Definition starts with an uppercase letter I. The method only defines its name. in C #, the method is a public method by default. The public modification method is not allowed; otherwise, a compilation error occurs. The interface can be inherited from other interfaces, if multiple interfaces are inherited, the parent interface list is separated by commas.
Interfaces can be implemented through classes. When the base list of classes contains both the base classes and interfaces, the base classes appear in the list first. Classes must implement their abstract methods;
Interface Usage: see Code (for details)
Interface Usage
Interface Usage (instance 1)
Using System;
Namespace Dage. Interface
{
// Printer interface
Public interface IPrint
{
String returnPrintName ();
}
}
//--------------------------------------------
Using System;
Using Dage. Interface;
Namespace Dage. Print
{
// HP printer
Public class HP: IPrint
{
Public string returnPrintName ()
{
Return "this is an HP printer ";
}
}
}
//--------------------------------------------
Using System;
Namespace Dage. Print
{
// Eps printer type
Public class Eps: IPrint
{
Public string returnPrintName ()
{
Return "this is an Eps printer ";
}
}
}
//--------------------------------------------
Using System;
Using Dage. Interface;
Namespace Dage
{
// Print
Public class Printer
{
Public Printer ()
{}
Public string PrintName (IPrint iPrint)
{
Return iPrint. returnPrintName ();
}
}
}
//--------------------------------------------
-- WinFrom:
Private void button#click (object sender, System. EventArgs e)
{
Printer p = new Printer ();
Switch (this. comboBox1.Text)
{
Case "HP ":
MessageBox. Show (p. PrintName (new HP ()));
Break;
Case "Eps ":
MessageBox. Show (p. PrintName (new Eps ()));
Break;
Default:
MessageBox. Show ("This brand is not found! ");
Break;
}
}

 

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.