• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Are this() and super() same?

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.

     
    Ranch Hand
    Posts: 80
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    indra negi
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Anastasia. Got it.
     
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.

     
    Sheriff
    Posts: 9697
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 )
     
    I'm not sure if I approve of this interruption. But this tiny ad checks out:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic