Inheritance in VB.Net

Inheritance

Inheritance, VB.Net का एक महत्वपूर्ण फीचर है जो कि एक class को किसी अन्य class के फीचर को प्रयोग करने की सुविधा प्रदान करता है। यह पहले से बनी हुई class पर based नई class बनाने की सुविधा देता है। यह reusability को support करता हैं जहां किसी feature को जोड़ने या पुराने class के features को प्रयोग करने के लिए उन्हे फिर से नहीं बनाना पड़ता है उन्हे inheritance के द्वारा पुरानी class से inherit कर नई class मे add कर लेते हैं। inherit कि जाने वाली class को base-class या parent class और inherit करने वाली class को derived या child class कहते हैं।

Inheriting a Class

Derived class मे base class को inherit करने के लिए Inherits keyword का use कर base class का नाम define कर देते हैं। inherit करने के बाद base class की सभी properties, methods, events, fields और constants को derived class मे use किया जा सकता है।

Inheritance Modifiers

Inheritance के लिए Visual basic .Net कुछ class level modifiers provide करता है।

  • NotInheritable: इसका प्रयोग करने पर class को inherit नहीं किया जा सकता हैं।
  • MustInherit: इसका use करने पर class को base class मे use करना अनिवार्य हो जाता है। इसे केवल base class की तरह ही use किया जा सकता है।

Overriding Properties and Methods in Derived Class

By default derived class, base class की सभी properties और methods को inherit कर लेती है। कभी-कभी Derived class मे inherit की गई property या method को अलग प्रकार से प्रयोग करने की आवश्यकता पड़ती है इसे overriding करना कहते है। इसके लिए VB.Net Base class और Derived class दोनों में use होने वाली properties और methods के लिए कुछ modifiers प्रदान करता है जिन्हें प्रयोग कर किसी भी method आदि की overriding को control कर सकते हैं। जो निम्नलिखित हैं।

Properties and methods of Base Class

  • Overridable: इसका use करने पर property या method को override किया जा सकता है।
  • NotOverridable: इसका use करने पर property या method को override नहीं किया जा सकता है।
  • MustOverride: इसका use करने पर property या method को override करना अनिवार्य हो जाता है।

Properties and methods of Derived Class

Overrides: यह overridable property या method को override करते समय प्रयोग की जाती है।

Limitations of Inheritance

VB.net मे सभी classes को inherit किया जा सकता है। लेकिन ऐसी सभी classes जिनमें NotInheritable keyword का use किया गया है inherit नहीं की जा सकती हैं।

  • Classes को other projects से भी inherit किया जा सकता है।
  • VB.Net केवल Single और Multilevel inheritance को support करती है।
  • Inheritance करने के लिए Base और Derived class दोनों के access type equal होने चाहिए तभी inheritance possible है।

error: Content is protected !!