Me, MyBase and MyClass keywords

Me એ this છે, MyBase એ super છે, અને MyClass એ એક છે જે Java ક્યારેય નહોતું: તે call ને current class ની own version પર pins કરે છે, ignore કરીને જે child override કર્યું હોય.

11 min read · 9 cards · 2 checks

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


Theory

ત્રણ pronouns, એક family business

ShopKeeper ને staff records મળે છે: Person parent, Cashier child. immediately old BCA403 questions return થાય છે: constructor parameter name ને field name થી કેવી રીતે tell કરે છે? child parent ના constructor ને કેવી રીતે call કરે છે? override parent ના version ને કેવી રીતે borrow કરે છે?

Java એ this અને super સાથે answer આપ્યું હતું. VB.NET એ Me અને MyBase સાથે answer આપે છે, પછી third pronoun ને add કરે છે જે Java ક્યારેય નહોતું: MyClass, જેનો one trick exam questions અને occasionally real arguments ને win કરે છે.

Theory

Me અને MyBase: direct translations

Me એ current object છે:

Me.Name = name same parameter-field shadow ને untangle કરે છે જે title = title એ BCA403 માં create કર્યું હતું; અને this જેવું, Me Shared (static) context માં illegal છે, જ્યાં કોઈ current object exist નથી કરતું.

MyBase એ parent છે:

MyBase.New(name) parent constructor ને call કરે છે, child ના Sub New નું first 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 નું 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 actually pins કરે છે

Cashier object પર c.Intro() ને follow કરો.

Line 1, Describe(): normal (virtual) call. object really Cashier છે, Describe Overridable છે અને overridden છે, એટલે Cashier નું version runs થાય છે. (Me.Describe() exactly same કરતું; Me dispatch ને change નથી કરતું.)

Line 2, MyClass.Describe(): MyClass નો અર્થ છે આ class માં લખાયેલું version, Person, object really શું છે તેના regardless. Person નું version runs થાય છે.

એટલે MyClass એ promise છે: આ call child ના override દ્વારા ક્યારેય redirected નહીં થાય. Use કરો જ્યારે method ની correctness તેના OWN class ના behaviour પર depend કરે છે.

Quiz

Dim c As New Cashier("Riya") : c.Intro() 2 message boxes શું કહે છે, order માં?

  1. Person: Riya, પછી Cashier: Riya
  2. Cashier: Riya, પછી Person: Riya
  3. Cashier: Riya, twice
  4. Person: Riya, twice
Show the answer

Cashier: Riya, પછી Person: Riya

Intro નો first call virtual છે, એટલે runtime type wins કરે છે: Cashier: Riya. second ને MyClass દ્વારા Person ના own Describe પર pinned કરવામાં આવ્યું છે: Person: Riya. Option C એ છે જે તમે get કરો છો જો તમે forget કરો કે MyClass exists કરે છે અને બંને calls ને virtual તરીકે treat કરો; option D ને BOTH calls pinned ની જરૂર પડતી, પણ plain calls VB માં virtually dispatch થાય છે એકવાર method Overridable/Overrides હોય. જો આ ordering slippery feel થાય, BCA403 rule ને પહેલા re-run કરો (object ની type decide કરે છે) અને પછી MyClass ને apply કરો as single exception જે તમે top પર bolt કરો છો.

Think first

Me.Describe() vs MyClass.Describe()

બંને Person ની અંદરથી self-calls જેવા look કરે છે. precise difference ને state કરો, અને name કરો કે કયું one Java express કરી શકતું હતું.

Show the answer

Me.Describe() current object પર virtual call છે: જો તે object Cashier હોય override સાથે, override runs થાય છે; bare Describe() લખવા જેવું identical છે, અને Java ના this.describe() સાથે identical છે. MyClass.Describe() object ની runtime type ને ignore કરે છે અને Person ની own implementation ને run કરે છે, always. Java ની પાસે તે pin માટે કોઈ syntax નથી (super UP જાય છે, પણ કંઈ નથી કહેતું my-own-class-regardless-of-overrides). exam sentence: Me object ને follow કરે છે, MyClass call ને લખનાર class ને follow કરે છે.

Watch out

3 keyword traps

Shared context માં Me: Shared members class ને belong કરે છે; કોઈ object નહીં, no Me: compile error, same as BCA403 નું this-in-static.

MyBase.New first નથી: child constructor માં તેની ઉપર કોઈ પણ statement compilation ને kill કરે છે, અને તેને entirely omit કરવાથી VB no-arg MyBase.New() ને insert કરે છે, જે fail થાય છે જો parent પાસે એક ન હોય: Person અહીં does.

MyBase ફક્ત એક level up skips કરે છે: MyBase.MyBase exist નથી કરતું; તમે grandparent chain પર pogo નથી કરી શકતા.

Theory

trio ને file કરો exam માટે અને BCA405-02 માટે

notes ની margin માં mapping ને once write કરો: Me = this, MyBase = super, MyClass = pinned self (no Java twin), અને note કરો recurring first-line rule ને parent constructors માટે જે હવે તમારે 3 languages માં follow કર્યું છે. Dart, BCA405-02 માં waiting, તમને this અને super ને yet again hand કરશે. next lesson full OOP trio (abstraction, encapsulation, polymorphism) ને assemble કરે છે VB terms માં, including Overridable surprise જે આ lesson quietly leaned on.

Summary

Key takeaways

  • Me = current object: shadow-untangling (Me.Name = name), Shared context માં illegal.
  • MyBase = parent: child constructor ની first line માં MyBase.New(args); parent ના version માટે MyBase.Method().
  • MyClass call ને current class ની own implementation પર pins કરે છે, child overrides ને ignore કરીને.
  • Plain calls અને Me-calls virtual રહે છે: runtime object નું override wins કરે છે.
  • Traced: Cashier પર, Intro() Cashier: Riya (virtual) પછી Person: Riya (pinned) print કરે છે.
  • MyBase exactly one level up પહોંચે છે; no chaining.
  • Memory hook: Me object ને follow કરે છે, MyBase up look કરે છે, MyClass mirror માં look કરે છે.

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