If a class A has non-empty constructor as shown below, class B inherits from A but does not have any constructor. Is that an error condition?

If a class A has non-empty constructor as shown below, class B inherits from A but does not have any constructor. Is that an error condition?

Ninja Asked on 18th September 2018 in Java.
Add Comment
1 Answer(s)
Best answer

Question:

public class A {
 public A(int x) {
  System.out.println("Constructor invoked "+x);
 }
}

Answer:

Yes, this is an error condition. Since constructors are not inherited, results in the below compilation error.

Constructor A in class A cannot be applied to given types;

public class B extends A{
^

required: int

found: no arguments

reason: actual and formal argument lists differ in length

Ninja Answered on 18th September 2018.
Add Comment