3

I am a novice to Java and just learning OOP concepts. Please review my code. I am getting the following error.- Implicit Super Constructor is undefined.

class BoxSuper
{
    int height;
    int length;
    int width;

    BoxSuper(BoxSuper obj)
    {
        height=obj.height;
        length=obj.length;
        width=obj.width;
    }
    BoxSuper(int a,int b,int c)
    {
        height=a;
        length=b;
        width=c;
    }
    BoxSuper(int val)
    {
        height=length=width=val;
    }
    int volume()
    {
        return height*length*width;
    }
}

class BoxSub extends BoxSuper
{
    int weight;
    BoxSub(int a,int b,int c,int d)
    {
        height=a;
        length=b;
        width=c;
        weight=d;
    }
}
10
  • I am unable to add my code.How do i add it? Commented Feb 4, 2012 at 18:32
  • Click edit below your question and cut-and-paste the code. Highlight the code and press ctrl-K to format it properly. Commented Feb 4, 2012 at 18:32
  • Note also that there's a Markdown FAQ / editing help, accessible via the orange ? when you edit. Commented Feb 4, 2012 at 18:34
  • 1
    Hi all.. I added the code finally.. My main was causing the problem to edit the code. Commented Feb 4, 2012 at 18:48
  • 1
    possible duplicate of Java error: Implicit super constructor is undefined for default constructor Commented Sep 13, 2013 at 6:45

2 Answers 2

11

You are receiving this error because BoxSuper does not have a no-arg constructor. During your constructor call in BoxSub, if you do not define the super constructor call Java tries to automatically call the no-arg super() constructor.

Either define a super constructor call in BoxSuper like so:

class BoxSub extends BoxSuper
{
    int weight;
    BoxSub(int a,int b,int c,int d)
    {
        super(a, b, c);
        weight=d;
    }
}

or define a no-arg constructor in BoxSuper:

class BoxSuper
{
    int height;
    int length;
    int width;

    BoxSuper(){}
...
Sign up to request clarification or add additional context in comments.

1 Comment

If I were you, I would instead use the super(a,b,c) call and utilize the constructor in BoxSuper you already have defined. Well, good luck with learning.
7

A constructor always calls the super constructor, always. If no explicit call to the super constructor is made, then the compiler tries to set it up so that it will call the default parameter-less constructor. If a default parameter-less constructor doesn't exist, a compilation error as you're seeing is shown and compilation will fail.

The solution in your case is to explicitly call the appropriate super constructor as the first line of your Box's constructor, and this makes perfect sense too if you think about it since you want to initialize the super with a, b, and c just as written in its constructor:

class BoxSub extends BoxSuper
{
    int weight;
    BoxSub(int a,int b,int c,int d)
    {
        super(a, b, c);
        // height=a;
        // length=b;
        // width=c;
        weight=d;
    }
}

4 Comments

Tried this .. its not working... But here my concern is sub class is inheriting all the variables and methods from super class in my code.Why is it still throwing undefined constructor?
@Venk: hm, "it's not working" doesn't tell us much. You've still got a bug and will need to show us your updated code attempt and any error messages you see.
@Venk, What do you mean it's not working? What error are you seeing?
The same error that the default constructor is not visible

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.