超类和这种方法属于哪类?

时间:2014-02-09 18:56:36

标签: java

这些方法super()和this()属于哪个类?以及在我们的类构造函数中调用这些方法的类实例(对象)?他们是来自Object类吗?他们是否调用当前实例this.super()?

1 个答案:

答案 0 :(得分:2)

super()用于调用超类no-arg构造函数。如果没有指定超类,则调用java.lang.Object的构造函数(隐式超类)。 / p>

this()用于从另一个重载的构造函数中调用no-arg构造函数。

注意: -

1) super() and this () can be called from contructors only.They cannot be treated as methods.
2)A constructor will have to call super() at one point of time otherwise stack will explode.
3)They are the first statement in the body of a constructor and thus only one can be used at a time.