Why default constructor is required in a parent class if it has an argument-ed constructor?

后端 未结 12 1528
终归单人心
终归单人心 2020-11-29 21:17

Why default constructor is required(explicitly) in a parent class if it has an argumented constructor

class A {    
  A(int i){    
  }
}

class B extends          


        
12条回答
  •  清歌不尽
    2020-11-29 21:51

    When we have parameter constructor. we explicit bound to consumer by design. he can not create object of that class without parameter. some time we need to force user to provide value. object should be created only by providing parameter(default value).

    class Asset
    {
        private int id;
        public Asset(int id)
        {
            this.id = id;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            /* Gives Error - User can not create object. 
             * Design bound
             */
            Asset asset1 = new Asset();/* Error */
        }
    }
    

    Even child class can not create. hence it is behavior of good design.

提交回复
热议问题