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

后端 未结 12 1374
终归单人心
终归单人心 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 22:00

    Assuming that you meant to write class B extends A:

    Every constructor has to call a superclass constructor; if it does not the parameterless superclass constructor is called implicitly.

    If (and only if) a class declares no constructor, the Java compiler gives it a default constructor which takes no parameters and calls the parameterless constructor of the superclass. In your example, A declares a constructor and therefor does not have such a default constructor. Class B does not declare a constructor, but cannot get a default constructor because its superclass does not have a parameterless constructor to call. Since a class must always have a constructor, this is a compiler error.

    0 讨论(0)
  • 2020-11-29 22:05

    Of course its an error if written like this it's not JAVA.

    If you would have use JAVA syntax it wouldn't be an error.

    Class A and B knows nothing about each other if in separate files/packages.

    Class A doesn't need a default constructor at all it works fine with only a parameter constructor.

    If B extends A you simple use a call to super(int a) in B's constructor and everything is fine. for constructors not calling a super(empty/or not) extending a super class the compiler will add a call to super().

    For further reading look at Using the Keyword super

    0 讨论(0)
  • 2020-11-29 22:09
    Why default constructor is required(explicitly) in a parent class if it 
    has an argumented constructor
    

    Not necessarily!

    Now in your class B

    class B extends A {
    }
    

    you have not provided any constructor in Class B so a default constructor will be placed. Now it is a rule that each constructor must call one of it's super class constructor. In your case the default constructor in Class B will try to call default constructor in class A(it's parent) but as you don't have a default constructor in Class A(as you have explicitly provided a constructor with arguments in class A you will not have a default constructor in Class A ) you will get an error.

    What you could possibly do is

    Either provide no args constructor in Class A.

    A()
    {
      //no arg default constructor in Class A
    }
    

    OR

    Explicitly write no args constructor in B and call your super with some default int argument.

    B()
    {
        super(defaultIntValue);
    }
    

    Bottom line is that for an object to be created completely constructors of each parent in the inheritance hierarchy must be called. Which ones to call is really your design choice. But in case you don't explicitly provide any java will put default constructor super() call as 1st line of each of your sub class constructors and now if you don't have that in superclass then you will get an error.

    0 讨论(0)
  • 2020-11-29 22:10

    Every subclass constructor calls the default constructor of the super class, if the subclass constructor does not explicitly call some other constructor of the super class. So, if your subclass constructor explicitly calls a super class constructor that you provided (with arguments), then there is no need of no arguments constructor in the super class. So, the following will compile:

    class B extends A{
         B(int m){
            super(m);
         }
    }
    

    But the following will not compile, unless you explicitly provide no args constructor in the super class:

    class B extends A{
         int i; 
         B(int m){
            i=m;
         }
    }
    
    0 讨论(0)
  • 2020-11-29 22:16

    Because if you want to block creation of objects without any data in it, this is one good way.

    0 讨论(0)
  • 2020-11-29 22:18

    Say this compiled, what would you expect it to print?

    class A{
      A(int i){
        System.out.println("A.i= "+i);
      }
    }
    
    class B extends A {
      public static void main(String... args) {
        new B();
      }
    }
    

    When A is constructed a value for i has to be passed, however the compiler doesn't know what it should be so you have specify it explicitly in a constructor (any constructor, it doesn't have to be a default one)

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