37

This question is out of curiosity. Is there a difference between:

public abstract class MyClass
{
    public MyClass()
    {
    }
}

and

public abstract class MyClass
{
    protected MyClass()
    {
    }
}

Thanks.

Marlon
  • 19,924
  • 12
  • 70
  • 101

3 Answers3

32

They are the same for all practical purposes.

But since you asked for differences, one difference I can think of is if you are searching for the class's constructor using reflection, then the BindingFlags that match will be different.

BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
var constructor = typeof(MyClass).GetConstructor(flags, null, new Type[0], null);

This will find the constructor in one case, but not the other.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
16

You shouldn't have a public constructor in an Abstract class Constructors on abstract types can only be called by derived types. Because public constructors create instances of a type, and you cannot create instances of an abstract type, an abstract type with a public constructor is incorrectly designed.

have a look here for details http://msdn.microsoft.com/en-us/library/ms182126.aspx

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • 1
    @Shekhar: please don't post MSDN links to old versions. Readers clicking links within that document will be led to more old versions. – John Saunders Dec 26 '10 at 00:08
  • @John oh thanx for editing my post. I forgot to look for the version on the page.... :P – Shekhar_Pro Dec 26 '10 at 00:15
  • This is wrong. You can have public constructors, but as far as usage, there is no difference between a public and protected constructor in abstract classes. – jakobbotsch Dec 26 '10 at 00:51
  • This answer is wrong. You can have public constructors on abstract types. You can have internal, protected, protected internal or private constructors. You just can't call them directly on the abstract type - you can only call them from derived non-abstract types. And all constructors, not matter the accessor, will create instances, not just public constructors. And you must define public constructors on abstract classes if you wish derived classes to be publicly created so it is a valid design to do so. – Enigmativity Dec 26 '10 at 01:04
  • 5
    "is incorrectly designed" is quite different from "can't have". – Ben Voigt Dec 26 '10 at 01:07
  • @Enigmativity you can have but its not a good design tahts what link says – Shekhar_Pro Dec 26 '10 at 01:09
  • @Shekhar_Pro - I did read the article and I think its opinion is very subjective. I would argue that to refactor code I should be able to make as few changes as possible, and since there is little difference in practical terms between public and protected constructors then abiding by this "good design" makes refactoring more difficult. Unless there's a substantive reason behind the design it seems rather flimsy. – Enigmativity Dec 28 '10 at 22:47
  • 1
    This is a very late post, but if you use any IOC or DI style approaches, having a public constructor in the abstract class *does* make sense in certain cases, where you want to minimize code duplication. – code4life Apr 04 '13 at 20:57
  • 2
    @code4life - It would be awesome if you could give an example or post a link to an example. Not ripping on your comment by any means but I am interested by what you mean. – Alex Jorgenson Sep 04 '13 at 02:33
2

In terms of future use of this code, there is no difference.

Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111