Access Specifiers: Public, Private, Protected, ProtectedFriend

VB.NET के 5 access doors: Public (सब), Private (अपनी class), Protected (सिर्फ़ derived classes), Friend (पूरा project), Protected Friend (दोनों में से कोई): एक specifier wider है और एक meaning narrower है उससे जो Java ने सिखाया था।

10 min read · 9 cards · 2 checks

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


Theory

Cost Price किसे देखने को मिले?

Mehta Uncle का एक business secret है cost price: उन्होंने वह साबुन कितने में खरीदा जो वह 40 में बेचते हैं। ShopKeeper को इसे store करना है (profit reports को यह चाहिए), पर इसे कौन पढ़ सके?

Till का form? हाँ: same project, family। कोई plugin DLL जो कोई अगले साल install करे? बिल्कुल नहीं।

Java ने आपको class, package और children पर keyed 4 access levels दिए थे। VB.NET के पास भी class, project और children पर keyed levels हैं, 2 renames और 1 genuinely different meaning के साथ। Differences वही हैं जहाँ exams aim करते हैं।

At a glance

5 Doors, सबसे Narrow पहले

Specifierकौन अंदर आता हैShopKeeper Use
Privateसिर्फ़ declaring class_price backing field
ProtectedClass + derived classes SIRFकिसी future DiscountedItem child के लिए Helper
FriendSame project (assembly) में कुछ भीCostPrice: shop का code, किसी और का नहीं
Protected FriendDerived classes OR same project (union)Children और in-house tools दोनों के लिए hooks
Publicसब, हर जगहName, Price, LineTotal

Practical

Item, इसके Doors Labelled के साथ

Public Class Item
    Private _price As Decimal            ' this class only
    Friend CostPrice As Decimal          ' whole ShopKeeper project
    Protected Sub MarkRepriced()         ' me + my derived classes
        ' audit note for children like DiscountedItem
    End Sub

    Public Property Price As Decimal
        Get
            Return _price
        End Get
        Set(value As Decimal)
            If value >= 0 Then _price = value
        End Set
    End Property
End Class

' Same project, in the till form:      itm.CostPrice = 32D   ' legal (Friend)
' A derived class calling MarkRepriced()                     ' legal (Protected)
' An external DLL reading itm.CostPrice     ' COMPILE ERROR: wrong assembly
' Anyone:  itm._price = 5                   ' COMPILE ERROR: Private

Theory

2 Renames और 1 असली Difference

BCA403 के Java से mapping:

  • Private और Public: identical twins, कुछ नया नहीं
  • Friend Java के package-private का role निभाता है, boundary assembly (compiled project, .exe या .dll) पर खींची जाती है, एक package folder की जगह
  • Protected असली difference है: VB का Protected सिर्फ़ derived classes admit करता है। Java का protected पूरे package को भी admit करता था। Java की definition को एक VB answer में ले आना ऐसी access claim करता है जो exist नहीं करती
  • Protected Friend union है (derived OR same assembly), जो उसके सबसे करीब आता है जो Java का protected असल में था

Quiz

Item का एक Friend member SAME ShopKeeper project की एक दूसरी class से access किया जाता है, जो Item से inherit नहीं करती। क्या होता है?

  1. Compile error: Friend को एक inheritance relationship चाहिए
  2. काम करता है: Friend same project (assembly) के किसी भी code को admit करता है
  3. सिर्फ़ तभी काम करता है जब दूसरी class same source file में हो
  4. Line execute होने पर runtime security exception
Show the answer

काम करता है: Friend same project (assembly) के किसी भी code को admit करता है

Friend की boundary assembly है: ShopKeeper project में compile हुई हर class family है, inheritance हो या न हो। Option A Protected describe करता है, पड़ोस वाला door, और इन दोनों को mix करना इस topic का classic slip है। Option C एक file rule invent करता है जो VB के पास नहीं है; source-file layout access को कभी affect नहीं करता। Option D ग़लत phase है: Java की तरह, BCA403 की तरह, access control पूरी तरह compile time पर settle होता है। एक-line memory: Friend = मेरा project, Protected = मेरे children।

Think first

Union Door

Protected Friend 2 specifiers combine करता है। precisely काम कीजिए कौन ऐसे member को access कर सकता है: yes cases और वह एक important no case list कीजिए, फिर tap कीजिए।

Show the answer

Yes: same assembly में कहीं भी code (Friend वाला half, inheritance ज़रूरी नहीं), AND किसी भी assembly में derived classes (Protected वाला half: एक outside DLL जो Item Inherits करे फिर भी अंदर आता है)। Important no: एक अलग assembly में एक unrelated class: न family, न project, दोनों doors बंद। यह permissions का एक OR है, तो Protected Friend दोनों में से किसी एक part से WIDER है: Public से पहले सबसे wide door, और इस सवाल का answer कि "कौन सा VB specifier Java के protected जैसा सबसे ज़्यादा behave करता है?"

Watch out

तीन Specifier Slips

एक VB answer में Java के protected को quote करना: VB का Protected बाकी project include NAHI करता; लिखिए "class और इसके derived classes सिर्फ़"।

Friend को 2 classes के बीच friendship की तरह पढ़ना: यह C++ के friend declaration जैसा नहीं है जो एक specific class को नाम देता है; यह एक blanket same-assembly grant है।

Defaults भूल जाना: एक class के अंदर एक plain Dim field Private है; बिना specifier declared एक class Friend है। Silence के meanings होते हैं; उन्हें जानिए।

Theory

एक Architect की तरह Doors चुनिए

हर member के लिए practical recipe जो आप declare करते हैं: Private से शुरू कीजिए; सिर्फ़ तभी Protected तक widen कीजिए जब किसी child class को genuinely चाहिए हो; in-project plumbing के लिए Friend इस्तेमाल कीजिए (CostPrice); उस surface के लिए Public reserve कीजिए जिसे आप happily document करेंगे। वह progression खुद encapsulation policy IS है, और 2 pages आगे वाला pillar lesson इसे formally नाम देता है। पर अभी, family pronouns आते हैं: Me, MyBase और वह एक जो Java के पास कभी नहीं था, MyClass।

Summary

Key takeaways

  • पाँच specifiers: Private (class), Protected (class + derived only), Friend (same assembly/project), Protected Friend (इन दोनों का union), Public (हर जगह)।
  • Friend .NET का package-private है, assembly boundary पर खींचा गया।
  • VB का Protected Java से NARROWER है: सिर्फ़ derived classes, कोई project-mates नहीं।
  • Protected Friend = Protected OR Friend: Java के protected का सबसे करीबी match।
  • Access violations compile time पर fail होते हैं।
  • Defaults: class में Dim fields Private हैं; unspecified classes Friend हैं।
  • Memory hook: Friend = मेरा project, Protected = मेरे children, Protected Friend = दोनों में से कोई।

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

Access Specifiers: Public, Private, Protected, ProtectedFriend · .NET Programming · Gri-Learn