Constructor in class cannot be applied to given types

后端 未结 3 1060
别那么骄傲
别那么骄傲 2020-12-02 01:01

I ve got the following code using arrays to find some prim numbers. However, when trying to compile my user class PalindromeArrayUser it says - \"Constructor in class cannot

3条回答
  •  死守一世寂寞
    2020-12-02 01:33

    when you create a constructor for a class, there won't be any default constructor created for that class. so if you extend that class and if the subclass tries to call the no-arg constructor of its super class then there will be an compile-time error.

    to demonstrate:

    class Parent {
      int i;
      public Parent(int i) {
        this.i=i;
      }
    }
    
    class Child extends Parent {
      int j;
      public Child(int i, int j) {
        super(i);
        this.j=j;
      }
      public Child(int j) {
        // here a call to super() is made, but since there is no no-arg constructor
        // for class Parent there will be a compile time error
        this.j=j;
      }
    }
    

    EDIT:

    to answer your question do this, don't assign the value arrLength to arr[] and check[] as arrLength would be 0 at that time.

    so just declare them like this

    int arr[];
    boolean check[];
    

    and in the constructor after you assign the input to arrLength put these statements.

    arr = new int[arrLength];
    check = new boolean [arrLength];
    

提交回复
热议问题