Constructors and Inheritance

后端 未结 10 937
臣服心动
臣服心动 2020-12-16 15:18

Lets take an example in C#

public class Foo
{
    public Foo() { }
    public Foo(int j) { }
}

public class Bar : Foo
{

}

Now, All the pu

相关标签:
10条回答
  • 2020-12-16 15:37

    Constructors are not inheritable for design reasons. (Note that this is the same situation in every object-oriented language of which I know.) The simple answer is that in many cases you'd really not want the same constructors as the base class to be available. See this SO thread for some more complete explanations.

    0 讨论(0)
  • 2020-12-16 15:37

    Really, its because the parent constructor wouldn't fully initialize the child object. A constructor is kind of a personal thing in that respect. That's why most languages don't inherit constructors.

    0 讨论(0)
  • 2020-12-16 15:39

    They are (via chaining), you would have to chain the constructor in your derived object.. IE:

    public class Foo
    {
        public Foo() { }
        public Foo(int j) { }
    }
    
    public class Bar : Foo
    {
        public Bar() : base() { }
        public Bar(int j) : base(j) { }
    }
    

    The constructors in the derived objects will then chain the calls do the constructors in the base objects.

    This article provides some more examples if you want further reading.

    0 讨论(0)
  • 2020-12-16 15:40

    Constructors are not inheritable because it might cause weird and unintended behavior. More specifically, if you added a new constructor to a base class, all derived classes get an instance of that constructor. That's a bad thing in some cases, because maybe your base class specifies parameters that don't make sense for your derived classes.

    A commonly given example for this is that in many languages, the base class for all objects (commonly called "Object") has a constructor with no parameters. If constructors were inherited, this would mean that all objects have a parameterless constructor, and there's no way to say "I want people who make an instance of this class to provide parameters X, Y and Z, otherwise their code shouldn't compile." For many classes, it's important that certain parameters be defined for their proper function, and making constructors non-heritable is part of the way that class authors can guarantee that some parameters are always defined.

    Edit to respond to comments: Ramesh points out that if constructors were inherited as he would like them to be, he could always override base class constructors using privately declared constructors in each derived class. That is certainly true, but there it a logistical problem with this strategy. It requires that writers of derived classes have to watch base classes closely and add a private constructor if they want block inheritance of the base class constructor. Not only is this a lot of work for people writing derived classes, this kind of implicit dependency across classes is exactly the sort of thing that can cause weird behavior.

    Ramesh - it's not that what you describe would be impossible to add to a language. In general it's not done because that sort of behavior could confuse people and lead to a lot of extra debugging and code writing.

    Quintin Robinson provides some very worthwhile responses to this question in the comments that are definitely worth reading.

    0 讨论(0)
  • 2020-12-16 15:41

    I think you can do the following:

    public class Bar : Foo
    {
      public Bar (int i)
        : base (i)
      {
      }
    }
    

    I may be a bit off -- but it's the general idea.

    0 讨论(0)
  • 2020-12-16 15:42

    One reason why you might introduce a constructor into a class is because it makes no sense to have an instance of that class without a specific "dependency". For example, it might be a data-access class that has to have a connection to a database:

    public class FooRepository
    {
        public FooRepository(IDbConnection connection) { ... }
    }
    

    If all the public constructors from base classes were available, then a user of your repository class would be able to use System.Object's default constructor to create an invalid instance of your class:

    var badRepository = new FooRepository();
    

    Hiding inherited constructors by default means that you can enforce dependencies without worrying about users creating "invalid" instances.

    0 讨论(0)
提交回复
热议问题