जावा नेस्टेड और इनर क्लास (उदाहरण के साथ)

इस ट्यूटोरियल में, आप उदाहरणों की मदद से जावा और उसके प्रकारों में नेस्टेड क्लास के बारे में जानेंगे।

जावा में, आप एक वर्ग को दूसरी कक्षा में परिभाषित कर सकते हैं। ऐसे वर्ग के रूप में जाना जाता है nested class। उदाहरण के लिए,

 class OuterClass ( //… class NestedClass ( //… ) )

दो प्रकार के नेस्टेड वर्ग हैं जिन्हें आप जावा में बना सकते हैं।

  • गैर-स्थिर नेस्टेड वर्ग (आंतरिक वर्ग)
  • स्टेटिक नेस्टेड क्लास

अनुशंसित पढ़ने :

  • जावा एक्सेस संशोधक
  • जावा स्टेटिक कीवर्ड

आइए पहले गैर-स्थिर नेस्टेड कक्षाओं को देखें।

नॉन-स्टेटिक नेस्टेड क्लास (इनर क्लास)

एक गैर-स्थिर नेस्टेड वर्ग एक अन्य वर्ग के भीतर एक वर्ग है। इसमें संलग्न वर्ग (बाहरी वर्ग) के सदस्यों की पहुँच है। यह आमतौर पर के रूप में जाना जाता है inner class

चूंकि inner classबाहरी वर्ग के भीतर मौजूद है, इसलिए आपको आंतरिक कक्षा को तुरंत करने के लिए, पहले बाहरी वर्ग को तुरंत बदलना होगा।

यहाँ एक उदाहरण है कि आप जावा में आंतरिक कक्षाओं की घोषणा कैसे कर सकते हैं।

उदाहरण 1: भीतरी वर्ग

 class CPU ( double price; // nested class class Processor( // members of nested class double cores; String manufacturer; double getCache()( return 4.3; ) ) // nested protected class protected class RAM( // members of protected nested class double memory; String manufacturer; double getClockSpeed()( return 5.5; ) ) ) public class Main ( public static void main(String() args) ( // create object of Outer class CPU CPU cpu = new CPU(); // create an object of inner class Processor using outer class CPU.Processor processor = cpu.new Processor(); // create an object of inner class RAM using outer class CPU CPU.RAM ram = cpu.new RAM(); System.out.println("Processor Cache = " + processor.getCache()); System.out.println("Ram Clock speed = " + ram.getClockSpeed()); ) )

आउटपुट :

 प्रोसेसर कैश = 4.3 राम क्लॉक स्पीड = 5.5

उपरोक्त कार्यक्रम में, दो नेस्टेड वर्ग हैं: बाहरी वर्ग के अंदर प्रोसेसर और रैम: सीपीयू। हम आंतरिक वर्ग को संरक्षित घोषित कर सकते हैं। इसलिए, हमने रैम वर्ग को संरक्षित घोषित किया है।

मुख्य वर्ग के अंदर,

  • हमने पहले सीपीयू नामक एक बाहरी वर्ग सीपीयू का एक उदाहरण बनाया।
  • बाहरी वर्ग के उदाहरण का उपयोग करते हुए, हमने तब आंतरिक कक्षाओं की वस्तुओं का निर्माण किया:
     CPU.Processor processor = cpu.new Processor; CPU.RAM ram = cpu.new RAM();

नोट : हम .बाहरी वर्ग का उपयोग करके आंतरिक वर्ग का एक उदाहरण बनाने के लिए डॉट ( ) ऑपरेटर का उपयोग करते हैं।

भीतर के वर्ग के भीतर बाहरी वर्ग के सदस्यों तक पहुँचना

हम इस कीवर्ड का उपयोग करके बाहरी वर्ग के सदस्यों तक पहुँच सकते हैं। यदि आप इस कीवर्ड के बारे में सीखना चाहते हैं, तो जावा इस कीवर्ड पर जाएँ।

उदाहरण 2: सदस्यों तक पहुँचना

 class Car ( String carName; String carType; // assign values using constructor public Car(String name, String type) ( this.carName = name; this.carType = type; ) // private method private String getCarName() ( return this.carName; ) // inner class class Engine ( String engineType; void setEngine() ( // Accessing the carType property of Car if(Car.this.carType.equals("4WD"))( // Invoking method getCarName() of Car if(Car.this.getCarName().equals("Crysler")) ( this.engineType = "Smaller"; ) else ( this.engineType = "Bigger"; ) )else( this.engineType = "Bigger"; ) ) String getEngineType()( return this.engineType; ) ) ) public class Main ( public static void main(String() args) ( // create an object of the outer class Car Car car1 = new Car("Mazda", "8WD"); // create an object of inner class using the outer class Car.Engine engine = car1.new Engine(); engine.setEngine(); System.out.println("Engine Type for 8WD= " + engine.getEngineType()); Car car2 = new Car("Crysler", "4WD"); Car.Engine c2engine = car2.new Engine(); c2engine.setEngine(); System.out.println("Engine Type for 4WD = " + c2engine.getEngineType()); ) )

आउटपुट :

 8WD के लिए इंजन प्रकार = 4WD = छोटे के लिए बड़ा इंजन प्रकार

उपरोक्त कार्यक्रम में, हमारे पास बाहरी वर्ग कार के अंदर इंजन नामक आंतरिक वर्ग है। यहाँ, लाइन को नोटिस करें,

 if(Car.this.carType.equals("4WD")) (… )

हम thisबाहरी वर्ग के कार टाइप वेरिएबल तक पहुंचने के लिए कीवर्ड का उपयोग कर रहे हैं । आपने देखा होगा कि this.carTypeहमने उपयोग करने के बजाय उपयोग किया है Car.this.carType

ऐसा इसलिए है क्योंकि अगर हमने बाहरी वर्ग की कार के नाम का उल्लेख नहीं किया है, तो thisकीवर्ड आंतरिक वर्ग के अंदर सदस्य का प्रतिनिधित्व करेगा।

इसी तरह, हम आंतरिक वर्ग से बाहरी वर्ग की विधि तक भी पहुंच बना रहे हैं।

 if (Car.this.getCarName().equals("Crysler") (… )

यह ध्यान रखना महत्वपूर्ण है कि, हालांकि getCarName()यह एक privateविधि है, हम इसे आंतरिक वर्ग से एक्सेस करने में सक्षम हैं।

स्टेटिक नेस्टेड क्लास

जावा में, हम एक staticवर्ग को दूसरी कक्षा के अंदर भी परिभाषित कर सकते हैं । ऐसे वर्ग के रूप में जाना जाता है static nested class। स्टेटिक नेस्टेड क्लासेस को स्टैटिक इनर क्लास नहीं कहा जाता है।

आंतरिक वर्ग के विपरीत, एक स्थिर नेस्टेड वर्ग बाहरी वर्ग के सदस्य चर का उपयोग नहीं कर सकता है। यह इसलिए है क्योंकि स्थिर नेस्टेड वर्ग आपको बाहरी वर्ग का एक उदाहरण बनाने की आवश्यकता नहीं है।

 OuterClass.NestedClass obj = new OuterClass.NestedClass();

यहां, हम केवल बाहरी वर्ग के वर्ग नाम का उपयोग करके स्थिर नेस्टेड क्लास का एक ऑब्जेक्ट बना रहे हैं । इसलिए, बाहरी वर्ग का उपयोग करके संदर्भित नहीं किया जा सकता है OuterClass.this

उदाहरण 3: स्टेटिक इनर क्लास

 class MotherBoard ( // static nested class static class USB( int usb2 = 2; int usb3 = 1; int getTotalPorts()( return usb2 + usb3; ) ) ) public class Main ( public static void main(String() args) ( // create an object of the static nested class // using the name of the outer class MotherBoard.USB usb = new MotherBoard.USB(); System.out.println("Total Ports = " + usb.getTotalPorts()); ) )

आउटपुट :

 कुल पोर्ट्स = 3

उपरोक्त कार्यक्रम में, हमने क्लास मदरबोर्ड के अंदर USB नामक एक स्थिर वर्ग बनाया है। लाइन नोटिस करें,

 MotherBoard.USB usb = new MotherBoard.USB();

यहां, हम बाहरी वर्ग के नाम का उपयोग करके USB का एक ऑब्जेक्ट बना रहे हैं।

अब, देखते हैं कि यदि आप बाहरी वर्ग के सदस्यों तक पहुँचने की कोशिश करते हैं तो क्या होगा:

उदाहरण 4: स्टेटिक इनर क्लास के अंदर बाहरी वर्ग के सदस्यों तक पहुँचना

 class MotherBoard ( String model; public MotherBoard(String model) ( this.model = model; ) // static nested class static class USB( int usb2 = 2; int usb3 = 1; int getTotalPorts()( // accessing the variable model of the outer classs if(MotherBoard.this.model.equals("MSI")) ( return 4; ) else ( return usb2 + usb3; ) ) ) ) public class Main ( public static void main(String() args) ( // create an object of the static nested class MotherBoard.USB usb = new MotherBoard.USB(); System.out.println("Total Ports = " + usb.getTotalPorts()); ) )

जब हम प्रोग्राम को चलाने का प्रयास करते हैं, तो हमें एक त्रुटि मिलेगी:

 त्रुटि: गैर-स्थिर चर यह एक स्थिर संदर्भ से संदर्भित नहीं किया जा सकता है

This is because we are not using the object of the outer class to create an object of the inner class. Hence, there is no reference to the outer class Motherboard stored in Motherboard.this.

Key Points to Remember

  • Java treats the inner class as a regular member of a class. They are just like methods and variables declared inside a class.
  • Since inner classes are members of the outer class, you can apply any access modifiers like private, protected to your inner class which is not possible in normal classes.
  • Since the nested class is a member of its enclosing outer class, you can use the dot (.) notation to access the nested class and its members.
  • Using the nested class will make your code more readable and provide better encapsulation.
  • गैर-स्थिर नेस्टेड कक्षाएं (आंतरिक कक्षाएं) बाहरी / संलग्न वर्ग के अन्य सदस्यों तक पहुंच रखती हैं, भले ही उन्हें निजी घोषित किया गया हो।

दिलचस्प लेख...