Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Monday, April 29, 2013

 Mybase keyword inheritance मे base class की overrided method को call करने के लिए use की जाती है। जब inheritance करते समय कभी भी base class की overrided method की आवश्यकता पड़ती है तब mybase keyword का use कर use call कर सकते हैं। जैसे given example मे जब Circle के area calculate किया जाता है साथ ही जब Cone का area calculate करते है तब Circle class के area को override करने के आवश्यकता पड़ती है इसमे base class (Circle) के area method को कॉल करने के लिए mybase का use किया जाता है।
 
Circle.vb

Public Class Circle
    Private cradius As Double
    Public Property Radius() As Double
        Get
            radius = cradius
        End Get
        Set(ByVal value As Double)
            If value < 0 Then
                msgbox("Wrong Input:")
            Else
                cradius = value
            End If
        End Set
    End Property

    Public Overridable Function Area() As Double
        Return Math.PI * Radius ^ 2
    End Function

  
End Class
Cone.vb 
Public Class Cone
    Inherits Circle
    Dim l As Double
    Public Property length() As Double
        Get
            length = l
        End Get
        Set(ByVal value As Double)
            l = value
        End Set
    End Property
    Public Overrides Function Area() As Double
        Return ((Math.PI * Radius * length) + MyBase.Area())
    End Function

End Class


Limitations of MyBase keyword: 
  • Mybase keyword केवल immediate base class के लिए use की जा सकता है।
  • Mybase real object नहीं है इसीलिए इसके द्वारा variable मे value को assign, procedure मे pass etc. नहीं कर सकते हैं।
  • Mybase को MustOverride base class के लिए use नहीं कर सकते हैं।
  • Mybase base class के अंदर प्रयोग नहीं की जा सकती है।
  • Mybase को friend member के लिए use नहीं किया जा सकता है।

Inheritance Modifiers: 

         Inheritance के लिए Visual basic .Net कुछ class level modifiers provide करता है। 
  • NotInheritable: class को ininheritable बनाने के लिए। ऐसी class को inherite नहीं किया जा सकता हैं।
  • MustInherit: इसका use करने पर class को base class मे use करना अनिवार्य हो जाता है।   इसे केवल base class की तरह ही use किया जा सकता है।
Oerriding Properties and Methods in Derived Class:

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

For Properties and methods of Base Class: 
  • Overridable: इसका use करने पर property या method को override किया जा सकता है।
  • NotOverridable: इसका use करने पर property या method को override नहीं किया जा सकता है।
  • MustOverride: इसका use करने पर property या method को override करना अनिवार्य हो जाता है।
For Properties and methods of Derived Class: 
  • Overrides: यह overridable property या method को override करते समय प्रयोग की जाती है।

VB.net मे सभी classes को inherite किया जा सकता है। लेकिन ऐसी सभी classes जिनमें की       NotInheritable keyword का use किया गया है inherite नहीं की जा सकती हैं। 
  • Classes को other projects से भी inherite किया जा सकता है।
  • VB.Net केवल Single और Multilevel inheritance को support करती है।
  • Inheritance करने के लिए Base और Derieved class दोनों के access type equal होने चाहिए तभी inheritance possible है।
 

 Inheritance VB.Net का एक महत्वपूर्ण feature है जो कि एक class को किसी अन्य class के features को use करने के facility provide करता है। यह पहले से बनी हुई class पर based नई class बनाने की सुविधा देता है। यह reusability को support करता हैं जहां किसी feature को जोड़ने या पुराने class के features को use करने के लिए उन्हे फिर से नहीं बनाना पड़ता है उन्हे inheritance के द्वारा पुरानी class से inherite कर नई class मे add कर लेते हैं। inherite कि जाने वाली class को base-class या parent class और inherite करने वाली class को derived या child class कहते हैं। 
Inheriting a Class: 
         Derived class मे base class को inherite करने के लिए Inherits keyword का use कर base class का नाम define कर देते हैं। inherite करने के बाद base class की सभी properties, methods, events, fields और constants को derived class मे use किया जा सकता है। 

DerivedCircle.vb

Public Class DerivedCircle    Inherits Circle     Public Function Circumfrence() As Double        Return 2 * math.pi * radius    End FunctionEnd Class
 
Form4.vb 
Public Class Form4
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim dc As New DerivedCircle        dc.Radius = Val(TextBox1.Text)        MsgBox("Area = " & dc.Area)        MsgBox("Circumfrence =" & dc.Circumfrence)    End Sub
  
End Class

Contact us

Name

Email *

Message *