Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 29

this and super

this और super क्या हैं

this और super दो ख़ास keywords हैं। this मौजूदा object को दर्शाता है, और super parent class को। ये आपको सही field, method या constructor access करने में मदद करते हैं, ख़ासकर जब class और उसके parent के बीच नाम टकराएँ।

💡 this = यह object। super = इसके ऊपर की parent class।

this — मौजूदा object

class Student {
    String name;
    void setName(String name) {
        this.name = name;   // this.name = field, name = parameter
    }
}

जब parameter का नाम field जैसा हो, name parameter को और this.name object के field को मतलब देता है। this इस अस्पष्टता को साफ़ करता है — setters और constructors में बहुत आम।

constructors को this()

class Box {
    int w, h;
    Box() { this(1, 1); }          // दूसरा constructor call करो
    Box(int w, int h) {
        this.w = w; this.h = h;
    }
}

this() उसी class का दूसरा constructor call करता है, उसका setup दोबारा इस्तेमाल करके। यह constructor का पहला statement होना चाहिए।

super — parent

class Animal {
    void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
    void sound() {
        super.sound();      // parent का version call करो
        System.out.println("Woof");
    }
}
Some sound
Woof

super.sound() parent की method call करता है भले child ने उसे override किया हो। यह child को parent के व्यवहार में जोड़ने देता है, पूरी तरह बदलने के बजाय।

constructors में super()

class Person {
    String name;
    Person(String name) { this.name = name; }
}
class Student extends Person {
    int roll;
    Student(String name, int roll) {
        super(name);        // parent constructor call करो
        this.roll = roll;
    }
}

super(name) पहले parent का constructor चलाता है, तो parent अपना हिस्सा (name) set करता है, फिर child अपना (roll) जोड़ता है। यह constructor की पहली line होनी चाहिए।

this बनाम super

बातthissuper
दर्शाता हैमौजूदा objectParent class
Field accessthis.xsuper.x
Method callthis.m()super.m()
Constructorthis(...)super(...)

आम गलतियाँ

  • नाम टकराने पर this भूलना। name = name; parameter को खुद पर assign करता है।
  • super() पहले नहीं। यह child constructor का पहला statement होना चाहिए।
  • this() और super() दोनों इस्तेमाल करना। सिर्फ़ एक पहला हो सकता है, तो दोनों इस्तेमाल नहीं कर सकते।
  • Inheritance बिना super की उम्मीद। super सिर्फ़ subclass में मायने रखता है।

सारांश

  • this मौजूदा object को दर्शाता है; super parent class को।
  • जब parameter field का नाम साझा करे तब this.field = field इस्तेमाल कीजिए।
  • this() उसी class का दूसरा constructor call करता है।
  • super.method() parent का version; super(...) उसका constructor call करता है।
  • this() और super() दोनों पहली line होने चाहिए, तो सिर्फ़ एक इस्तेमाल कीजिए।

आगे, इन्हें काम में देखिए inheritance से, या objects set कीजिए constructors से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Java में this keyword क्या करता है?
this मौजूदा object को दर्शाता है — वो जिस पर method या constructor काम कर रहा है। यह सबसे उपयोगी है एक ही नाम वाले field और parameter को अलग करने में, जैसे this.name = name;।
super keyword क्या करता है?
super parent (superclass) को दर्शाता है। आप इसे parent का constructor super(...) से call करने को, या child द्वारा override किया parent field या method super.show() से access करने को इस्तेमाल करते हैं।
Constructor में this.name = name क्यों इस्तेमाल करूँ?
जब parameter का नाम field जैसा हो, अकेला name parameter को मतलब देता है। this.name object का field मतलब देता है, तो this.name = name; parameter को field में साफ़ store करता है।
this() और super() में क्या अंतर है?
this() उसी class का दूसरा constructor call करता है। super() parent class का constructor call करता है। दोनों constructor का पहला statement होने चाहिए, तो आप दोनों साथ इस्तेमाल नहीं कर सकते।
super() अपने-आप कब call होता है?
अगर आप खुद super() न लिखें, Java अपने-आप पहले parent का no-argument constructor call करती है। आप super(...) स्पष्ट रूप से तभी लिखते हैं जब parent constructor को values भेजनी हों।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicOverloading vs Overriding आपकी प्रगति: 29 / 61 (48%) · Lesson 30 / 61 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें