What will be output of below code? public class MyClass{private MyClass(){}}class Program:MyClass{static void Main(string[] args){Console.WriteLine("Hello");Console.ReadLine();}}
Rajesh Singh
Constructor -> private MyClass() cannot be inherited Program:MyClass
Hello
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.
Error Because A class having private constructor cannot be inherited or instantiated.
Error I because ClassWithPvtConstructor cannot be declare as private
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);}}
Ans: Compile time errorDesc: MyClass has private constructor. A class having private constructor cannot be inherited or instantiated.