Illustrate the uses of 'this' and 'super' keywords

Ram Pothuraju
this” Keyword:

     Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. We can use this anywhere a reference to an object of the current class’s type is permitted.

From above example:

     B(int i, int j, int k) // parameterized constructor
            {
                        this.x=i;
                        this.y=j;
                        this.z=k;
            }

“super” keyword:
            super has two general forms: i) The first calls the superclass’s constructor. ii) super used to access a member of the superclass that has been hidden by a member of a subclass.

From above example:

B(int i, int j, int k) // parameterized constructor
            {
                        super(i,j);
                        z=k;
            }

Class A{
int i;
}
class B extends A{
int i;
B(int i1,int j1)
{
   super.i=i1;
   i=j1;
}
}


Tags

Post a Comment

0Comments

Post a Comment (0)