CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS J2EE JAVA DETAILS
Question :
How are this() and super() used with constructors?
Category Java Interview Questions
Rating (3.0) By 13 users
Added on 10/13/2004
Views 3036
Rate it!
Answers:

this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.



From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation. Here's another Rectangle class, with a different implementation from the one in the Objects section.
public class Rectangle {
private int x, y;
private int width, height;

public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments.
If present, the invocation of another constructor must be the first line in the constructor.


Super()


With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.

This could be a problem, if there is a constructor in the superclass that does a lot of necessary work. It looks like you might have to repeat all that work in the subclass! This could be a real problem if you don't have the source code to the superclass, and don't know how it works, or if the constructor in the superclass initializes private member variables that you don't even have access to in the subclass!

Obviously, there has to be some fix for this, and there is. It involves the special variable, super(). As the very first statement in a constructor, you can use super to call a constructor from the superclass.



Posted by: Amit Rane    

Contact Amit Rane Contact Amit Rane

1.this is keyword is used to resolve the conflict between local and instance variable.
2.this is keyword is used to acess all the members of same class.
3.this() should also be used in the first line of the constuctor i.e why this() & super()cannot be used in the same constructor.
4.this is keyword which refers to the current object getting created.
5.this is also used for constructor chaining-Invoking one constructor from another constructor.
6.We can do constructor chaining with this() & super().this() will create the chaining among the constructors within the same class & super will create in super classes.
Eg:
class A
{
A()
{
this(1,"s");
System.out.println("1st costructor");

}
A(int i,string s)
{
this(i,s,true);
System.out.println("2nd costructor");

}
A(int i,string s,boolean b)
{
System.out.println("3rd costructor");
}
}
Output:
3rd costructor
2nd costructor
1st costructor




Posted by: Arundhati .V.Nelli    

Contact Arundhati .V.Nelli Contact Arundhati .V.Nelli

If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    How is it possible for two String objects with identical values not to be equal under the == operator?
    View Answer
    How does multithreading take place on a computer with a single CPU?
    View Answer
    What restrictions are placed on method overloading?
    View Answer
    What restrictions are placed on method overriding?
    View Answer
    What happens when a thread cannot acquire a lock on an object?
    View Answer
    What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
    View Answer
    What classes of exceptions may be caught by a catch clause?
    View Answer
    What is the purpose of finalization?
    View Answer
    What is synchronization and why is it important?
    View Answer
    What are synchronized methods and synchronized statements?
    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL Java Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/360/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

    1.06
    Cache = 0 Seconds