Abstract class. Basic concepts. Keyword abstract. Examples
Contents
- 1. The concept of an abstract class. The need to use an abstract class. Example
- 2. The general form for declaring an abstract class. Keyword abstract
- 3. What class elements can be abstract? Example
- 4. What is an abstract method? The general form of an abstract method declaration
- 5. The general form of an abstract property declaration
- 6. The general form of an abstract indexer declaration
- 7. The general form of declaring an abstract event
- 8. What restrictions are imposed on abstract classes and their instances?
- 9. Is it possible to declare non-abstract elements in an abstract class?
- 10. Can constructors be used in an abstract class?
- 11. What restrictions are imposed on abstract elements of a class (methods, properties)?
- 12. Can an abstract class not contain declarations of abstract elements?
- 13. Figure showing the use of an abstract class
- 14. Can an abstract class inherit from another abstract class? Can an abstract class inherit from another non-abstract class?
- 15. Differences between abstract classes and interfaces
- Related topics
Search other websites:
1. The concept of an abstract class. The need to use an abstract class. Example
There are times when you need to create the most general form of the base class, which will be filled with details in derived (inherited) classes. In this case, the elements of the base class (methods, properties, etc.) are of a very vague nature and it makes no sense to implement them. Classes containing elements without a concrete implementation are called abstract. In the abstract class, only the general character of the element is declared, which will be filled with content in the inherited classes.
An abstract class is a class in which at least one abstract element (method, property) is declared. If an abstract element, (method, property) is declared in an abstract class, then the keyword abstract is placed in front of the name of such a class. If in a derived class it is necessary to determine the concrete implementation of an element (method, property) of an abstract class, then the override keyword is indicated when declaring an element.
An abstract class provides for its use as a base for other inherited classes. In turn, the inherited class must implement all the abstract elements of the base abstract class. If, for some reason, it is not necessary to implement elements of an abstract class in an inherited class, then such a class also needs to be declared as abstract.
For example. Let it be necessary to develop the base class Figure, which describes a certain figure. More specific implementations of shapes should be inherited from this class: Triangle, Circle, Rectangle as shown in Figure 1.
Let the Area() method, which determines the area of the figure, be introduced into the class hierarchy. In the base class Figure and derived classes, the Area() method uses the mechanism of polymorphism when it is called.
The implementation of the Area() method in derived classes (Triangle, Circle, Rectangle) is understandable. For any of these derived classes, there is a way to determine the area. However, it is impossible to determine the area for the Area() method of the Figure base class, because at this stage it is not yet known for which figure the area needs to be calculated. Therefore, the implementation (body) of the Area() method does not make sense. It is advisable to declare such a method as abstract with the keyword abstract. Automatically, the Figure class also becomes abstract.
Figure 1. The need to use abstract classes. The Area() method in the Figure class is abstract
⇑
2. The general form for declaring an abstract class. Keyword abstract
To declare an abstract class you need to use the abstract keyword. In the simplest case, the general declaration form of an abstract class named ClassName is as follows
abstract class ClassName { // Abstract and non-abstract elements // ... }
According to C# syntax, the public or internal access modifier can be used before the abstract keyword.
⇑
3. What class elements can be abstract? Example
The abstract keyword can be declared:
- methods;
- properties;
- indexers;
- events.
You cannot declare an abstract field (variable) of a class.
Example. The example demonstrates the declaration of various abstract elements of a class.
... // Declare a delegate type delegate int MyDelegate(int a, int b); abstract class A { // Abstract method public abstract void Print(); // Abstract property public abstract int IntProp { get; set; } // Abstract indexer public abstract int this[int index] { get; set; } // Abstract event public abstract event MyDelegate MyEvent; } ...
⇑
4. What is an abstract method? The general form of an abstract method declaration
An abstract method is a method in which there is no implementation that is specified in braces. An abstract method does not contain a method body. For an abstract method, only its signature is specified: the return type, name, and list of parameters.
The general form of an abstract method declaration:
access_modifier abstract return_type MethodName(parameters);
here
- MethodName – the name of abstract method;
- access_modifier – one of the access modifiers protected, public, internal, protected internal;
- return_type – returned type;
- parameters – list of method parameters.
⇑
5. The general form of an abstract property declaration
The general form of an abstract property declaration
access_modifier abstract return_type PropName { get; set; }
here
- PropName – property name;
- access_modifier – one of the access modifiers except the private modifier;
- return_type – property type.
As with a regular property, get or set access specifiers may be missing.
⇑
6. The general form of an abstract indexer declaration
C# allows the use of abstract indexers. The general declaration form of an abstract indexer is as follows:
access_modifier abstract return_type this[type_index index] { get; set; }
here
- access_modifier – one of the access modifiers except the private modifier;
- return_type – indexer type;
- type_index – the type of index by which the value is obtained by the indexer;
- index – the name of the index variable.
An abstract indexer must contain at least one of the get or set qualifiers.
⇑
7. The general form of declaring an abstract event
To declare an abstract event, the delegate type for this event must first be declared. The general form for declaring an abstract event is as follows:
access_modifier abstract event Delegate_Type eventName;
where
- access_modifier – one of the access modifiers: public, protected, internal or protected_internal;
- eventName – name of the abstract event;
- Delegate_Type – the type of delegate.
The delegate type Delegate_Type is declared as
delegate return_type Delegate_Type(parameters);
here
- Delegate_Type – delegate type name;
- return_type – the type returned by the delegate;
- parameters – delegate parameters.
⇑
8. What restrictions are imposed on abstract classes and their instances?
The following restrictions apply to abstract classes:
- abstract class cannot be static;
- an abstract class cannot be sealed (with the sealed keyword);
- it is impossible to create an instance (object) of the abstract class.
⇑
9. Is it possible to declare non-abstract elements in an abstract class?
Yes, it is. In an abstract class, you can declare any non-abstract elements that are usually declared in classes (data fields, methods, properties, indexers, etc.).
⇑
10. Can constructors be used in an abstract class?
In an abstract class, the use of constructors is allowed. Although an abstract class instance cannot be created. Constructors in an abstract class are needed to implement the initialization of the internal fields of an abstract class (if any). These constructors are invoked using the base keyword in derived classes. More details about the use of the base keyword in the constructors of derived classes are described here.
⇑
11. What restrictions are imposed on abstract elements of a class (methods, properties)?
Abstract class elements are elements that are declared with the abstract keyword. The following restrictions are imposed on abstract elements (methods, properties, indexers, events):
- an abstract element cannot be private. It is allowed to declare an abstract method with any other access modifier: public, protected, internal, protected internal;
- an abstract element must not have an implementation;
- an abstract element (an element designated as abstract) cannot be indicated by the virtual keyword. This is natural because the abstract keyword is sufficient to ensure the use of polymorphism.
⇑
12. Can an abstract class not contain declarations of abstract elements?
Yes. The C# compiler allows the declaration of a class with the keyword abstract in which there are no abstract elements. Such a property is useful when, at the initial stage of constructing a program, the list of all elements of the class located at the top of the hierarchy is not yet fully known. However, it is already clear that with further development of the program, this class will contain abstract elements, and, therefore, should be abstract.
For example. At the beginning of the development of the class hierarchy, an abstract stub class is introduced. This example declares a class named BaseAbstractClass
// The base class in the hierarchy // does not yet contain abstract elements abstract class BaseAbstractClass { // Here in the future abstract elements may be added }
⇑
13. Figure showing the use of an abstract class
Figure 2. Use of abstract class
⇑
14. Can an abstract class inherit from another abstract class? Can an abstract class inherit from another non-abstract class?
Yes. An abstract class can inherit both abstract and non-abstract classes. In this case, the inheritance rules are the same as for inheriting non-abstract classes. Throughout the hierarchy chain, abstract and non-abstract classes can successively inherit from each other.
Example. The following example demonstrates the freedom to use abstract classes in combination with non-abstract ones.
namespace ConsoleApp5 { // Abstract class A_Abstract - top of the hierarchy abstract class A_Abstract { // some internal class field public int a; } // Non-abstract class B - derived from A_Abstract class B : A_Abstract { public int b; } // Abstract class C_Abstract - derived from non-abstract class B abstract class C_Abstract : B { public int c; } // Non-abstract class D class D : C_Abstract { public int d; } class Program { static void Main(string[] args) { // 1. Declare instances of classes B, D. // Instances of classes A_Abstract, C_Abstract // are not allowed to be declared. B objB = new B(); D objD = new D(); // A_Abstract obj = new A_Abstract(); - error // 2. Access to the elements of the instance objB objB.a = 23; objB.b = 33; // obj.d = 40; - error, cannot access element of inherited class // 3. Access to the elements of the instance objD objD.a = 77; objD.b = 777; objD.c = 7777; objD.d = 7777; } } }
⇑
15. Differences between abstract classes and interfaces
The following differences were noticed between interfaces and abstract classes:
- implementations of methods, properties, etc., cannot be declared in an interface. In an abstract class, it is allowed to declare implementations of its elements;
- an interface cannot have constructors. An abstract class can have constructors;
- the interface cannot contain data fields. An abstract class allows the use of internal data fields;
- default interface elements (without an access modifier) are considered public. In abstract classes, elements are considered private by default;
- interface elements must not contain an access modifier (otherwise, the compiler will generate the error message). In abstract classes, any access modifier is allowed in the declaration of a class element;
- a derived class can inherit only one abstract base class. If you use interfaces, a derived class can inherit any number of interfaces. Thus, an interface is an alternative to an abstract class with which you can implement multiple inheritance.
⇑
Related topics
- An example of using an abstract class that contains abstract properties and methods
- An example of using an abstract class to build class hierarchies
⇑