Difference between “this” and“super” keywords in Java

前端 未结 9 1829
你的背包
你的背包 2020-11-27 03:00

What is the difference between the keywords this and super?

Both are used to access constructors of class right? Can any of you explain?

9条回答
  •  隐瞒了意图╮
    2020-11-27 03:39

    this is a reference to the object typed as the current class, and super is a reference to the object typed as its parent class.

    In the constructor, this() calls a constructor defined in the current class. super() calls a constructor defined in the parent class. The constructor may be defined in any parent class, but it will refer to the one overridden closest to the current class. Calls to other constructors in this way may only be done as the first line in a constructor.

    Calling methods works the same way. Calling this.method() calls a method defined in the current class where super.method() will call the same method as defined in the parent class.

提交回复
热议问题