Alex Rivera | Logout

What's the difference between an abstract class, and a class with only protected constructors? (.NET)

Asked 2010-02-05T19:41:33.280
9

What are all the difference between an abstract class, and a class with only protected constructor(s)? They seem to be pretty similar to me, in that you can't instantiate either one.

EDIT:

How would you create an instance in a derived class, with a base class with a protected constructor? For instance:

public class ProtectedConstructor
{
    protected ProtectedConstructor()
    {

    }

    public static ProtectedConstructor GetInstance()
    {
        return new ProtectedConstructor(); // this is fine
    }
}

public class DerivedClass : ProtectedConstructor
{

    public void createInstance()
    {
        ProtectedConstructor p = new ProtectedConstructor(); // doesn't compile
    }

    public static ProtectedConstructor getInstance()
    {
        return new ProtectedConstructor(); // doesn't compile

    }
}
Edit
Report

1 Answer

0

Well, the first difference that comes to mind is that an abstract class can not be instantiated, but a class with protected constructors could be instantiated throw another public method.

A common example of this might be something like the Singleton pattern: http://en.wikipedia.org/wiki/Singleton_pattern

answered 2010-02-05T19:48:25.723

Your Answer