Abstract Classes In Depth

Introduction

One of the meanings of the abstract word is “exists in thought as an idea but does not have a physical or concrete existence". In .Net, an abstract class is a special type of class that acts as a base class and cannot be instantiated. Class members (methods, properties, indexers and events) are marked as abstract; it must be implemented by a derived class.

Example

  1. public abstract class Test  
  2. {  
  3.     public abstract string GetName();  
  4.     public string GetOtherName()  
  5.     {  
  6.         return "Other";  
  7.     }  
  8. }  
An abstract class has the following features:

 

  • It cannot be instantiated.

  • It contains abstract as well as non-abstract members.

  • We cannot mark an abstract class as a sealed class or static class.

    abstract class

  • A derived class must include an actual implementation of all the inherited abstract class members.

  • An abstract class can be inherited from an abstract as well as non-abstract class and one or more interfaces.

    Important notes for an abstract class that is inherited from an interface:

    • An abstract class must provide an implementation of all interface members.

    • An interface method might map with any abstract method of the abstract class.
      1. interface ITest  
      2. {  
      3.     string GetName();  
      4. }  
      5. public abstract class Test : ITest  
      6. {  
      7.     public abstract string GetName();  
      8. }  
  • An abstract class may have private, internal, static non abstract-members.
    1. public abstract class Test  
    2. {  
    3.     public static int variables = 0;  
    4. }  
    5.   
    6. public class Test1   
    7. {  
    8.     public string GetNames()  
    9.     {  
    10.         var p = Test.variables;  
    11.         return "";  
    12.     }  
    13. }  

Feature of abstract members

Abstract methods

  • An abstract method is implicitly a virtual method.

  • A non-abstract class does not have an abstract method. It means an abstract method can only be declared with an abstract class.

    abstract method

  • An abstract method cannot have a private access modifier. In short, an abstract method is always public.

    modifier

  • An abstract method cannot be declared as virtual or static.

    virtual

  • The access modifier of the abstract method should be the same in both of the abstract class as well as its derived class.

Abstract properties

  • Abstract properties are just like an abstract method, the only difference is in the declaration and invocation syntax.
    1. public abstract class Test  
    2. {  
    3.    public abstract string Name { getset; }  
    4. }  
  • An abstract property cannot be declared as virtual or static.

    Abstract property

When to use an abstract Class

  • An abstract class is a class that must be inherited from and it cannot be instantiated. It can be fully or partially implemented or not implemented at all, so that it is encapsulating common functionality for inherited classes.

  • An abstract class provides an easy and simple way to version our components, so it is very useful in multiple versions of our component.

  • If we are planning to create a large functional unit, it is recommended to use an abstract class.

  • Abstract classes allow us to partially implement our classes; an abstract class is very useful for providing common implemented functionality among all implementations of our component.

  • It is recommended to use an abstract class when creating a class library that will be widely distributed or reused.

Design guidelines for Abstract Class

  • Do not define public constructors because the visibility of the public or protected internal is for a type that can be instantiated. An abstract class can never be instantiated.

  • Use a protected or internal constructor in an abstract class if required. A protected constructor does the initialization tasks when instances of a derived class are created. An internal constructor prevents the abstract class from being used as a base class when it is not in the same assembly.

  • Provide at least one abstract member in an abstract class.

    Example for good design
    1. public abstract class Test  
    2. {  
    3.     protected Test()  
    4.     {  
    5.   
    6.     }  
    7.     public abstract string GetName();  
    8. }  
Summary

This was all about abstract classes.

Up Next
    Ebook Download
    View all
    Learn
    View all