1

this is my program:

class A{
    A(int a){
    }

class B extends A{
    B(){    
    }
}

}

when I compile I have an error:

C:\Users\Public\Documents\AB.java:6: error: constructor A in class A cannot be applied to given types;
    B(){
       ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length

and it fixed by this change in my code:

    class A{
        A(int a){
        }
            A(){
        }

    class B extends A{
        B(){
        }
    }
}

but I don't understand why?

thanks for any help.

2
  • Read about default constructor, and also in what situation the compiler adds it to your class. Commented Nov 24, 2013 at 18:50
  • Similar question stackoverflow.com/questions/7187799/… Commented Nov 24, 2013 at 18:50

4 Answers 4

6

Since your A class declares a constructor

A(int a){
}

then any code that wants to instantiate A needs to do so with that constructor. Something like

A a = new A(42);

You also declare a child class, B. Since B is an A, in addition to its constructor, it must call the parent class' constructor. This is done implicitly by the compiler. Say you had

class A{
    A(){
    }

class B extends A{
    B(){
        // super(); injected by compiler
    }
}

If you don't have a no-arg constructor, then the compiler doesn't know which constructor call to inject. You need to explicitly declare the super(...) call.

class A{
    A(int a){
    }

class B extends A{
    B(){    
        super(42);
    }
}

Think of it this way: you can't construct an A without the constructor call. When you construct a B, you are, through inheritance, also constructing an A, so you need that same constructor call.

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

Comments

0

You need to have a default constructor (no-argument) in A. The first implicit call of a constructor of any class is super(). But there is not super() method in the superclass. You can either explicitly call super(5) or some value, or create a default constructor in A:

A() {}

Comments

0

When instance of class B is created a default call to super() constructor with no parameters is made. Each class has a default constructor with no parameters, unless a different constructor is provided explicitly.

As you have explicitly provided class A with a constructor that takes one parameter, it no longer has default constructor with no params, so you need to define it.

Comments

0

Whenever we extend a class, by default it calls super() (it has no params). In your case , it dint get the matching A() , hence it resulted in compiler error. Whenever u write this, what happens internally is :

 class A
  {
   A(int a)
   {
   }
 class B extends A
 {
   B()
   {   
    //Internally compiler calls super()
    super();//In your case, it dint get the matching constructor
   }
 }

 }

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.