Abstract classes and abstract methods in C #

Source: Internet
Author: User

I. Concepts of abstract classes and abstract methods

Virtual methods in a base class can sometimes not be called, but simply express an abstract concept that provides a common interface for its derived classes. C # introduces the concept of abstract class, which can be defined as an abstract method. Define the class where the method resides as an abstract class.

Abstract method: Contains only the method definition, but does not have the concrete implementation method, needs its subclass or subclass to implement concretely.

Abstract class: An abstract class is a class that can contain abstract members. Abstract classes can only be used as base classes and cannot be instantiated.

Ii. principles of use of abstract and abstract methods

1 A non-abstract method can exist in an abstract class, and an abstract method must be included in an abstract class.

2 Implement the abstract method with the Override keyword. If the subclass does not implement all the abstract methods in the abstract base class, the subclass must also be defined as an abstract class.

3 The modifier cannot be changed after the abstract method is implemented.

4 abstract classes can be inherited by abstract classes, and the results are still abstract classes.

5 All abstract methods must be implemented in a derived class.

Let's take a look at the abstract class in an example:

[CSharp]View PlainCopy  
  1. <span style="Font-family:microsoft yahei;font-size:18px;" > abstract Class Shapleclass declaration
  2. {
  3. abstract public void area (); //abstract method, except that the declaration has no actual implementation, so it ends with a semicolon, and no curly braces after the signature
  4. public Void Fun () //member function
  5. {
  6. Console.WriteLine ("This is a non-abstract method!  ");
  7. }
  8. }
  9. class Shap:shapleclass
  10. {
  11. public override void area () //All methods derived from an abstract class must implement an abstract method in an abstract class
  12. {
  13. Console.WriteLine ("This is an abstract method");
  14. }
  15. }</span>

[CSharp]View PlainCopy  
  1. <span style="Font-family:microsoft yahei;font-size:18px;" > class Program
  2. {
  3. static void Main (string[] args)
  4. {
  5. Shap Circle = new Shap ();
  6. Circle.fun ();
  7. Circle.area ();
  8. }
  9. }</span>


The results of the operation are as follows:

Abstract classes and abstract methods 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.