Constructor in class cannot be applied to given types

后端 未结 3 951
别那么骄傲
别那么骄傲 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:21

    Error is because you extend PalindromeArray .This is not necessary. Subclass (your PalindromeArrayUser) must provide constructor with one int.

    If yous super class don't have default constructor then in you subclass constructors must invoke one of non-default constructor from super class. (super(params))

    0 讨论(0)
  • 2020-12-02 01:21

    Error is because you are extending PalindromeArray, which has an explicit constructor. You must provide the constructor with argument.

    Because there is no default constructor available in B, as the compiler error message indicates. Once you define a constructor in a class, the default constructor is not included. If you define any constructor, then you must define all constructors.

    Read more here

    Disclaimer: The above is taken from the article

    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题