Why do I need to write a constructor here explicitly?

前端 未结 7 1877
無奈伤痛
無奈伤痛 2021-01-15 11:28

This is how I encountered the problem. I am giving an example:

package check;

public class Check {
  int a,b;
  Check (int i,int j) {
    a = i;
    b = j;
         


        
相关标签:
7条回答
  • 2021-01-15 11:50

    Think that it's not warning you and you write a piece of code like:

    V v = new V();
    

    Then, what would you expect your compiler to behave? I mean, there is no no-arg consructor defined. You need to explicity add it.

    Check() {
    
    }
    
    0 讨论(0)
  • 2021-01-15 11:51

    When you create a class, if it has no constructors then Java will provide a default constructor for you. If, however, you have 1 or more constructors, then Java does not provide this default (or zero argument) constructor.

    In your subclass, you have not yet created any constructors, so java has provided a default constructor for you. In subclasses, there is an implicit call to super() in the constructor. Since the no argument constructor has not been defined in the super class, Java is effectively trying to invoke a constructor that does not yet exist.

    0 讨论(0)
  • 2021-01-15 11:51
    Why is it compulsory to use a constructor at all for class V?
    

    It is because In the check class you are explicitly use an parametrized constructor and is also the super class of class V, So if you are not mentioning paramaterized constructor then you need to call super() explicitly and again if you are not defining any parametized constructor in check class then compiler is going to invoke an default no-argument constructor and there it will place super() which is responsible to call Object class constructor .

    0 讨论(0)
  • 2021-01-15 11:53

    Because when creating an object in Java, the classes superconstructor will have to be called first. Since you don't have default values for the superconstructor and you don't have a default constructor for the superclass, you need to provide one for the subclass, which calls super(i, j)

    0 讨论(0)
  • 2021-01-15 11:55

    1) Why is it compulsory to use a constructor at all for class V? AFAIK, creating a constructor is not necessary as JAVA compiler creates default constructor automatically to carry on its operation. Also from the message, it also seems a default constructor is needed, but is not written by me, but as I said doesn't JAVA create it automatically?

    A default constructor is only created when no other constructor exists, when you created the constructor Check(int i,int j) you removed the default constructor.

    When you do not include a call to super within a constructor java attempts to call super() by default. However as there is no default constructor in the parent class it cannot do this.

    This behaviour is good because you may not want a default constructor; certain variables may have to be initialised for the object to behave correctly. As such there needs to be a way to remove the default constructor, and this is done by explicitly creating a constructor.

    2) Another thing, I changed the code in subclass as V(int i, int j){ super.a=i; super.b=j}., but I was still getting error. Why is that? Isn't this code super.a=i; super.b=j same with super(i,j)? Also, in class V, I might not need to use b, then why should I need to initialize it by a constructor?

    The code

    V(int i, int j){ 
       super.a=i; super.b=j
    }
    

    still has no call to a parent constructor, as such becomes

    V(int i, int j){ 
       super();
       super.a=i; super.b=j
    }
    

    And again super() does not exist

    Some call to super must exist to "set up" the parent parts of the object, all the way down to Object (which all objects implicitely extend). As such you cannot omit the parent constructor simply because you do something equivalent in the child constructor

    0 讨论(0)
  • 2021-01-15 11:59

    1) The compiler generates a no-arg constructor only if you don't define any constructor yourself. In class Check you have defined a constructor.

    2) If you don't explicitly call a super class constructor in a constructor that you define, the compiler generates a call to a no-arg constructor. Since class Check doesn't have a no-arg constructor, the compiler can't generate the call and you must write it yourself.

    You might be wondering why the constructor of a class must call the constructor of its parent. This is to ensure that the object is fully initialized before you try to use it: the parent constructor may initialize some aspect of the object that is only accessible to itself, and calling the constructor when the object is created is the only way to ensure proper initialization.

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