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

2019-01-07 06:49发布

What is the difference between the keywords this and super?

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

标签: java keyword
10条回答
我想做一个坏孩纸
2楼-- · 2019-01-07 07:07

super() & this()

  • super() - to call parent class constructor.
  • this() - to call same class constructor.

NOTE:

  • We can use super() and this() only in constructor not anywhere else, any attempt to do so will lead to compile-time error.

  • We have to keep either super() or this() as the first line of the constructor but NOT both simultaneously.

super & this keyword

  • super - to call parent class members(variables and methods).
  • this - to call same class members(variables and methods).

NOTE: We can use both of them anywhere in a class except static areas(static block or method), any attempt to do so will lead to compile-time error.

查看更多
Bombasti
3楼-- · 2019-01-07 07:09

this is used to access the methods and fields of the current object. For this reason, it has no meaning in static methods, for example.

super allows access to non-private methods and fields in the super-class, and to access constructors from within the class' constructors only.

查看更多
Emotional °昔
4楼-- · 2019-01-07 07:13

This almost appears to be a situation where a person has asked a question that everyone sees only one possible answer for. Yet the person calls each answer a non-answer, so everyone tries a different variant on the same answer since there appears to be only one way to answer it.

So, one person starts out with A is a class and B is its subclass. The next person starts out with C is a Class and B is its subclass. A third person answers it a little differently. A is a class and B extends A. Then a fourth one says A is A class that gets extended as B is defined. Or Animal is a class and Dog is a subclass. Or Nation is a class and China is a special case of Nation.

Or better yet, Man is a class, and Clark Kent is a subclass of Man. So, Superman...no...that doesn't work in terms of Java....

Well, it's at least another attempt to come up with a different description that nobody came up with that might explain things differently. Arggghhhh.

It seems to me that everyone's explanation worked except mine.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-07 07:15

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.

查看更多
登录 后发表回答