All about abstract classes in C#

 Sep 19, 2014

In today’s article, I will review one of the most exciting object-oriented programming (OOP) features of C#, namely the concept of abstract classes. An abstract class is a class that cannot be instantiated. In C#, we declare a class as abstract by using the abstract keyword as a modifier in the class declaration.
public abstract class MyAbstractClass { }
The normal way to use a class is by having a variable that holds an instance of the class and then call the methods and/or read and write to the properties of the class.
MyClass class1 = new MyClass(); class1.Method1(); class1.Prop1 = 123;
For the code above to be correct, we must be sure that MyClass is not abstract. So if we try using MyAbstractClass instead of MyClass we would get a compiler error. But if we can’t have an instance of MyAbstractClass, how do I use it? The answer is quite simple: you must declare another class that inherits from MyAbstractClass which is not also declared as abstract. So, for example, the MyClass above could have been declared as:
public class MyClass : MyAbstractClass { }
So you can see that the abstract keyword gives you a way to enforce the right use of the classes in a complex inheritance chain. Everyone would agree that inheritance is by far the most important concept in OOP, so a keyword that allows you to decide if a particular class can only be used as a base class to derive other classes, and not be consumed directly by a client is definitely an important concept and should be well known by any developer. Let’s dig deeper into this important concept. All methods and properties that were defined as public in the abstract class MyAbstractClass are visible to the instances of MyClass by inheritance. So in a real world scenario we could have, for example, a base class Person as the top level class in an object model containing specific classes for different types of persons you need to deal with in your system: CustomerSalesPersonTeamLeaderManager, etc. You can instantiate any of the specific classes but you can’t have an instance of Person created directly with the new operator.
Customer cust1 = new Customer(); //FirstName and LastName were declare in Person class cust1.FirstName = “John”; cust1.LastName = “Smith”; //this method is specifically declared in Customer cust1.CreditRating = “A”;
Our inheritance hierarchy could have more levels. For example, we could have a class Employee that inherits from Person which is also declared as abstract, and then use it as the base for the more specific types of employees. The abstract class Employee would add some additional members to the Person class, but it would still require some derived class in order to be of any use. If we need to force that the derivation process will necessarily stop at some point in a hierarchy we declare the last class as sealed. Note that sealed cannot be combined with abstract, because one is saying that you can’t derive (sealed) and the other is saying that you have to derive before you create instances (abstract). If a class is abstract it can have members declared as abstract. An abstract property or method must be implemented by a derived class.
public abstract class MyAbstractClass { public abstract void  Method1(); //only a declaration public void Method2() { /* implementation provided here */ } public virtual void Method3() { /* can be overridden */ } }
Note that you can mix methods together that are declared as abstract and ordinary methods, virtual or not. When you declare a method as abstract, you cannot provide an implementation in the abstract base class itself. It must be implemented by the derived class.
public class MyClass : MyAbstractClass { public override void Method1() { } public override void Method3() { } }
Note that we use the keyword ‘override’ in the derived class for both abstract and virtual methods from the base class. Method3 was declared virtual, so it must have a body in the base, maybe an empty one {}, and it can optionally be overridden in the derived class. The abstract Method1, on the other hand, cannot have a body in the base, not even an empty one, and it must be overridden in the derived class. So if we leave out the Method1 declaration in MyClass, the code would not compile. The keyword ‘abstract’ for method declarations requires that the containing class is declared as abstract as well. If the concepts of abstract classes excites you or you want to learn more about OOP in C#, I recommend you take a look at New Horizons’ 20483: Programming in C# training program.

How do your Excel skills stack up?   

Test Now  

About the Author:

Newton Godoy  

With over 17 years of in-class training experience and over 16 years of industry experience, Newton offers students a wealth of real-world technical knowledge and expertise in the areas of .NET application development, SQL Server and SharePoint Server. After spending several years lecturing as a professor, Newton found his true calling and began his career as a MCT. He worked as a technical trainer for some of Brazil’s and Australia’s largest corporate training organisations before finally finding a home with New Horizons where he is now one of our top trainers. Newton brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. His combination of technical knowledge and instructor experience make him one of the most respected instructors within the IT training industry.

Read full bio
top