java继承改变函数形参,Java中的类继承问题,带和不带参数的构造函数

这篇博客探讨了在Java中子类继承父类时遇到的构造函数问题。作者指出,子类必须调用父类的构造函数来初始化继承的属性。当父类只有带参数的构造函数时,子类在没有明确调用super()时,编译器会默认添加一个无参的super(),但这会导致错误,因为父类没有这样的构造函数。解决方案是子类在构造函数中显式调用父类的带参数构造函数。
摘要由CSDN通过智能技术生成

I'm learning Java (2nd year IT student) and I'm having a little problem. With inheritance to be precise. Here's the code:

class Bazowa

{

public Bazowa(int i)

{

System.out.println("konstruktor bazowy 1");

}

public Bazowa(int j, int k)

{

System.out.println("konstruktor bazowy 2");

}

}

class Pochodna extends Bazowa

{

/* Pochodna()

{

System.out.println("konstruktor pochodny bez parametru");

} */

Pochodna(int i)

{

super(i);

System.out.println("konstruktor pochodny z parametrem");

}

}

So, the Pochodna class extends the Bazowa class, and my exercise is to make a superclass that has only constructors with parameters and a subclass that has both types (with and without).

When I comment the first constructor in Pochodna class, everything works fine, but I don't really know how to make it work without commenting that part. I guess that I have to somehow call the constructor from the first one, but don't have an idea how to do that.

Any help would be appreciated,

Paul

解决方案

Your first constructor from Pochodna calls by default super(), a constructor which you do not have in Bazowa.

You should either call one of the base constructors with 1 or 2 params in Pochodna(), or create a constructor with no parameters in your base class.

EDIT: Since you said you are learning Java, I will add some extra explanations to my answer.

Every class must have a constructor, so when you do not declare one explicitly, the compiler does so for you, creating a default constructor with no parameters. This constructor won’t be added if YOU declare constructors explicitly.

In inheritance, the child class is a “specialization” of the parent. That means that the child class contains the attributes and behavior of the parent class and extends on them. But you do not declare the parent elements again (unless you really want to overwrite stuff). So, when you create an instance of the child, somehow the elements taken from the parent must also be initialized. For this you have the super(...) construct.

The first thing that must be in a child constructor is a call to super(...) so that the elements taken from the parent are properly initialized before the child tries to do something with them (you can also have a call to another of child’s constructor this(...) – in this case, the last child constructor in the calling chain will call super(...) ).

Because of this, the compiler will again add a default call to super() – with no parameters – for you when you do not do so yourself in the child.

In the first constructor of Pochodna, since you did not call super(i) or super(j, k) yourself, a call to super() was placed by default. But in the parent class you explicitly specified constructors, so the default was not created by the compiler. And from here the exception, you end up calling a constructor that does not exist.

Hope this makes it easier to learn Inheritance. Cheers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值