Me, MyBase and MyClass keywords

Me यानी this है, MyBase यानी super है, और MyClass वह है जो Java के पास कभी नहीं था: यह एक call को current class के अपने version पर pin कर देता है, किसी child ने जो भी override किया हो उसे ignore करते हुए।

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

तीन Pronouns, एक Family Business

ShopKeeper को staff records मिलते हैं: एक Person parent, एक Cashier child। तुरंत पुराने BCA403 questions वापस आते हैं: एक constructor parameter name को field name से कैसे बताए? एक child parent के constructor को कैसे call करे? एक override parent का version कैसे borrow करे?

Java ने this और super से answer दिया था। VB.NET Me और MyBase से answer देता है, फिर एक तीसरा pronoun जोड़ता है जो Java के पास कभी नहीं था: MyClass, जिसकी एक trick exam questions और कभी-कभी असली arguments जीतती है।

Theory

Me और MyBase: Direct Translations

Me current object है:

Me.Name = name वही parameter-field shadow untangle करता है जो BCA403 में title = title बनाता था; और this की तरह, Me Shared (static) context में illegal है, जहाँ कोई current object exist ही नहीं करता।

MyBase parent है:

MyBase.New(name) parent constructor को call करता है, child के Sub New का पहला statement (base-first birth order rule, unchanged)।

MyBase.Describe() एक override के अंदर से parent का version run करता है, extend-not-replace pattern के लिए।

Practical

MyClass Experiment

Public Class Person
    Protected Name As String

    Public Sub New(ByVal name As String)
        Me.Name = name                  ' Me: shadow untangled
    End Sub

    Public Overridable Sub Describe()
        MsgBox("Person: " & Name)
    End Sub

    Public Sub Intro()
        Describe()            ' virtual: an override wins
        MyClass.Describe()    ' pinned: Person's own, always
    End Sub
End Class

Public Class Cashier
    Inherits Person

    Public Sub New(ByVal name As String)
        MyBase.New(name)                ' parent constructor, line 1
    End Sub

    Public Overrides Sub Describe()
        MsgBox("Cashier: " & Name)
    End Sub
End Class

' Dim c As New Cashier("Riya")
' c.Intro()   ' box 1: ?   box 2: ?

Theory

MyClass असल में क्या Pin करता है

Cashier object पर c.Intro() follow कीजिए।

Line 1, Describe(): एक normal (virtual) call। Object असल में एक Cashier है, Describe Overridable और overridden है, तो Cashier का version run होता है। (Me.Describe() exactly वही करता; Me dispatch नहीं बदलता।)

Line 2, MyClass.Describe(): MyClass का मतलब है वह version जो THIS class में लिखा गया, Person में, चाहे object असल में कुछ भी हो। Person का version run होता है।

तो MyClass एक promise है: यह call कभी किसी child के override से redirect नहीं होगी। इसे इस्तेमाल कीजिए जब एक method की correctness इसकी OWN class के behaviour पर depend करे।

Quiz

Dim c As New Cashier("Riya") : c.Intro() दोनों message boxes क्रम में क्या कहते हैं?

  1. Person: Riya, फिर Cashier: Riya
  2. Cashier: Riya, फिर Person: Riya
  3. Cashier: Riya, दो बार
  4. Person: Riya, दो बार
Show the answer

Cashier: Riya, फिर Person: Riya

Intro की पहली call virtual है, तो runtime type जीतता है: Cashier: Riya। दूसरी MyClass से Person की अपनी Describe पर pin है: Person: Riya। Option C वह है जो आपको मिलता अगर आप भूल जाते कि MyClass exist करता है और दोनों calls को virtual treat करते; option D को दोनों calls pinned होने की ज़रूरत होती, पर VB में plain calls virtually dispatch होती हैं एक बार जब method Overridable/Overrides हो जाए। अगर यह ordering slippery लगे, पहले BCA403 का rule फिर से चलाइए (object का type decide करता है) और फिर MyClass को उस single exception की तरह apply कीजिए जो आप ऊपर bolt करते हैं।

Think first

Me.Describe() बनाम MyClass.Describe()

Person के अंदर से दोनों self-calls जैसे दिखते हैं। Precise difference state कीजिए, और नाम बताइए कौन सा Java express कर सकता था।

Show the answer

Me.Describe() current object पर एक virtual call है: अगर वह object एक override वाला Cashier है, override run होता है; बिल्कुल वैसे ही जैसे Describe() bare लिखना, और Java के this.describe() जैसा identical। MyClass.Describe() object के runtime type को ignore करता है और Person का अपना implementation हमेशा run करता है। Java के पास उस pin के लिए कोई syntax नहीं है (super ऊपर जाता है, पर कुछ भी my-own-class-regardless-of-overrides नहीं कहता)। Exam sentence: Me object को follow करता है, MyClass उस class को follow करता है जिसने call लिखी।

Watch out

3 Keyword Traps

Shared context में Me: Shared members class के होते हैं; कोई object नहीं, कोई Me नहीं: compile error, BCA403 के this-in-static जैसा।

MyBase.New पहला नहीं है: child constructor में इसके ऊपर कोई भी statement compilation मार देता है, और इसे पूरी तरह omit करना VB को एक no-arg MyBase.New() insert करने पर मजबूर करता है, जो fail होता है अगर parent के पास एक न हो: Person यहाँ नहीं रखता।

MyBase सिर्फ़ एक level ऊपर जाता है: MyBase.MyBase exist नहीं करता; आप grandparent chain को pogo करके ऊपर नहीं जा सकते।

Theory

Exam और BCA405-02 के लिए Trio File कीजिए

अपने notes के margin में एक बार mapping लिखिए: Me = this, MyBase = super, MyClass = pinned self (कोई Java twin नहीं), और उस recurring first-line rule पर नोट कीजिए जो अब 3 languages के through parent constructors के लिए follow करता आया है। Dart, जो BCA405-02 में wait कर रही है, आपको यह और super फिर से देगी। अगला lesson पूरी OOP trio (abstraction, encapsulation, polymorphism) को VB terms में assemble करता है, including वह Overridable surprise जिस पर यह lesson चुपचाप lean करता था।

Summary

Key takeaways

  • Me = current object: shadow-untangling (Me.Name = name), Shared context में illegal।
  • MyBase = parent: MyBase.New(args) child constructor की पहली line; MyBase.Method() parent के version के लिए।
  • MyClass एक call को current class के अपने implementation पर pin करता है, child overrides ignore करते हुए।
  • Plain calls और Me-calls virtual रहती हैं: runtime object का override जीतता है।
  • Traced: एक Cashier पर, Intro() Cashier: Riya (virtual) फिर Person: Riya (pinned) print करता है।
  • MyBase exactly एक level ऊपर पहुँचता है; कोई chaining नहीं।
  • Memory hook: Me object को follow करता है, MyBase ऊपर देखता है, MyClass mirror में देखता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Object Oriented Programming

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Me, MyBase and MyClass keywords · .NET Programming · Gri-Learn