Me, MyBase and MyClass keywords

Me is this, MyBase is super, and MyClass is the one Java never had: it pins a call to the current class's own version, ignoring whatever a child overrode.

11 min read · 9 cards · 2 checks

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


Theory

Three pronouns, one family business

ShopKeeper gets staff records: a Person parent, a Cashier child. Immediately the old BCA403 questions return: how does a constructor tell the parameter name from the field name? how does a child call the parent's constructor? how does an override borrow the parent's version?

Java answered with this and super. VB.NET answers with Me and MyBase, then adds a third pronoun Java never had: MyClass, whose one trick wins exam questions and occasionally real arguments.

Theory

Me and MyBase: direct translations

Me is the current object:

Me.Name = name untangles the same parameter-field shadow that title = title created in BCA403; and like this, Me is illegal in Shared (static) context, where no current object exists.

MyBase is the parent:

MyBase.New(name) calls the parent constructor, first statement of the child's Sub New (the base-first birth order rule, unchanged).

MyBase.Describe() runs the parent's version from inside an override, for the extend-not-replace pattern.

Practical

The 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

What MyClass actually pins

Follow c.Intro() on the Cashier object.

Line 1, Describe(): a normal (virtual) call. The object is really a Cashier, Describe is Overridable and overridden, so Cashier's version runs. (Me.Describe() would do exactly the same; Me does not change dispatch.)

Line 2, MyClass.Describe(): MyClass means the version written in THIS class, Person, no matter what the object really is. Person's version runs.

So MyClass is a promise: this call will never be redirected by a child's override. Use it when a method's correctness depends on its OWN class's behaviour.

Quiz

Dim c As New Cashier("Riya") : c.Intro() What do the 2 message boxes say, in order?

  1. Person: Riya, then Cashier: Riya
  2. Cashier: Riya, then Person: Riya
  3. Cashier: Riya, twice
  4. Person: Riya, twice
Show the answer

Cashier: Riya, then Person: Riya

Intro's first call is virtual, so the runtime type wins: Cashier: Riya. The second is pinned by MyClass to Person's own Describe: Person: Riya. Option C is what you get if you forget MyClass exists and treat both calls as virtual; option D would need BOTH calls pinned, but plain calls in VB dispatch virtually once a method is Overridable/Overrides. If this ordering feels slippery, re-run the BCA403 rule first (object's type decides) and then apply MyClass as the single exception you bolt on top.

Think first

Me.Describe() vs MyClass.Describe()

Both look like self-calls from inside Person. State the precise difference, and name which one Java could express.

Show the answer

Me.Describe() is a virtual call on the current object: if that object is a Cashier with an override, the override runs; identical to writing Describe() bare, and identical to Java's this.describe(). MyClass.Describe() ignores the object's runtime type and runs Person's own implementation, always. Java has no syntax for that pin (super goes UP, but nothing says my-own-class-regardless-of-overrides). Exam sentence: Me follows the object, MyClass follows the class that wrote the call.

Watch out

The 3 keyword traps

Me in Shared context: Shared members belong to the class; no object, no Me: compile error, same as BCA403's this-in-static.

MyBase.New not first: any statement above it in a child constructor kills compilation, and omitting it entirely makes VB insert a no-arg MyBase.New(), which fails if the parent lacks one: Person here does.

MyBase skips one level only: MyBase.MyBase does not exist; you cannot pogo up the grandparent chain.

Theory

File the trio for the exam and for BCA405-02

Write the mapping once in the margin of your notes: Me = this, MyBase = super, MyClass = pinned self (no Java twin), and note the recurring first-line rule for parent constructors that has now followed you through 3 languages. Dart, waiting in BCA405-02, will hand you this and super yet again. Next lesson assembles the full OOP trio (abstraction, encapsulation, polymorphism) in VB terms, including the Overridable surprise this lesson quietly leaned on.

Summary

Key takeaways

  • Me = the current object: shadow-untangling (Me.Name = name), illegal in Shared context.
  • MyBase = the parent: MyBase.New(args) first line of a child constructor; MyBase.Method() for the parent's version.
  • MyClass pins a call to the current class's own implementation, ignoring child overrides.
  • Plain calls and Me-calls stay virtual: the runtime object's override wins.
  • Traced: on a Cashier, Intro() prints Cashier: Riya (virtual) then Person: Riya (pinned).
  • MyBase reaches exactly one level up; no chaining.
  • Memory hook: Me follows the object, MyBase looks up, MyClass looks in the 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