Google

Saturday, February 12, 2005

Use of this and super key words

Use of this and super key words
-------------------------------------------------------------

Constructors

Constructors are special methods which are used for creating an instance of a class and initializing that instance.

Consider the following example:

/*
* Created on Feb 12, 2005
*
*/
package deepak.learn.twelve_feb_five;

/**
* @author Deepak.Saini
*
*/
public class ConstructorExample {

int instanceVariable;

//Defining the no argument constructor
ConstructorExample(){
instanceVariable = 0;
}

public static void main(String args[]){

ConstructorExample obj = new ConstructorExample();
System.out.println("The value of instanceVariable is : " +
obj.instanceVariable);
}
}

Run the program:
java deepak.learn.twelve_feb_five.ConstructorExample

Output:

The value of instanceVariable is : 0

So when we instantiated the class ConstructorExample in its main method, a call was made to its no arg contructor and its instance variable instanceVariable was assigned a value of 0.

However, we can define more than one constructor.

Check out the following program:

/*
* Created on Feb 12, 2005
*/
package deepak.learn.twelve_feb_five;

/**
* @author Deepak.Saini
*
*/
public class ConstructorExample {
int instanceVariable;

//Defining the no argument constructor
ConstructorExample(){
instanceVariable = 0;
}

ConstructorExample(int instanceVariable){
this.instanceVariable = instanceVariable;
}

public static void main(String args[]){
ConstructorExample obj = new ConstructorExample(10);
System.out.println("The value of instanceVariable is : " + obj.instanceVariable);
}

}

Run the program:
java deepak.learn.twelve_feb_five.ConstructorExample

Output:

The value of instanceVariable is : 10

This time when we ran the program ConstructorExample, in place of no argument constructor, a call to the constrcutor with argument "instanceVariable" was called.

So a value of 10 was assigned to instanceVariable.

One thing to notice here is key word this.

We used the keyword this in the one argument constructor, because the name of the class variable and the argument to the constructor was same.

So in the line this.instanceVariable = instanceVariable

this.instanceVariable is the class variable and instanceVariable is the parameter to the constructor.

How to call one constructor from another?

We cannot explicitly invoke any constructor.
that is you cannot write obj.ConstructorExample();

This is illegal.

Check out the following program:

/*
* Created on Feb 12, 2005
*/
package deepak.learn.twelve_feb_five;

/**
* @author Deepak.Saini
*
*/
public class ConstructorExample {
int instanceVariable;

//Defining the no argument constructor
ConstructorExample(){
this(0);
}

ConstructorExample(int instanceVariable){
this.instanceVariable = instanceVariable;
}

public static void main(String args[]){
ConstructorExample obj = new ConstructorExample();
System.out.println("The value of instanceVariable is : " +
obj.instanceVariable);
}

}

Notice the statement this(0);

What we intend to do here is to call the one argument constructor from the no argument constructor. This we normally do when we have lot of duplicate code in the constructors so instead of duplicating the code. Just invoke the contructors from other constructors.

In our example, in the main method we are instatiating the class ConstructorExample with the statement ConstructorExample obj = new ConstructorExample();

So we have made a call to the no argument constructor.
Inside the no argument constructor, the statement this(0); makes a call to the one argument constructor.

Calls to super class constructor
---------------------------------------

Write a class SubClassConstructorExample which extends ConstructorExample.

package deepak.learn.twelve_feb_five;

/**
* @author Deepak.Saini
*/
public class SubClassConstructorExample extends ConstructorExample {

public SubClassConstructorExample() {

}
public SubClassConstructorExample(int instanceVariable) {

}

public static void main(String args[]){
SubClassConstructorExample obj = new SubClassConstructorExample(40);
System.out.println("The value of instanceVariable is : " +
obj.instanceVariable);
}

}

In this class we created an instance of SubClassConstructorExample with the following statement:

SubClassConstructorExample obj = new SubClassConstructorExample(40);

We passed 40 as a parameter to the one agrument constructor of SubClassConstructorExample class.

Now run the program. The output is as below
The value of instanceVariable is : 0

What???? We passed 40 and what was displayed is 0....Hey what happened here? Any ideas? No....

Ok. What happened here is that we made a call to one argument constructor of class
SubClassConstructorExample and internally, behind the scenes, a call was made to the no argument constructor of ConstructorExample class and as we saw earlier, in the no argument constructor of the class ConstructorExample, we initialized instanceVariable to 0.

Now the question is how to set the value of instanceVariable to 40?

We can solve this problem by modifying the one argument constructor of SubClassConstructorExample class as below

public SubClassConstructorExample(int instanceVariable) {
super(40);
}

With statement
super(40);

we have made a call to the one argument constructor of the super class. So if you make a call to the super class constructor using super, no call is made to the no argument constructor.

One thing to remember here is that the call to the super class constructor should be the first statement inside the sub class constructor.

Another important thing to remember here is that you cannot have both this() and super() statements inside your constructor. Because both the statements should be the first statement of the constructor so there is no way to write both the statements.

Thats is from my side on this topic. If you have any questions you can post a comment. I will definitley reply back

Recommended excercise:

Just play around with the constructors. Define different types of constructors and check the sequence in which they are called.

Happy learning
Deepak Saini

0 Comments:

Post a Comment

<< Home