Access Specifiers: Public, Private, Protected, ProtectedFriend

VB.NET ના 5 access doors: Public (everyone), Private (own class), Protected (ફક્ત derived classes), Friend (આખું project), Protected Friend (બેમાંથી કોઈ): એક specifier Java એ તમને શીખવ્યું તેના કરતાં wider અને એક meaning narrower.

10 min read · 9 cards · 2 checks

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


Theory

કોણ cost price ને જોઈ શકે છે?

Mehta Uncle નો એક business secret એ cost price છે: શું તેણે soap માટે ચૂકવ્યું જે તે 40 પર વેચે છે. ShopKeeper એ તેને store કરવું જોઈએ (profit reports ને તેની જરૂર છે), પણ કોણ તેને read કરી શકે છે?

till form? હા: same project, family. કોઈ next year install કરે તે plugin DLL? bilkul નહીં.

Java એ તમને 4 access levels આપ્યા હતા જે class, package અને children પર keyed હતા. VB.NET પાસે પણ levels છે જે class, project અને children પર keyed છે, 2 renames અને 1 genuinely different meaning સાથે. differences એ છે જ્યાં exams aim કરે છે.

At a glance

5 doors, narrowest first

Specifierકોણ અંદર જાય છેShopKeeper use
Privateફક્ત declaring class_price backing field
Protectedફક્ત class + derived classesભવિષ્યના DiscountedItem child માટે Helper
Friendસમાન 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            ' ફક્ત આ class
    Friend CostPrice As Decimal          ' આખું ShopKeeper project
    Protected Sub MarkRepriced()         ' હું + મારી derived classes
        ' DiscountedItem જેવા children માટે audit note
    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, till form માં:      itm.CostPrice = 32D   ' legal (Friend)
' Derived class જે MarkRepriced() ને call કરે છે                     ' legal (Protected)
' External DLL જે itm.CostPrice ને read કરે છે     ' COMPILE ERROR: wrong assembly
' કોઈપણ:  itm._price = 5                   ' COMPILE ERROR: Private

Theory

2 renames અને 1 real difference

BCA403 ના Java થી mapping:

  • Private અને Public: identical twins, કંઈ new નથી
  • Friend Java ના package-private ની role play કરે છે, boundary assembly (compiled project, .exe અથવા .dll) પર drawn છે package folder ને બદલે
  • Protected એ real difference છે: VB નું Protected ફક્ત derived classes ને admit કરે છે. Java નું protected આખા package ને ALSO admit કરતું હતું. Java ની definition ને VB answer માં carry કરવું એ access ને claim કરે છે જે exist નથી કરતું
  • Protected Friend એ union છે (derived OR same assembly), જે land થાય છે closest ત્યાં જે Java નું protected actually હતું

Quiz

Item ના Friend member ને SAME ShopKeeper project ની બીજી class માંથી access કરવામાં આવે છે, જે Item થી inherit થતી નથી. શું થાય છે?

  1. Compile error: Friend ને inheritance relationship જોઈએ છે
  2. It works: Friend same project (assembly) માં કોઈપણ code ને admit કરે છે
  3. It works ફક્ત જો બીજી class same source file માં હોય
  4. Runtime security exception જ્યારે line execute થાય છે
Show the answer

It works: Friend same project (assembly) માં કોઈપણ code ને admit કરે છે

Friend ની boundary એ assembly છે: ShopKeeper project માં compiled દરેક class family છે, inheritance કે નહીં. Option A Protected ને describe કરે છે, neighbouring door, અને 2 ને mix up કરવું એ આ topic નો classic slip છે. Option C file rule ને invent કરે છે જે VB પાસે નથી; source-file layout ક્યારેય access ને affect નથી કરતું. Option D wrong phase છે: Java જેવું, BCA403 જેવું, access control entirely compile time પર settle થાય છે. One-line memory: Friend = my project, Protected = my children.

Think first

union door

Protected Friend 2 specifiers ને combine કરે છે. precisely work out કે કોણ આવા member ને access કરી શકે છે: yes cases ને list કરો અને એક important no case, પછી tap કરો.

Show the answer

Yes: same assembly માં કોઈપણ code (Friend half, inheritance જરૂરી નથી), AND કોઈપણ assembly માં derived classes (Protected half: બહારની DLL જે Item ને Inherits કરે છે તે પણ અંદર જાય છે). important no: different assembly માં unrelated class: neither family nor project, બંને doors shut. એ permissions નો OR છે, એટલે Protected Friend એ બંને parts કરતાં WIDER છે એકલા: Public પહેલાં widest door, અને જવાબ "કયો VB specifier Java ના protected જેવું behave કરે છે?"

Watch out

ત્રણ specifier slips

VB answer માં Java ના protected ને Quote કરવું: VB Protected બાકીના project ને include નથી કરતું; લખો "the class and its derived classes only".

Friend ને 2 classes વચ્ચે friendship તરીકે Read કરવું: તે C++ ની friend declaration નથી જે specific class ને name કરે છે; તે blanket same-assembly grant છે.

defaults ને Forget કરવું: class ની અંદર plain Dim field એ Private છે; specifier વગર declared class એ Friend છે. silence ના meanings છે; એને જાણો.

Theory

architect જેમ doors ને choose કરો

દરેક member માટે practical recipe જે તમે declare કરો છો: Private થી start કરો; Protected સુધી widen કરો ફક્ત જ્યારે child class genuinely તેને need કરે; in-project plumbing માટે Friend ને use કરો (CostPrice); Public ને તે surface માટે reserve કરો જે તમે happily document કરી શકો. એ progression IS encapsulation policy છે, અને pillar lesson 2 pages ahead તેને formally names કરે છે. next, though, come family pronouns: Me, MyBase અને એક જે Java ક્યારેય નહોતું, MyClass.

Summary

Key takeaways

  • પાંચ specifiers: Private (class), Protected (class + ફક્ત derived), Friend (same assembly/project), Protected Friend (તે 2 નું union), Public (દરેક જગ્યાએ).
  • Friend એ .NET નું package-private છે, assembly boundary પર drawn.
  • VB Protected એ Java કરતાં NARROWER છે: ફક્ત derived classes, કોઈ project-mates નહીં.
  • Protected Friend = Protected OR Friend: Java ના protected નો closest match.
  • Access violations compile time પર fail થાય છે.
  • Defaults: class માં Dim fields Private છે; unspecified classes Friend છે.
  • Memory hook: Friend = my project, Protected = my children, Protected Friend = either.

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