Home > Code Analysis > CodeRush code issues for abstract members

CodeRush code issues for abstract members

August 19th, 2011

In addition to CodeRush code issues for virtual members, there are similar code issues for abstract members. An abstract member is similar to a virtual member but with no implementation, in other words, it has no body. In contrast to virtual members, abstract members must be implemented in a derived class if one is inherited from the class that contains an abstract member.

An abstract member should be marked with an ‘abstract‘ keyword and declare an access modifier, return type, and member signature. A non-abstract derived class is required to provide its own implementation by overriding the abstract members from the base class.

Here are code issues that help you declare abstract members correctly without violating their declaration rules:

Abstract member cannot be declared in non-abstract class

Cause:

The class that holds an abstract member must be marked as abstract as well. If a class containing an abstract member is not marked as abstract, you will see the corresponding code issue highlighting of an error type.

Sample:

CodeRush Code Issues - Abstract member cannot be private

How to fix:

  • Mark the class as abstract:

CodeRush Abstract Member Cannot Declare A Body Fix 1

  • Do not mark the member as abstract:

CodeRush Abstract Member Cannot Declare A Body Fix 2

Abstract member cannot be private

Cause:

The visibility of an abstract member must be greater than ‘private‘, because it must be visible to derived classes. An abstract member with a ‘private‘ access modifier is highlighted with an error in the code editor.

Sample:

CodeRush Code Issues - Abstract member cannot be private

How to fix:

  • Change the visibility of the abstract member:

CodeRush Abstract Member Cannot Be Private Fix

Abstract member cannot be marked as sealed

Cause:

It is not allowed to declare an abstract member inside a class marked as sealed. Sealed classes cannot be inherited, so they cannot contain abstract members.

Sample:

CodeRush Code Issues - Abstract member cannot be marked as sealed

How to fix:

  • Do not mark an abstract member as ‘sealed’:

CodeRush Abstract Member Cannot Be Marked As Sealed Fix

Abstract member cannot be marked as virtual

Cause:

An abstract member is not allowed to be marked as ‘virtual‘ and ‘abstract‘ at the same time – this is a violation of the language specification. Once the member is marked as an ‘abstract‘ and ‘virtual‘ is found, the corresponding error is shown.

Sample:

CodeRush Code Issues - Abstract member cannot be marked as virtual

How to fix:

  • Do not mark an abstract member as ‘virtual’:

CodeRush Abstract Member Cannot Be Marked As Virtual Fix

Abstract member cannot declare a body

Cause:

By its nature, an abstract member should have no body. If you’ve started to code an implementation of an abstract member, you will see an error.

Sample:

CodeRush Code Issues - Abstract member cannot declare a body

How to fix:

  • Remove the body of an abstract member:

CodeRush Abstract Member Cannot Be Declared In Non Abstract Class Fix 1

  • Do not mark the member as ‘abstract’:

CodeRush Abstract Member Cannot Be Declared In Non Abstract Class Fix 2

Also, take a look at the code issues for overridden members, which override abstract members to learn more.

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/19/2012
ID: C123

Similar Posts: