aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are this() and super() same? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are this() and super() same?" Watch "Are this() and super() same?" New topic
Author

Are this() and super() same?

indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68
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

    Joined: Jan 28, 2008
    Posts: 5575

    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

    Joined: Aug 12, 2009
    Posts: 68
    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

    Joined: Jun 20, 2009
    Posts: 80
    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.


    VAMSI MOHAN.V
    (SCJP5.0, IBM Rational Solution Designer, IBM Rational Architect, IBM RUP Designer)
    Anastasia Sirotenko
    Ranch Hand

    Joined: Jul 20, 2009
    Posts: 64


    [SCJP 6.0]
    indra negi
    Ranch Hand

    Joined: Aug 12, 2009
    Posts: 68
    Thanks Anastasia. Got it.
    Priyanka Tyagi
    Greenhorn

    Joined: Jan 09, 2007
    Posts: 6
    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

    Joined: Aug 03, 2008
    Posts: 9321
        
      17

    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 )


    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Are this() and super() same?