Super keyword in java in Hindi – जावा में सुपर कीवर्ड क्या है?

Hello दोस्तों! आज हम इस पोस्ट में Super keyword in java in Hindi (जावा में सुपर कीवर्ड क्या है?) के बारें में विस्तार से पढेंगे और इसके example को भी देखेंगे, तो चलिए शुरू करते हैं.

Super Keyword in Java in Hindi

जावा में, Super Keyword एक reference variable है जिसका प्रयोग parent class objects को refer करने के लिए किया जाता है.

दूसरे शब्दों में कहें तो, “Super keyword का प्रयोग superclass members को access करने के लिए sub class में किया जाता है”

जब भी हम sub class का instance बनाते है तो parent class का instance अपने आप create हो जाता है. जिसे super reference variable कहा जाता है.

अगर आपको super कीवर्ड को समझना है तो सबसे पहले आपको inheritance के बारें में जानकारी होनी चाहिए.

 Super Keyword का प्रयोग–

  • इसका प्रयोग parent class के data members को access करने के लिए किया जाता है ( जब parent और child class के पास एक ही name के member हों.)
  • इसका उपयोग parent class की method को call करने के लिए किया जाता है.
  • और इसका प्रयोग parent class के default और parameterized constructor को call करने के लिए किया जाता है.

Super का प्रयोग variable के साथ

जब parent class और child class में same (समान) variable होता है तो parent class के variable को access करने के लिए हम super का प्रयोग करते हैं.

अब इसको एक उदाहरण के द्वारा आसानी से समझ सकते हैं. इसका example नीचे दिया गया है:-

class ParentClass
{ 
int maxValue = 110; 
} 
class ChildClass extends ParentClass
{ 
int maxValue = 170; 

void displayNumber() 
{ 		
System.out.println("Maximum Value: " + super.maxValue); 
} 
} 

class Test 
{ 
public static void main(String[] args) 
{ 
ChildClass value1 = new ChildClass(); 
value1.displayNumber(); 
} 
}  

इसका आउटपुट:-
Maximum value: 110

ऊपर दिए गये example में, दोनों base class और sub class के पास maxValue नाम का variable है. हमने super keyword का प्रयोग करके base class के MaxValue को access किया है.

Super का प्रयोग methods के साथ

इसका प्रयोग तब किया जाता है जब हम parent class की method को call करना चाहते है. जब base class और sub class में समान method हो तो इसका प्रयोग किया जाता है. अर्थात् इसका प्रयोग तब किया जाता है जब method को sub class के द्वारा override किया जाता है.

example:- नीचे आपको इसका उदाहरण दिया गया है जिससे आपको यह आसानी से समझ में आ जाएगा.

class Parentclass
{
//Overridden method
void display(){
System.out.println("This is Parent class method");
}
}
class Subclass extends Parentclass
{
 //Overriding method
 void display(){
System.out.println("This is Child class method");
   }
void printMsg(){
//This would call Overriding method
display();
//This would call Overridden method
super.display();
   }
public static void main(String args[]){		
Subclass obj= new Subclass();
obj.printMsg(); 
   }
}

इसका आउटपुट:-
This is Parent class method
This is Child class method.

ऊपर दिए गये उदाहरण में आपने देखा कि जब हम केवल display() को call कर रहे हैं तो subclass का display() invoke हो रहा है. परन्तु जब हम super का प्रयोग display() के साथ कर रहें है तो parentcalss का display() भी invoke हुआ.

Super का प्रयोग Constructors के साथ

super keyword का प्रयोग parent class के constructor को access करने के लिए भी किया जा सकता है. सबसे महत्वपूर्ण बात यह है कि यह default और parameterized दोनों तरह के constructors को call कर सकता है.

example:- नीचे आपको इसका उदाहरण दिया गया है. जिससे आपको यह और अच्छे से समझ में आएगा.

class SuperClass 
{ 
SuperClass () 
{ 
System.out.println("Super class Constructor"); 
} 
} 
class SubClass extends SuperClass { 
SubClass () 
{ 
super(); 
System.out.println("Sub class Constructor"); 
} 
} 

class Test 
{ 
public static void main(String[] args) 
{ 
SubClass s = new SubClass (); 
} 
}

इसका आउटपुट:-
Super class Constructor
Sub class Constructor

ऊपर दिए गये example में हमने super keyword का प्रयोग करके super class के constructor को call किया है.

इसका references:

निवेदन:- अगर आपके लिए यह पोस्ट useful रही हो तो इसे अपने friends और classmates के साथ अवश्य share कीजिये और आपके java और अन्य subjects से सम्बन्धित कोई question हो तो नीचे कमेंट के द्वारा बताइए. thanks.

2 thoughts on “Super keyword in java in Hindi – जावा में सुपर कीवर्ड क्या है?”

Leave a Comment