C # abstract classes and abstract methods,

Source: Internet
Author: User

C # abstract classes and abstract methods,

Overview: abstract keywords abstract classes cannot be instantiated, abstract methods cannot have method bodies, and all abstract methods in abstract classes must be rewritten in subclass (Override). An abstract class can contain both abstract and non-abstract methods.

Abstract Modifiers can be used with classes, methods, attributes, indexers, and events.

Use in class declarationAbstractModifier to indicate that the class can only be the base class of other classes.

Abstract classes have the following features:

  • Abstract classes cannot be instantiated.
  • Abstract classes can contain abstract methods and abstract accessors.
  • The abstract class cannot be modified with the sealed modifier, which means the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.

Used in method or attribute DeclarationAbstractModifier to indicate that this method or attribute does not contain an implementation.

Abstract methods have the following features:

  • The abstract method is an implicit virtual method.
  • Only abstract methods can be declared in abstract classes.
  • Because the abstract method declaration does not provide real implementation, there is no method body. The method declaration ends with a semicolon and there is no braces ({}) after the signature ({}). For example:
    public abstract void MyMethod();
  • The implementation is provided by the overriding method, which is a non-abstract class member.
  • Used in abstract method declarationStaticOrVirtualThe modifier is incorrect.

Except for the differences in the declaration and call syntax, abstract attributes behave the same way as abstract methods.

  • Use static attributesAbstractThe modifier is incorrect.
  • In a derived classOverrideThe modifier's attribute declaration can override abstract inherited attributes.

Abstract classes must be implemented for all interface members.

In MSDN, the C # language standard is: 10.1.1.1 abstract class. This article is as follows:

abstractThe modifier is used to indicate that the modified class is incomplete and can only be used as the base class. Abstract classes and non-abstract classes are different in the following aspects:

  • Abstract classes cannot be directly instantiated and used for abstract classesnewThe operator is a compilation error. Although some types of variables and values during compilation can be abstract, such variables and values must ornullOr include references to instances that are not abstract classes (this non-abstract class is derived from the abstract class ).
  • Allowed (but not required) abstract classes include abstract members.
  • Abstract classes cannot be sealed.

When a non-abstract class is derived from an abstract class, these non-abstract classes must specifically implement all the abstract members inherited to override those abstract members. In the following example

abstract class A { public abstract void F(); } abstract class B: A { public void G() {} } class C: B{public override void F() {// actual implementation of F}}

Abstract classAIntroduce Abstract METHODSF. ClassBIntroduce another methodGBut since it does not provideFImplementation,BIt must also be declared as an abstract class. ClassCRewriteFAnd provides a specific implementation. BecauseCNo abstract member in, so you can (but not required)CThe Declaration is not an abstract class.

So when should we use abstract classes?
If the object of a class design is inherited by other classes and it represents the public attributes or methods of A Class Object, the class should be set as an abstract class.

 

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.