1

class A

class A {

    int a;
    int c;

    A (int a, int c) {

       this.a = a;
       this.c = c;

    }
}

class B

class B extends A{

    public static void main (String [] args) {

        A obj = new A (5, 6);

    }
}

When I compile the code It shows me this error

 B.java:1: error: constructor A in class A cannot be applied to given types;
    class B extends A{
    ^
      required: int,int
      found: no arguments
      reason: actual and formal argument lists differ in length
    1 error

When this error appears exactly? And when inheritance the class Is the constructor must be the same type of super class?

2 Answers 2

5

A specifies a constructor taking two arguments, B only specifies a parameterless (the default one).

If you really want to let B inherit from A, you'll need to create a constructor as well, either one with two arguments as well, or one just calling the constructor of A with default values:

// variant A

class B extends A {
  B (int a, int c) {
    super(a, c);
  }
}

// variant B

class B extends A {
  B () {
    super(0, 0); // replace 0 with an appropiate default value for each parameter
  }
}

However, from your code, B wouldn't need to inherit from A, if you only want to instantiate A in the main. Just remove the inheritance in that case.

Sign up to request clarification or add additional context in comments.

Comments

1

Add a constructor to class B.

The compiler realizes that you cannot instantiate class B!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.