Ads Sponsored By Google :)

Friday 25 January 2013

use of this and super keyword variable level in java with example


this keyword at variable level this keyword at variable level with example super keyword at variable level
super keyword at variable level with example this and super keyword in java this and super keyword in java with example use of this and super keyword at variable level in java use of this and super keyword variable level in java with example


 In java we can differentiate variable in a class by using

1) this at variable level
2) Super at variable level

1)  this At Variable level:
         
        Whenever the formal parameters and data members of the class are similar, jvm gets an ambiguity. In order to differ between formal parameters and data members of the class must be preceded by keyword “this”.

Syntax:
this.currentclassvariable;


 2)Super At variable level:
In order to differ between base class data member and derived class data members, In context of derived class, the data members of the base class must be preceded by keyword “super”.

Syntax:
Super.baseclassdatamember;


The Following Example Illustrate the Concept of this and super keyword at variable level
          
      // Test.java
      
        Class  Test
       {
           int a,b;
          void  Demo(int a,int b)
         {
            this.a=a; //Here it to differ the formal and data members
            this.b=b; //Here it to differ the formal and data members
          }
       }
      
   Class DerivedTest extends Test
{
    int a,b;
     void Dispaly()
  {
       a=super.a; //Here it to differ base and derived class variables
       b=98;
     System.out.println(a+"       "+b);
   }
    public static void main(String args[])
  {
    Test t1=new Test();
    Display();
    t1.Demo(90,304);
   }
}




No comments:

Post a Comment

LinkWithin