Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Abstract Keyword

This C# tutorial covers abstract methods and classes. Abstract things cannot have implementations or be instantiated.

Abstract. An abstract method has no implementation.

Its implementation logic is provided instead by classes that derive from it. We use an abstract class to create a base template for derived classes.

Example. We introduce first an abstract class named Test. Two other classes derive from Test: the Example1 and Example2 classes. In the Test class, we have a field, and an abstract method.

Methods: Abstract methods cannot have bodies. This makes sense: these bodies would never be used.

Classes: Abstract classes have certain restrictions. They cannot be constructed directly.

Tip: These are features. They enforce a design concept. An abstract type is, by definition, not a concrete one.

C# program that uses abstract class

using System;

abstract class Test
    public int _a;
    public abstract void A();

class Example1 : Test
    public override void A()

class Example2 : Test
    public override void A()

class Program
    static void Main()
	// Reference Example1 through Test type.
	Test test1 = new Example1();

	// Reference Example2 through Test type.
	Test test2 = new Example2();



Derived classes. When you create a derived class like Example1 or Example2, you must provide an override method for all abstract methods in the abstract class. The A() method in both derived classes satisfies this requirement.


Int field. An abstract class can have an instance field in it. The derived classes can access this field through the base syntax. This is a key difference between abstract classes and interfaces.


Cannot instantiate abstract class. The important part of an abstract class is that you can never use it separately from a derived class. Therefore in Main you cannot use the new Test() constructor.

However: You can use the Test type directly once you have assigned it to a derived type such as Example1 or Example2.

Discussion. What is the difference between an abstract class and an interface? An abstract class can have fields on it. These fields can be referenced through the derived classes. An interface, on the other hand, cannot have fields.


Thus: An abstract class is essentially the same thing as an interface except it is an actual class, not just a contract.

A static class cannot be abstract. Static classes cannot be instantiated or derived from. This makes the abstract keyword have no meaning for them. Abstract classes have effect only when used with inheritance.

Static: A static class is a class with additional restrictions placed upon it. With "static" on a class, we take away features.

Static Class

Performance. In my testing, abstract classes with virtual methods have better performance than interface implementation in the .NET Framework 4.0. We show that performance remains the same with an abstract class instead of a regular class.

Interface Performance

In my view, programs that use abstract classes tend to be better-designed. Less code duplication occurs in them. With less code, we tend to have higher-quality and better-tested code. This leads to faster and more reliable programs.

So: In solving the problem of code duplication among many object types, abstract classes help improve code quality and performance.

Summary. An abstract class is one that cannot be directly instantiated. Instead, derived classes must inherit from it—their constructors must be used. Compared to an interface, it adds features and improves performance.

Review: Abstract classes use the abstract keyword. Abstract methods use a special form. The method body is replaced by a semicolon.

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf