11

While studying for my finals, I came across the following statement in the book from which I am currently studying. Considering the following code :

class A {
    public A(int x) {   }
}

class B extends A {
    public B(int x ) {   }
}

is it mandatory to call the constructor of class A in the constructor of class B(super(x)). The book states that it's not mandatory, because they have the exact number and type of parameters. But when I try this in a java compiler, the following error gets thrown :

constructor A in class A cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length

0

5 Answers 5

17

The compiler automatically inserts super() in the beginning.

However, even constructors arguments, super() (without arguments) is added which invokes the default constructor of the superclass. And you don't have one, hence the error.

You have to specify super(x) (to invoke A(x)), or define a no-argument constructor.

By the way, Eclipse compiler gives a way better error message:

Implicit super constructor A() is undefined. Must explicitly invoke another constructor

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

2 Comments

The error is thrown at this line : "public B(int x ) {}". The code that I posted in the question is all the code I am trying to compile (just the class definition, no instantiations at all).
It will only insert super(). It won't insert super(int). Hence the error here. In this case, he does need to call super(x). See download.oracle.com/javase/tutorial/java/IandI/super.html .
4

It looks like the compiler tries to create a call to the superclasses default constructor with super(), which isn't avaliable:

required: int
found:    no arguments

But back to your book: I've never heard of a rule that you can skip the super statement in a constructor if the actual constructor has the exact same parameter list as a constructor in the direct superclass. Only a call to the superclass's default constructor is added implicitly (super()) but that requires that the superclass has a default constructor.

In contrast to what's written in your book (or in contrast to your understanding of the written text), here's a sentence from the language spec:

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a super class constructor invocation “super();”, an invocation of the constructor of its direct superclass that takes no arguments.

Comments

4

If you have a the base class having a default constructor (no-arg constructor), when you extend B, you don't need to explicitly call super() because it is called any way.

But when you have a constructor with arguments, when making the contructor with parameters in B, you need to pass in super() a parameter for A

example :

class A {
    public A(int x) {   }
  }

  class B extends A {
    public B(int x ) 
    {
       super(x); // need to specify the parameter for class A
       //... 
    }
  }

Comments

1

This happens when you dont have a default constructor and you are creating an instance with default one. Because If you have any Parameterized constructor then compiler will not insert the default one for you, instead you have to define.

Comments

0

It is neccessary to call constructor of super class in case of Java. Therefore, whenever you generate constructor of sub class, super class' constructor is self created by IDE.

It is because whenever base class constructor demands arguments, compiler thinks they are to be filled by base class constructor. In case of default constructor, it is OK. No need to call super() in sub class.

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.