Java继承错误:隐式超级构造函数未定义 [英] Java Inheritance error: Implicit Super Constructor is undefined

查看:472
本文介绍了Java继承错误:隐式超级构造函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,只是学习OOP的概念。请检查我的代码。我收到以下错误.- 隐藏的超级构造函数未定义。

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;
    }
}


推荐答案

正在接收此错误,因为BoxSuper没有无参构造函数。在构造函数调用BoxSub的过程中,如果没有定义超级构造函数,Java会尝试自动调用no-arg super()构造函数。

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.

调用BoxSuper如下:

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;
    }
}

或在BoxSuper中定义一个无参数构造函数: / p>

or define a no-arg constructor in BoxSuper:

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

    BoxSuper(){}
...

这篇关于Java继承错误:隐式超级构造函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆