public class MyClass{private MyClass(){}}class Program:MyClass{static void Main(string[] args){Console.WriteLine("Hello");Console.ReadLine();}}
Interviews6 answers
What will be output of below code?
6 Answers
Constructor -> private MyClass() cannot be inherited Program:MyClass
Bhuvanesh MohankumarPosted May 30, 2016, 8:47 AM EDT
Myclass is not accessible because of protection level.. when class has private constructor it can not inheritable.. if you want to inherit Myclass then you must need to add one public constructor with parameter then you can create object of MyClass.
Shivanand BastePosted Mar 22, 2016, 10:53 PM EDT
Error Because A class having private constructor cannot be inherited or instantiated.
Pankaj Kumar ChoudharyPosted Feb 21, 2016, 2:50 PM EST
Error I because ClassWithPvtConstructor cannot be declare as private
Ashish Kumar JaiswalPosted Jan 16, 2016, 1:55 AM EST
it can not be inherited however, we can instantiate it using parameterized constructor.
class Program
{
static void Main(string[] args)
{
ClassWithPvtConstructor classWithPvtConstructor = new ClassWithPvtConstructor("parameter1");
classWithPvtConstructor.a = 30;
Console.WriteLine("Value of a is {0}", classWithPvtConstructor.a);
Console.ReadKey();
}
}
public class ClassWithPvtConstructor
{
public int a = 0;
private ClassWithPvtConstructor()
{
Console.WriteLine("This is a private constructor.");
}
public ClassWithPvtConstructor(string strInput)
{
Console.WriteLine("This is a private constructor with Parameter " + strInput);
}
}
Anil Kumar MurmuPosted Jan 12, 2016, 3:11 AM EST
Ans: Compile time error
Desc:
MyClass has private constructor. A class having private constructor cannot be inherited or instantiated.
Rajesh SinghPosted Jan 9, 2016, 8:09 AM EST




