Java super Keyword In Hindi

📔 : Java 🔗

Inheritance से लेकर अभी तक आपने कई examples में आपने super keyword का use होते हुए देखा होगा। हालाँकि super keyword को लेकर अभी भी आप confuse हैं तो कोई बात नहीं। इस topic में आप super keyword के बारे में ही पढ़ेंगे।


Java programming language में super एक predefined keyword है जिसका का use Inheritance में parent class के non static variables और methods को access करने के लिए किया जाता है। जब आप किसी class को inherit करके sub class का instance बनाते हैं , तो इसकी parent class का भी instance implicitly create हो जाता है जिसे super variable से refer हैं।


super keyword का use मुख्यता 3 तरीके से use करते हैं -

  1. super keyword का use immediately parent class के instance variable को access करने के लिए। instance variables means : non static variables.

  2. super keyword का use immediately parent class के instance methods को access करने के लिए। instance methods means : non static methods.

  3. और immediately parent class के constructors को करने के लिए भी super keyword का use किया जाता है।

Access Parent Class Instance Variables Through super

File : B.java

CopyFullscreenClose FullscreenRun
class A {
  // non static(instance) variable
  String message = "Message from class A";
}

public class B extends A {
  // create a non static method so that we can use super.
  void printMessage() {
    System.out.println(super.message);
  }
  public static void main(String[] args) {
    B b =  new B();
    b.printMessage();
  }
}
Output
javac B.java
java B
Message from class A

super & this दोनों ही class instance को represent करते हैं इसलिए ध्यान रहे आप इन्हे किसी instance (Non - Static) method में ही access कर सकते हैं। क्योंकि Static methods class instance का part नहीं होते हैं। इसीलिए ऊपर दिए गए example में super का use करने के लिए printMessage() method define किया है।

Access Parent Class Instance Methods Through super

File : B.java

CopyFullscreenClose FullscreenRun
class A {
  // non static(instance) method
  void message() {
     System.out.println("Message from class A.");
  }
}

public class B extends A {
  // override method. 
  void message() {
     System.out.println("Message from class B.");
  }
  
  void printMessage() {
     this.message(); // current class method.
     super.message();  // parent class method.
  }
  public static void main(String[] args) {
    B b =  new B();
    b.printMessage();
  }
}
Output
javac B.java
java B
Message from class B.
Message from class A.

Use super to Invoke Parent Class Constructor

आपने देखा होगा कि किसी भी child class का object बनाते समय parent class का constructor by default run हो जाता था जो कि order में सबसे पहले था। मतलब सबसे पहले parent class का constructor run होता है उसके बाद child class का।

लेकिन कई जगह हमें ये order change करने की भी need पड़ सकती है , इसके लिए आप super() का use parent class का constructor invoke करने के लिए कर सकते हैं।

File : B.java

CopyFullscreenClose FullscreenRun
class A {
  A() {
    System.out.println("A class constructor.");
  }
}

public class B extends A {
  B() {
    System.out.println("Class B constructor");
    // here call parent class constructor.
    super();
  }
  public static void main(String[] args) {
    B b =  new B();
  }
}
Output
javac B.java
java B
Class B constructor
A class constructor.

example में आप देख सकते हैं कि किस तरह से parent class का constructor last में call हुआ है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers