C # abstract class, sealed class and class members

Source: Internet
Author: User
Tags abstract define abstract

1. Abstract classes and class members

Use the abstract keyword to create an incomplete class and class member that must be implemented in a derived class.

For example:

public abstract class A
{
    // Class members here.
}

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library can define an abstract class that is a parameter to its multiple functions and require the programmer to use the library to provide its own class implementation by creating a derived class.

Abstract classes can also define abstract methods. method is to add the keyword abstract to the front of the return type of the method.

For example:

public abstract class A
{
    public abstract void DoWork(int i);
}

Abstract methods are not implemented, so the method definition is followed by a semicolon rather than a regular method block. Derived classes of an abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, an abstract class can override the virtual method with an abstract method.

For example:

// compile with: /target:library
public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}

public abstract class E : D
{
    public abstract override void DoWork(int i);
}

public class F : E
{
    public override void DoWork(int i)
    {
        // New implementation.
    }
}

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.