Is it possible to inherit a class that has only private constructor?

Showing Answers 1 - 38 of 38 Answers

Krishna Bayanna

  • Jan 24th, 2006
 

No.

ex.

Base Class:

/// Summary description for BaseA.

/// </summary>

public class BaseA

{

private BaseA()

{

//

// TODO: Add constructor logic here

//

}

}

Dervied Class:

/// <summary>

/// Summary description for Class1.

/// </summary>

public class DerviedB: BaseA

{

public DerviedB()

{

//

// TODO: Add constructor logic here

//

}

}

Result: DerviedB.cs(10): 'PrivateAssembly.BaseA.BaseA()' is inaccessible due to its protection level

  Was this answer useful?  Yes

samiksc

  • Feb 1st, 2006
 

friend concept is not there in C#. The maximum you can have is an 'internal' scope.

It is not possible to inherit from a class which has only a private constructor. It is possible to instantiate a class with a private constructor provided it has a static member function which creates an object of the class. However such a class is generally used, neither for instantiating nor for inheriting, but for storing logically related static data members and static methods.

  Was this answer useful?  Yes

Naresh

  • Apr 1st, 2006
 

you can not inherit the class with private constructor. for example the following code would generate the error.

using System;

namespace ClassLibrary1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

public class Class1

{

private Class1()

{

//

// TODO: Add constructor logic here

//

}

}

public class class2:Class1

{

}

}

  Was this answer useful?  Yes

Dharmendra

  • Feb 7th, 2007
 

Yes, it is possible to inherit a class that has private constructor. For example You can define private constructor in Abstract Class and can Inherit it.

  Was this answer useful?  Yes

samiksc

  • Feb 7th, 2007
 

It is NOT possible to inherit an abstract class with a private constructor as mentioned in the last comment above:This code results into compilation error "ClassLibrary1.Class1.Class1() is inaccessible due to its protection level"namespace ClassLibrary1{ ///

/// Summary description for Class1. /// public abstract class Class1 { private Class1() { // // TODO: Add constructor logic here // } } public class Class2:Class1 { public Class2(){} }}

  Was this answer useful?  Yes

pankajbanga

  • Feb 25th, 2007
 

When you instantiate a derived class, constructor of the base class is called before constructor of the derived class. If base class constructor is private it is not accessible from derived class and an error is thrown.

  Was this answer useful?  Yes

pritam83

  • Feb 4th, 2008
 

I have tried the above with including static method in the base class. But it giving me the 'inaccessible due to protection level' error. And one more thing some members told that "internal scope" method. Can any body give me the idea....or better give some code snippet.

thanks,
Pritam

  Was this answer useful?  Yes

nil_kudale

  • Dec 16th, 2009
 

It gives Compile Time Error, because when you trying to compile that time compiler check is base class constructor is accessible from derive, to resolve error you have to change the access modifier.

Let take example 

using System;
namespace abc
{
 public class base1 {
    private base1() { }
 }
 public class derive : base1 {
    public derive () { }
 }
}

output is :
E:Program FilesMicrosoft Visual Studio 9.0VC>csc  /out:testabs /target:library testabs.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
testabs.cs(9,12): error CS0122: 'abc.base1.base1()' is inaccessible due to its protection level
testabs.cs(6,13): (Location of symbol related to previous error)

  Was this answer useful?  Yes

Naveen Kumar Shivanadri

  • Sep 8th, 2016
 

The direct answer for this question is "No".
Before discussing this topic, there is necessary to know one thing
All the variables are initialized with the help constructor. It means, if the variable is initialized with some value, those variables can not
Modified. But if not initialize, those variables are initialized with default values by the time of class instance is created.
In the inheritance, while creating a child class instance, the compiler will call the child class constructor to initialize the variables of the child class. But a child class constructor before initialize the child class variable initialization, it will parent class constructor internally because the parent class variables should be initialized before child class variable initialization.
Without a parent class variable initialization, a child class constructor cannot initialize the child class variable initialization.
So, its the parent class constructor defined as private modifier, child class constructor cannot access the parent class constructor for parent class variable initialization. That is the reason parent class constructor should not be private.
Naveen Kumar Shivanadri

  Was this answer useful?  Yes

ASHISH KUMAR

  • Jul 23rd, 2017
 

Yes it is possible only in one case:
If derived class is child class of base class (which have private constructor).

  Was this answer useful?  Yes

Vikas Kumar Bhatnagar

  • Aug 14th, 2018
 

We cant inherit the abstract class and when we declared a class as abstract and define the private constructor .
Following compilation error:
Cannot create an instance of the abstract class or interface Singleton.
Singleton.Singleton() is inaccessible due to its protection level

  Was this answer useful?  Yes

Sujit

  • Feb 18th, 2019
 

Nope. We cant inherit a class if it has Private constructor.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions