Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है?

Hello दोस्तों! आज हम इस पोस्ट में Nested Class in Java in Hindi (जावा में नेस्टेड क्लास क्या है?) के बारें में पढेंगे और इसके types और examples को भी देखेंगे. तो चलिए शुरू करते हैं:-

Nested Class in Java in Hindi

Java में, एक class को दूसरे class के अंदर define किया जा सकता है और वह class जिसे दूसरे क्लास के अंदर define किया जाता है उसे Nested Class कहते हैं.

Nested classes का मुख्य मकसद ऐसी classes को एक group में रखना होता है जिनका केवल एक जगह पर ही use होता है. इससे encapsulation बढ़ता है, और इससे program को read करना आसान होता है तथा इससे code को हम आसानी से maintain कर सकते हैं.

इसका syntax –  

class OuterClass {
    // ...
    class NestedClass {
        // ...
    }
}
  • nested class का scope इसके outer class के scope से bound (घिरा) रहता है. जैसा कि आप ऊपर के syntax में देख सकते हैं.
  • एक नेस्टेड क्लास, outer class के सभी members को access कर सकती है, (private member को भी). परन्तु outer class नेस्टेड क्लास के members को access नहीं कर सकती.
  • एक नेस्टेड क्लास, outer class का एक member भी होता है.
  • एक nested class को public, private या protected declare किया जा सकता है.

Types of Nested Class

इसके निम्नलिखित दो प्रकार होते हैं:-

  1. Static Nested Class – ये एक class के static members होते हैं.
  2. Non-static Nested Class (Inner class)ये एक class के non-static members होते हैं.

नीचे आप इसका चित्र देख सकते हैं:-

nested class in hindi

Static Nested Class in Hindi

जावा में, जब हम एक क्लास के अंदर एक static class को define करते हैं तो इस प्रकार की क्लास को static nested class कहते हैं. एक static nested class कभी भी outer class के member variables और methods को access नहीं कर सकता. इस restriction (बाधा) के कारण static nested class का प्रयोग बहुत ही कम किया जाता है.

Syntax-

class MyOuter {
   static class Nested_Demo {
   }
}

Non-static Nested Class (Inner Class) in Hindi

Non-static Nested Class को inner class भी कहा जाता है. यह nested class का सबसे महत्वपूर्ण प्रकार है. Inner Class, outer class के सभी members को access कर सकता है. Inner class को private भी declare किया जा सकता है, अगर एक बार हमने इसे private declare कर दिया तो हम इसे class के बाहर access नहीं कर सकते.

इसका example:-

class Outer_Demo {
   int num;
   
   // inner class
   private class Inner_Demo {
      public void print() {
         System.out.println("This is an inner class");
      }
   }
   
   // Accessing he inner class from the method within
   void display_Inner() {
      Inner_Demo inner = new Inner_Demo();
      inner.print();
   }
}
   
public class My_class {

   public static void main(String args[]) {
      // Instantiating the outer class 
      Outer_Demo outer = new Outer_Demo();
      
      // Accessing the display_Inner() method.
      outer.display_Inner();
   }
}

इसका आउटपुट:- This is an inner class.

ऊपर दिए गये उदाहरण में Outer_demo एक outer class है और Inner_demo() एक inner class है.

Inner Class के प्रकार

इसके दो प्रकार होते हैं:-

  1. Local inner class
  2. Anonymous inner class

Local inner class

local inner class को एक block के अंदर define किया जाता है. सामन्यतया, यह ब्लॉक method body होता है. local inner class, outer class के members नहीं होते हैं. ये उसी block से सम्बन्धित होते है जिसमें वे define हुए होते हैं.

इसका उदाहरण:-

public class localInner1{  
 private int data=30;//instance variable  
 void display(){  
  class Local{  
   void msg(){System.out.println(data);}  
  }  
  Local l=new Local();  
  l.msg();  
 }  
 public static void main(String args[]){  
  localInner1 obj=new localInner1();  
  obj.display();  
 }  
}  

आउटपुट:- 30

Anonymous inner class

वह inner class जिसे बिना किसी नाम के declare किया जाता है उसे anonymous inner class कहते हैं. इस प्रकार की classes को एक ही समय declare और instantiate किया जाता है. सामन्यतया इनका प्रयोग एक class या interface की method को override करने के लिए किया जाता है. इसका example निम्नलिखित है:-

example:

abstract class Person{  
  abstract void eat();  
}  
class TestAnonymousInner{  
 public static void main(String args[]){  
  Person p=new Person(){  
  void eat(){System.out.println("awesome nuts");}  
  };  
  p.eat();  
 }  
}  

आउटपुट:- awesome nuts.

Advantage of Nested Class in Hindi

इसके लाभ निम्नलिखित हैं:-

  • ये outer class के सभी members access कर सकते हैं.
  • यह encapsulation को बढाता है.
  • इससे code को आसानी से read और maintain किया जा सकता है.
  • यह code को optimize करता है अर्थात् हमें कम code लिखने की आवश्यकता होती है.

references:-https://www.tutorialspoint.com/java/java_innerclasses.htm

निवेदन:- आशा करता हूँ कि यह पोस्ट आपके लिए उपयोगी रही होगी. इसे अपने friends के साथ share कीजिये और आपके जो भी questions हैं उन्हें नीचे comment के माध्यम से बताइए. Thanks.

1 thought on “Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है?”

Leave a Comment