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

后端 未结 12 1373
终归单人心
终归单人心 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.

    0 讨论(0)
  • 2020-11-29 21:53

    When extending a class, the default superclass constructor is automatically added.

    public class SuperClass {
    }
    
    public class SubClass extends SuperClass {
       public SubClass(String s, Product... someProducts) {
          //super(); <-- Java automatically adds the default super constructor
       }
    }
    

    If you've overloaded your super class constructor, however, this takes the place of the default and invoking super() will thus cause a compile error as it is no longer available. You must then explicitly add in the overloaded constructor or create a no-parameter constructor. See below for examples:

    public class SuperClass {
       public SuperClass(String s, int x) {
         // some code
       }
    }
    
    public class SubClass extends SuperClass {
       public SubClass(String s, Product... someProducts) {
          super("some string", 1);
       }
    }
    

    OR...

    public class SuperClass {
       public SuperClass() {
         // can be left empty.
       }
    }
    
    public class SubClass extends SuperClass {
       public SubClass(String s, Product... someProducts) {
          //super(); <-- Java automatically adds the no-parameter super constructor
       }
    }
    
    0 讨论(0)
  • 2020-11-29 21:55

    There are a few things to be noted when using constructors and how you should declare them in your base class and super class. This can get somewhat confusing solely because there can be many possibilities of the availability or existence of constructors in the super class or base class. I will try to delve into all the possibilities:

    • If you explicitly define constructors in any class(base class/super class), the Java compiler will not create any other constructor for you in that respective class.

    • If you don't explicitly define constructors in any class(base class/super class), the Java compiler will create a no-argument constructor for you in that respective class.

    • If your class is a base class inheriting from a super class and you do not explicitly define constructors in that base class, not only will a no-argument constructor be created for you (like the above point) by the compiler, but it will also implicitly call the no-argument constructor from the super class.

      class A 
      {
         A() 
       {
        super();
       }
      }
      
    • Now if you do not explicity type super(), (or super(parameters)), the compiler will put in the super() for you in your code.
    • If super() is being called (explicitly or implicitly by the compiler) , the compiler will expect your superclass to have a constructor without parameters. If it does not find any constructor in your superclass without parameters, it will give you a compiler error.

    • Similary if super(parameters) is called, the compiler will expect your superclass to have a constructor with parameters(number and type of parameters should match). If it does not find such a constructor in your superclass, it will give you a compiler error. ( Super(parameters) can never be called implicitly by the compiler. It has to be explicitly put in your code if one is required.)

    We can summarize a few things from the above rules

    • If your superclass only has a constructor with parameters and has no no-argument constructor, you must have an explicit super(parameters) statement in your constructor. This is because if you do not do that a super() statement will be implicitly put in your code and since your superclass does not have a no-argument constructor, it will show a compiler error.
    • If your superclass has a constructor with parameters and another no-argument constructor, it is not necessary to have an explicit super(parameters) statement in your constructor. This is because a super() statement will be implicitly put in your code by the compiler and since your superclass has a no-argument constructor, it will work fine.
    • If your superclass only has a no-argument constructor you can refer to the point above as it is the same thing.

    Another thing to be noted is if your superclass has a private constructor, that will create an error when you compile your subclass. That is because if you don't write a constructor in your subclass it will call the superclass constructor and the implicit super() will try to look for a no-argument constructor in the superclass but will not find one.

    0 讨论(0)
  • 2020-11-29 21:56

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

    I would say this statement is not always correct. As ideally its not required.

    The Rule is : If you are explicitly providing an argument-ed constructer, then the default constructor (non-argumented) is not available to the class.

    For Example :   
    class A {    
      A(int i){    
      }
    }
    
    class B extends A {
    }
    

    So when you write

    B obj_b = new B();
    

    It actually calls the implicit constructor provided by java to B, which again calls the super(), which should be ideally A(). But since you have provided argument-ed constructor to A, the default constructor i:e A() is not available to B().

    That's the reason you need A() to be specifically declared for B() to call super().

    0 讨论(0)
  • 2020-11-29 21:57

    I would guess that its because when you have an empty parameter list the super variable can't be instantiated. With empty parameter list I mean the implicit super() the compiler could add if the super class had a nonparametric constructor.

    For example if you type:

    int a;
    System.out.print(a);
    

    You will get an error with what I think is the same logic error.

    0 讨论(0)
  • 2020-11-29 21:58

    There are two aspects at work here:

    • If you do specify a constructor explicitly (as in A) the Java compiler will not create a parameterless constructor for you.

    • If you don't specify a constructor explicitly (as in B) the Java compiler will create a parameterless constructor for you like this:

      B()
      {
          super();
      }
      

    (The accessibility depends on the accessibility of the class itself.)

    That's trying to call the superclass parameterless constructor - so it has to exist. You have three options:

    • Provide a parameterless constructor explicitly in A
    • Provide a parameterless constructor explicitly in B which explicitly calls the base class constructor with an appropriate int argument.
    • Provide a parameterized constructor in B which calls the base class constructor
    0 讨论(0)
提交回复
热议问题