Home  /  Questions  /  Question



129   96.7
Mar 15, 2011


Why? C# abstract classes without abstract members

I am familiar with abstract classes, interfaces, and the like.

What is not clear to me is why anyone would create a C# abstract class that does not have abstract members?
(the C# compiler allows this).

Example:

public abstract class House
{
    public void OpenDoor()
    [
        Console.WriteLine("Door opens");
    }
}

 



50   50
Mar 16, 2011
public abstract class House
{
    public void OpenDoor()
    {
        Console.WriteLine("Door opens");
    }
}

It will not show complie time error. you can use this class as base class of other class

1,364   100.0
Mar 16, 2011
One of the primary benefit of this is to provide default implementation.  This can be useful in couple of scenarios.  Also, have a look at the Template Method pattern for a simple application of this.

http://www.dofactory.com/Patterns/PatternTemplate.aspx

50   50
Mar 16, 2011
In some scenario, you might want to provide a default implementation for your class or if you don't want your derived classes will define their own implementation, then the member will not be declared as abstract.