S06L08 – विरासत 01- प्रोग्रामिंग में विरासत को समझना


5. Inheritance के लिए Java Program Example


नीचे एक sample Java code snippet दिया गया है जो दर्शाता है कि एक vehicle-based application में Inheritance कैसे काम करता है:


6. चरण-दर-चरण Code व्याख्या


The Vehicle Class:

  • सामान्य properties शामिल हैं: engine, wheels, seats, fuel tank, और lights।
  • इसका constructor इन attributes को initialize करता है।
  • displaySpecs() method इन सामान्य विनिर्देशों को print करता है।

The Bike Class:

  • Vehicle से super() method का उपयोग करके सामान्य attributes को initialize करता है।
  • unique property “handle” और displayBikeSpecs() method जोड़ता है ताकि सभी specifications दिखाई जा सकें।

The Car and Truck Classes:

  • Vehicle को extend करते हैं और अतिरिक्त unique attributes शामिल करते हैं।
  • प्रत्येक के पास एक संबंधित display method (displayCarSpecs() और displayTruckSpecs()) होता है जो inherited और unique properties दोनों को print करता है।

Main Class:

  • Bike, Car, और Truck के objects instantiate करता है।
  • प्रत्येक vehicle के specifications को print करने के लिए display methods को call करता है, जिससे यह प्रदर्शित होता है कि Inheritance code redundancy को कम करने में कितना प्रभावी है।

7. निष्कर्ष


इस eBook में, हमने Java में vehicle classes के एक real-world उदाहरण का उपयोग करके Inheritance के concept का अध्ययन किया। हमने सीखा कि कैसे Vehicle class एक Parent Class के रूप में कार्य करती है जो common properties को धारण करती है, जबकि Bike, Car, और Truck Classes इन properties को inherit करके उन्हें unique attributes के साथ extend करती हैं। दिया गया code स्पष्ट रूप से दर्शाता है कि Inheritance कैसे reusability को बढ़ावा देता है और object-oriented design को streamlined बनाता है, जिससे code duplication कम होता है और maintenance सरल हो जाता है।

यदि आप Java programming में अपनी यात्रा की शुरुआत कर रहे हैं या OOP के मूल सिद्धांतों की समझ को मजबूत करना चाहते हैं, तो Inheritance में महारत हासिल करना अनिवार्य है। अपने भविष्य के projects में साफ, scalable, और manageable code को लागू करने के लिए इस guide का संदर्भ बिंदु के रूप में उपयोग करें।

SEO Optimized Keywords: inheritance, Java inheritance, vehicle class, object-oriented programming, Java tutorial, beginner programming, Java code, inheritance diagram, OOP fundamentals

हैप्पी कोडिंग!






Share your love