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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bassam Badr
  • 667
  • 4
  • 16
  • 25

2 Answers2

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.

Femaref
  • 60,705
  • 7
  • 138
  • 176
1

Add a constructor to class B.

The compiler realizes that you cannot instantiate class B!

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194