• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are this() and super() same?

 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain me whether this() and super() same. What I know about this is it refers to the currently running object.

I have two questions for the below given code:

  • What will be the difference if I replace this() by super() in the following code given in K&B book?
  • What will happen if i remove this and super both?





  • What I have understood is that this() will give a call to the no args constructor of the parent class(Building) current object. What I have not understood is once that completes the control will go to which line the one after the this() call(line 18) or to the no args constructor of the child class(line no. 14)? Please explain me.
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this() used to access to overloaded constructor in the same class.

    super() used to access super class constructors.

    Hope this helps
     
    indra negi
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good Answer.

    I have understood that what I am confused about is the flow of control mentioned in the code given above. Please explain that.

     
    Vamsi Mohan
    Ranch Hand
    Posts: 80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this keyword is used to access the object within the method or class and super is used to access the object of the superclass or constructor.
     
    Anastasia Sirotenko
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    indra negi
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Anastasia. Got it.
     
    Priyanka Tyagi
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    indra negi wrote:Please explain me whether this() and super() same. What I know about this is it refers to the currently running object.

    I have two questions for the below given code:

  • What will be the difference if I replace this() by super() in the following code given in K&B book?
  • What will happen if i remove this and super both?





  • What I have understood is that this() will give a call to the no args constructor of the parent class(Building) current object. What I have not understood is once that completes the control will go to which line the one after the this() call(line 18) or to the no args constructor of the child class(line no. 14)? Please explain me.
     
    Ankit Garg
    Sheriff
    Posts: 9519
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priyanka Tyagi wrote:
    What I have understood is that this() will give a call to the no args constructor of the parent class(Building) current object..


    I'm not sure about which this you are referring to.

    Situation 1: you are talking about this() on line 19
    Explanation: No, the this() call always calls another constructor of the same class (depending upon the arguments passed). In the program, when this() is called on line 19, then the no-arg constructor of House i.e. the one on line 14 will be called. Since there is no call to this() or super() in the constructor on line 14, so by default super() will be called i.e. the one on line 3. Then the control will return back to line 14, and then to line 20.

    Situation 2: you are talking about this() on line 7
    Explanation: the Building constructor with String parameter is never called, so that line is never executed (I'm sure you know this but just wanted to confirm )
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic