0

Can you please tell me the problem in following code?

class boxdemo1 {
    public static void main(String args[]) {
        boxweight weightbox = new boxweight(2, 3, 5, 4);
        System.out.println(weightbox.volume());
    }
}

class boxinfo {
    int l, b, h;

    /*
     * boxinfo() { l=b=h=-1; }
     */
    boxinfo(int a, int b, int c) {
        l = a;
        this.b = b;
        h = c;
    }

    int volume() {
        return l * b * h;
    }
}

class boxweight extends boxinfo {
    int w;

    boxweight(int a, int b, int c, int w) {
        l = a;
        this.b = b;
        h = c;
        this.w = w;
    }
}

When I compile it,it shows following error: "constructor boxinfo in class boxinfo cannot be applied to given types; required:int,int,int; found:no arguments; actual and formal argument lists differ in length."

But when I compile it including the code which is commented(i.e. boxinfo() constructor), it gets compiled. Why is it necessary to include default constructor?

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
Prabhjeet Singh
  • 317
  • 1
  • 4
  • 8
  • why arent you calling super? – ΦXocę 웃 Пepeúpa ツ May 23 '17 at 12:08
  • As boxweight inherits from boxinfo you need to call boxinfo's constructor from boxweight using super(arguments) On a sidenote, classes in Java should start with a capital letter – Jdv May 23 '17 at 12:08
  • https://docs.oracle.com/javase/tutorial/java/IandI/super.html – Maroun May 23 '17 at 12:09
  • 1
    Possible duplicate of ["Constructor cannot be applied to given types" when constructors have inheritance](https://stackoverflow.com/questions/14384563/constructor-cannot-be-applied-to-given-types-when-constructors-have-inheritanc) – Matsemann May 23 '17 at 12:18

6 Answers6

1

As this is necessary to build the super class part of the instance to get something working, the call of a super constructor is necessary.

If there is no statement calling super() in first statement of a constructor (or this() that would end up with a super()), the compiler will add a call to the default constructor (super();).

Since you don't have a constructor with no parameter, this can't compile as there is no constructor that match this statement.

In the same way, if a class don't define any constructor, a default constructor is implemented like

public MyClass(){ super(); }

Giving the same kind of problem if the superclass of MyClass only provide a constructor with parameters like said in the JLS

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.

To correct your problem, simply call the correct super constructor or define a constructor with no parameter (not called a default constructor anymore, only the one defined by the compiler is the default one ;) )

AxelH
  • 14,325
  • 2
  • 25
  • 55
1

Reason for the error-

Child class constructor calls the constructor of the parent which takes four parameters.

There is no constructor in the boxinfo class which takes four parameters.

Furthermore,

Java compiler automatically insert super() constructor to the child class. Meaning of this super constructor is Go and execdute a constructor in the parent class which takes no parameters.

According to your program:

boxinfo(int a, int b, int c) {
        l = a;
        this.b = b;
        h = c;
    }

When you run your program compiler remove the default constructor because constructor in parent class takes three parameters.

If you do not add constructor for your boxweight class Java compiler automatically insert default constructor. Since you have added four parameters compiler will check for four parameter constructor . As I said before when you extends another class(boxinfo) in the constructor also Java compiler automatically insert the super() in the first line of constructor.

It's look like this:

class boxweight extends boxinfo{
   boxweight (int a, int b, int c, int w){
     super();
     //.....
   }
}

We can add super() to child class's constructor in a way that it matches an existing constructor in the parent class.

Solution for your qustion,

Add super() in constructor(first line) of child class and add values to match the parent class constructor.

Like this:

boxweight(int a, int b, int c, int w) {
   super(a, b, c);
   this.w = w;
}

Now you dont need these assign l = a;, this.b = b;, and h = c; because through the constructor you send it to the parent class.

Note: when you add super() must be the first line in child class constructor.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

By setting your boxinfo(int a, int b, int c) constructor, the default no-parameters constructor set by java for boxinfo gets deleted. Then, when you call the boxweight(int a,int b,int c,int w) constructor, since the boxweight class inherits from boxinfo, you're implicitly calling the default constructor from boxinfo which just got overrided.

To avoid this, either create yourself a no-argument constructor in boxinfo or call explicitly super(a, b, c) in boxweight

Kaiserbogey
  • 191
  • 13
0

It's necessary this when you overload the default constructor, the default one doesn't exist anymore.

eg:

class boxweight extends boxinfo {
     boxweight(int a, int b, int c, int w) {
         ...
     }
}

is actually the same as :

class boxweight extends boxinfo {
     boxweight(int a, int b, int c, int w) {
         super();
         ...
     }
}

but in boxinfo such constructor doesn't exist anymore this you create a custom constructor. You need to keep it in code to make it callable.

milcaepsilon
  • 272
  • 3
  • 16
0

A subclass constructor must initialize it's super class members by calling at least one of the constructors of its super class.

This becomes mandatory if the superclass does not have a default constructor such as the one in your example.

In this case, Your boxweight constructor must initliaze the other members that boxinfo declares. So you must invoke super(...) to initialize boxinfo.

so, your boxweight constructor would become:

boxweight(int a, int b, int c, int w) {
    super(a,b,c); // put this call here and remove other lines.
    this.w = w; // this is sufficient
}

PS: I recommend that you follow naming conventions to name your classes, methods and other Java artifacts.

http://www.oracle.com/technetwork/java/codeconventions-135099.html

anacron
  • 6,443
  • 2
  • 26
  • 31
-2

When you extend a class using inheritance and create object of subclass the constructor of subclass will call its parent class constructor.In your case you are using below constructor it will call default constructor.In your class you have not specify the default constructor.In that case you can use super to call the declared constructor in parent class.

boxweight(int a, int b, int c, int w) {
       super(a, b, c); //or declared default constructor
        l = a;
        this.b = b;
        h = c;
        this.w = w;
    }
gati sahu
  • 2,576
  • 2
  • 10
  • 16