1

Since we know that constructor is not inherited in the child class as i asked in the my previous question Click here to view question

I had write the code

namespace TestConscoleApplication
{
    abstract public class A
    {
       public int c;
       public int d;

    private  A(int a, int b)
       {
           c = a;
           d = b;

       }
        public virtual void Display1()
        {
            Console.WriteLine("{0}{1}", c, d);
        }
    }
    internal class B : A 
    {
      protected  string Msg;

      public B(string Err)

        {
            Msg = Err;

        }

        public  void Display()
        {
            Console.WriteLine(Msg);
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            B ObjB = new B("Hello");


            Console.ReadLine();
        }
    }
}

when i compile the code its showing an error

Error TestConscoleApplication.A.A(int, int) is inaccessible due to its protection level.

Then why it is showing an error.

Community
  • 1
  • 1
naval
  • 1,423
  • 1
  • 13
  • 26
  • Since we know their is concept of private constructor when don`t want create an instance of the class and one point is also that constructor can not inherit the child class then why it showing the error.as simple concept is that private members are inherit in the child class but they are not accessible in the child class and i am not accessing the or calling the constructor of the base class in the child class – naval Jun 30 '12 at 10:44

4 Answers4

4

By making the only constructor of A private, you've prevented derived classes from being able to be constructed outside A.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • Since we know their is concept of private constructor when don`t want create an instance of the class and one point is also that constructor can not inherit the child class then why it showing the error.as simple concept is that private members are inherit in the child class but they are not accessible in the child class and i am not accessing the or calling the constructor of the base class in the child class – naval Jun 30 '12 at 10:44
  • 1
    @naval anytime you construct a derived class using the `new` keyword, it also calls the constructor of the base class. If you don't specify the base class constructor (e.g. `:base(1,2)`), it will try to use the _default_ constructor of the base class if one exists. In your case the base class does not have a default constructor because you explicitly declared one. And that constructor happens to be `private`, which cannot be accessed from `B`. Even if you made it `public`, you'd then see that it will complain about invalid arguments (it will want the `:base(x,y)`). – Eren Ersönmez Jun 30 '12 at 10:54
  • Then are you agree that constructor are inherit in the child class – naval Jun 30 '12 at 11:08
  • 1
    No. Constructors are not inherited -- when calling the constructor of the derived class (B) one of the base class (A) constructors is also called. That is different from _inheritance_. If constructors were inherited, that would mean if `A` has a constructor like `A(int a, int b)` then `B` would automatically inherit that constructor so you would be able to do `new B(1,2)` -- that is not the case. If you want to be able to do that, you have to write a new constructor for B like `B(int a, int b)`. – Eren Ersönmez Jun 30 '12 at 11:49
1

Derived class constructor always call the base constructor (one of). Making it private you prohibit access to it from outside. In other words you make it impossible to make an instance of A outside A.

Since you made a constructor, the compiler won't generate a default public one for this class for you.

If you want to provide access to it from the class descendant but not from outside, you should make it protected.

Ivan Nikitin
  • 3,578
  • 27
  • 39
0

You need to have a constructor for A accessible to B, and use it. Also, the default base constructor is base() (i.e. the parameterless constructor), which doesn't exist on A. Here's one way you can resolve this (nonessential bits removed):

abstract public class A
{
    protected A(int a, int b)
    {
    }
}
internal class B : A
{
    public B(int a, int b, string Err)
        : base(a, b)
    {
    }
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • Since we know their is concept of private constructor when don`t want create an instance of the class and one point is also that constructor can not inherit the child class then why it showing the error.as simple concept is that private members are inherit in the child class but they are not accessible in the child class and i am not accessing the or calling the constructor of the base class in the child class – naval Jun 30 '12 at 10:40
-1

constructors shouldn't be private otherwise you will not be able to create an instance of that class and won't be able to inherit it too, but if you want to create a private constructor create a public one with it too. For more info Go here

yogi
  • 19,175
  • 13
  • 62
  • 92
  • Yes they are but you have to have a public one too, if you are intended to inherit that class or create an instance of that class. – yogi Jun 30 '12 at 11:35