What is this & super keyword in JAVA-hindi ?-vishmy1.blogspot.com

 this & super keyword in JAVA

super keyword & this keyword is the reserved keyword in JAVA. super keyword refers to the parent class of that class where a super keyword is used while this keyword refer to the current instance of a class.

super कीवर्ड और this कीवर्ड जावा में आरक्षित कीवर्ड हैं। super कीवर्ड उस वर्ग के मूल वर्ग को संदर्भित करता है जहां सुपर कीवर्ड का उपयोग किया जाता है जबकि this कीवर्ड किसी वर्ग की वर्तमान आवृत्ति का संदर्भ देता है।

this keyword in JAVA

In JAVA, this keyword is used as a reference variable that refers to a current object

JAVA में, this कीवर्ड का उपयोग एक संदर्भ चर(reference variable) के रूप में किया जाता है जो वर्तमान वस्तु(current object) को संदर्भित(refers) करता है।

It can be used to refer current class instance variable, It can be used to invoke(call) current class Constructor

इसका उपयोग वर्तमान वर्ग(current class) के उदाहरण चर(instance variable) को संदर्भित करने के लिए किया जा सकता है। इसका इस्तेमाल करंट क्लास के कंस्ट्रक्टर को इनवाइट(call) करने के लिए किया जा सकता है।

It can be passed as an argument with the method as well as Constructor

इसे विधि(method) के साथ-साथ कंस्ट्रक्टर(Constructor) के साथ एक तर्क(argument) के रूप में पारित(passed) किया जा सकता है।

ex :-

class test 
{
    int a;
  void show(int s)
    {
          this.a = s;
          System.out.println(a);
     }
    void display()
       { 
               this.show();
              
        }
       public static void main(String args[])
      {
            test t = new test(10);
            t.display();
       }
}


Use of this keyword in constructor overloading


class test 

{
    int x, y;

   test(int p, int q)

    {
          this.x = p;
          this.y = q;
     }
    void display()
    {
        System.out.println("value of x"+x);
        System.out.println("value of y"+y);
    }

   public static void main (String args[]) {
        
       test t= new test(10, 20);
       t.display(); 
         
      }

}


super keyword in JAVA

The super keyword in JAVA is a reference variable which is used to refer the parent class object, whenever you create an instance of the subclass, an instance of parent class is created. the super() can be used to invoke(call) parent class constructor.

JAVA में सुपर कीवर्ड एक संदर्भ चर(reference variable) है जिसका उपयोग मूल वर्ग(parent class) ऑब्जेक्ट(object) को संदर्भित(refer) करने के लिए किया जाता है, जब भी आप उपवर्ग(subclass) का एक उदाहरण बनाते हैं, तो मूल वर्ग का एक उदाहरण बनाया जाता है।  super ( ) का उपयोग पैरेंट क्लास कंस्ट्रक्टर(parent class constructor) को आमंत्रित(call) करने के लिए किया जा सकता है।

the super keyword use for calling parent class constructor with argument and without argument. it can also be used for calling methods from parent class.

superकीवर्ड का उपयोग parent class constructor को argumentऔर बिना argument के कॉल करने के लिए किया जाता है। इसका उपयोग parent class से कॉलिंग method के लिए भी किया जा सकता है।

ex :-

class test
{
    int x = 4;
}
class test1 extends test
{
     int y = 10;
     void show();
     {
          int z;
          z = y+super.x;
          System.out.println(z);
     }
 }
 public static void main(String args[])
{
     test1 t1 = new test1();
     t1.show();
}
}

Super keyword with constructor.

class test 
{
     test()
     {
         System.out.println("base class constructor :");
     }
     class test1 extends test
     {
           test1()
          {
              super();
              System.out.println("derived class constructor :");
          }
      public static void main(String args[])
       {
            test1 t1 = new test1();
       }
}
       
Working with super and this keyword together.

class test
{
   int x, y;
   test(int p, int q)
   {
          this.x=p;
          this.y=q;
    }
    void display()
    {
         System.out.println("value of x :"+x);
         System.out.println("value of y :"+y);
    }
 }
 class test1 extends test
{
       test1(int a, int b)
       {
            super(p, q);
       }
 public static void main(String args[])
 {
         test1 t1 = new test1(10, 20);
         t1.display();
}
}

      

Post a Comment

2 Comments

  1. check this post :
    http://technicalround.com/uncategorized/cursors-in-java/

    ReplyDelete