Inheritance classes (Java), explicit constructor error message

897 views Asked by At

so I am trying to learn about inheritance classes.

First I created a class called Box to calculate the area of the box.

Then I created a TestBox Class in which I have created a box object called fedEx.

Box Class:

public class Box {
    private String boxName;

    public void calculateArea(int length, int width) {
        System.out.println("Area of " + getBoxInfo() + (length * width));
    }

    public Box(String boxName) {
        this.boxName = boxName;
    }

    public String getBoxInfo() {
        return boxName;
    }
}

TestBox Class:

public class TestBox {

    public static void main(String[] args) {

        Box fedEx = new Box("fedEx");
        fedEx.calculateArea(23, 2);
    }
}

So Far if I run this code everything works out fine and my print screen shows Area of Fedex 46

So now I went to create a new class called NewBox and used "extends" to inherit the methods from the class Box, this class is used to calculate Volume

NewBox Class:

public class NewBox extends Box {

    public void calculateVolume(int length, int width, int height) {
        System.out.println("Volume = " + (length * width * height));
    }
}

Now to test this I created a new object in my TestBox class called UPS, now my TestBox class looks like this:

public class TestBox {

    public static void main(String[] args) {

        Box fedEx = new Box("fedEx");
        fedEx.calculateArea(23, 2);

        NewBox UPS = new NewBox("UPS");
        UPS.calculateArea(3, 2);
        UPS.calculateVolume(3, 2, 2);
    }
}

When I try to run this program I get the following error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor NewBox(String) is undefined
    at day3.inheritence.TestBox.main(TestBox.java:10)

I am using eclipse as my IDE.

What can I do to fix my code, and what does the error message mean?

5

There are 5 answers

2
Jim Simon On BEST ANSWER

NewBox has to have a constructor that forwards to the parent class's constructor. Try this:

public class NewBox extends Box{ 

  public NewBox(String name) {
    super(name);
  }

  public void calculateVolume(int length, int width, int height){ 
    System.out.println("Volume = " + (length*width*height));
  }
}
0
Sarfaraz Khan On

Since your parent class(Box) is having a parametrized constructor your child class(NewBox) must have a parametrized constructor which then in turn should call its super constructor.So add the below constructor in NewBox class

public NewBox(String name){
   super(name)
}
0
Makoto On

Let's first talk about default constructors. A default constructor is implicitly declared if you do not create a constructor yourself.

public class SomeClass {
    // field and method declarations to follow
}

If you declare a constructor, then there is no default constructor; there is only the declared constructor(s).

public class Box {

    private final String boxName;

    public Box(String boxName) {
        this.boxName = boxName;
    }
}

With inheritance, if the child is implicitly declaring a default constructor but the parent does not have a constructor with a similar signature, a compile-time error occurs.

More specifically:

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.

In your case, add the super keyword, and pass in the argument straight to the parent.

public class NewBox extends Box {
    public NewBox(String boxName) {
        super(boxName);
    }
}

As another example, the class Beta will compile just fine because Alpha declares a constructor that matches the default constructor's signature, but Delta will not compile because Gamma does not have a constructor that matches.

class Alpha {
    public Alpha() {

    }
}

class Beta extends Alpha {

}

class Gamma {
    private final int count;

    public Gamma(int count) {
        this.count = count;
    }
}

class Delta extends Gamma {

}

This can be fixed by providing a no-arg constructor to `Gam

class Gamma {
    private final int count;

    public Gamma(int count) {
        this.count = count;
    }

    public Gamma() {
        count = 0;
    }
}
0
Atul Yadav On

Your class NewBox should be something like this

public class NewBox extends Box{

    public NewBox(String boxName){
       super(boxName);        
    }

    public void calculateVolume(int length, int width, int height){
        System.out.println("Volume = " + (length*width*height));
    }
}

By writing NewBox UPS = new NewBox("UPS");in your TestBox, JVM is expecting constructor with string as parameter in NewBox, but it is missing there. So we create a constructor which is having string as parameter in NewBox, and in that constructor we write super(boxName) which says to call the constructor of the parent class, here Box.

0
Prasad Kharkar On

add below in your NewBox class.

public NewBox(String name){
    super(name);
}

Now NewBox becomes

public class NewBox extends Box{
    public NewBox(String name){
        super(name);
    }

    public void calculateVolume(int length, int width, int height){
        System.out.println("Volume = " + (length*width*height));
    }
}