Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, July 25, 2016 7:28 AM
Hi, Can anyone explain use of private constructor in an Abstract class,
public abstract class A
{
private A()
{
}
public abstract void Add();
}
Latheesh K Contact No:+91-9747369936
All replies (7)
Monday, July 25, 2016 8:04 AM ✅Answered
It prevents that you or someone else can derive classes from that class on the outer scope. It allows similar behavior: You can create nested subclasses of that type (inner scope), but no one else.
E.g.
namespace ConsoleCS
{
using System;
public class Program
{
public static void Main(string[] args)
{
BaseClass.DerivedClass dc = new BaseClass.DerivedClass();
Console.WriteLine("\nDone.");
Console.ReadLine();
}
public abstract class BaseClass
{
private BaseClass()
{
Console.WriteLine("base ctor");
}
public class DerivedClass : BaseClass
{
public DerivedClass()
{
Console.WriteLine("derived ctor");
}
}
}
}
}
p.s. using sealed is imho the better way to do that.
Monday, July 25, 2016 8:55 AM ✅Answered
There's no difference on the private constructor part. Just use private constructor when you don't want the class to be inherited from other classes outside.
If you see it in production code, check your source control history to see if there is a previous version that is abstract class only. Maybe they only add it to fix a hole that "allows other class inherit from it to instantiate it". The abstract part leaving behind there does not matter.
Monday, July 25, 2016 7:49 AM
It prevents "class inheritance from A" because private constructor of A is inaccessible, effectively function like the "sealed" keyword for non-abstract classes. ("abstract" and "sealed" keywords cannot not coexist)
Monday, July 25, 2016 7:56 AM
Then what is the actual use of it.In normal class with private constructor we can useful in singleton design pattern.Can u explain a scenario.
Latheesh K Contact No:+91-9747369936
Monday, July 25, 2016 8:45 AM
Thanks Stefan Hoffmann
Latheesh K Contact No:+91-9747369936
Thursday, August 4, 2016 1:57 AM
Oh, so I have it wrong. I'd love to hear that.
It's always a good thing to have chance to correct the idea before I would mis-apply it to a project.
Btw, where is the link you're refering to?
Friday, June 9, 2017 6:48 AM
"Why are you allowed to use a special credential?"
If you mean "answer" or "editor", simple, I got invited to help the moderators. If you stay here for long enough and don't make much trouble, maybe you'll be asked to participate too. (That said, I think my lot is the only time so far they're recruiting people for this responsibility. I really don't have idea on whether they'll recruit again in the future. You see, it's the same identity but displayed differently on the forum, and they still not fixed it yet. It seems the chance they'll do it again is slim to me)
Btw, this "special credential" just give you the power to mark replies on other's thread as answer, and it's subject to the same rule that "you don't get points for marking your own reply as answer" like the moderators. Just consider it as "voluntary work" to help moderators tidy up forums.
And unlike MCC, it won't give you any credit on your way towards MCC/MVP programs too. If you plan to after that, it'd be better to just apply for MCC.
If you mean "Partner", just try get employed by one of the "Microsoft Certified Partners" companies, and register your MCP ID to the company's profile and you'll get that.