Back to list
Views:   13.6K
Replies:  3
Archived

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");
    }
}

 
Juan Perez, Mar 15, 2011
Reply 1
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.
Vivekananda P, Mar 16, 2011
Reply 2
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
Rajesh Pillai, Mar 16, 2011
Reply 3
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
Kamal Gupta, Mar 16, 2011


Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas.      Vsn 1.2.2