Access Controls, Object creation

new એ 3 steps માં object બનાવે છે, અને 4 access levels (private, default, protected, public) decide કરે છે કે કોણ શેને touch કરી શકે છે: BookBridge નું copies field હવે lock મેળવે છે.

10 min read · 9 cards · 2 checks

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


Theory

કોઈપણ catalog ને vandalise કરી શકે છે

છેલ્લો lesson uncomfortable note પર end થયો હતો. program ની કોઈપણ જગ્યાએ, કોઈપણ code લખી શકે છે:

b1.copies = -50;

Minus 50 copies. અથવા prank: member.fineDue = 0; collection થી just પહેલાં. compiler એ બધું approve કરે છે, કારણ કે દરેક field હાલમાં everyone માટે open છે.

Real systems records ને counter પાછળ મૂકે છે: તમે change ને request કરો છો, અને desk rules ને apply કરે છે. Java નો counter access controls થી build થયેલો છે.

Theory

new actually શું કરે છે

પહેલા, object creation ની precise anatomy, exam favourite:

Book b = new Book();

3 things happen:

  • Book b reference variable declare કરે છે (એ Book તરફ point કરી શકે છે, હાલમાં કંઈ નથી)
  • new Book() heap પર એક Book ના fields માટે memory allocate કરે છે અને તેનો constructor run કરે છે
  • = reference ને તે new object તરફ point કરાવે છે

Split form equally legal છે: Book b; b = new Book();

At a glance

4 access levels, narrowest first

Modifierકોણ access કરી શકે છે
privateફક્ત same class ની અંદરનું code
(no modifier / default)Same package ની કોઈપણ class
protectedSame package, plus subclasses anywhere
publicEverywhere

Practical

copies counter પાછળ જાય છે

class Book {
    private int copies = 3;    // locked: ફક્ત Book નું પોતાનું code touch કરી શકે
    String title;              // default access: આખું package

    public boolean issue() {   // the public front desk
        if (copies > 0) {
            copies--;
            return true;
        }
        return false;          // ક્યારેય 0 ની નીચે જતું નથી
    }

    public int getCopies() { return copies; }
}

public class BookBridge {
    public static void main(String[] args) {
        Book b = new Book();
        b.issue();
        System.out.println(b.getCopies());  // 2
        // b.copies = -50;   // uncomment: compile error, private access
    }
}

Quiz

field ને કોઈ access modifier વગર declare કરવામાં આવ્યું છે: int copies; એને કોણ access કરી શકે છે?

  1. Everyone, everywhere: no modifier એટલે કોઈ restriction નથી
  2. ફક્ત જે class એ declare કર્યું છે એ
  3. Same package ની કોઈપણ class
  4. ફક્ત declaring class ના subclasses
Show the answer

Same package ની કોઈપણ class

No modifier એ પોતે જ level છે, default અથવા package-private કહેવાય છે: આખું package (Java નું folder-like grouping, Unit 4) એને access કરી શકે છે, outsiders નહીં. Option A એ trap છે; Java માં omission દ્વારા fully-unrestricted rule નથી. Option B private ને describe કરે છે, option D લગભગ protected નો extra grant છે. Exams default ને love કરે છે precisely કારણ કે students assume કરે છે કે absence એટલે everything અથવા nothing.

Think first

vandal ક્યારે caught થાય છે?

હવે copies private છે, કોઈ BookBridge ના main માં b.copies = -50; લખે છે. tap કરતા પહેલા: આ program run થાય ત્યારે fail થાય છે, કે earlier?

Show the answer

Earlier: એ ક્યારેય compile નથી થતું. javac copies has private access in Book સાથે stop થાય છે. Access control compile time પર enforce થાય છે, એટલે illegal touches program exist થઈ શકે એ પહેલાં catch થાય છે, Java ની robustness નું બીજું ઉદાહરણ. છેલ્લા lesson ના NullPointerException સાથે contrast કરો, જે runtime crash છે: exams like asking which failures are compile-time and which are runtime.

Watch out

બે subtle rules જે marks cost કરે છે

private એ per-class છે, per-object નહીં: Book ના પોતાના code ની અંદર, એક Book બીજા Book ના private fields ને touch કરી શકે છે (this.copies + other.copies એ Book method માં legal છે). દીવાલ class ની આસપાસ stands કરે છે.

Java નું protected એ C++ ના protected કરતાં wider છે: એ whole package PLUS subclasses anywhere ને grant કરે છે. BCA304 એ protected ને "class + children only" શીખવ્યું; એ definition ને Java answer માં carry કરવાથી package half ગુમાવે છે.

Theory

આ encapsulation નો જન્મ છે

Private data plus public methods જે એને guard કરે છે: એ pattern નું નામ છે, encapsulation, OOP ના 3 pillars માંનું એક, અને એને ટૂંકમાં full lesson મળે છે. notice કરો કે guard એ શું આપ્યું: issue() copies ને 0 ની નીચે push નથી કરી શકતું, એટલે invalid state હવે impossible છે, not just unlikely. next lesson problem ની બીજી half ને fix કરે છે: દરેક Book ને પહેલી જ વાર sensible values સાથે born થવા દેવું, constructors દ્વારા.

Summary

Key takeaways

  • Book b = new Book(); reference declare કરે છે, object ને allocate કરે છે, અને 2 ને connect કરે છે.
  • 4 access levels: private (class), default (package), protected (package + subclasses), public (everywhere).
  • No modifier એ package-private છે, open-to-all નહીં.
  • Access violations compile-time errors છે; illegal program ક્યારેય run નથી થતો.
  • private class ને guard કરે છે, દરેક object ને નહીં: same-class code બધું જુએ છે.
  • Java નું protected package ને include કરે છે, C++ ના protected કરતાં wider.
  • Memory hook: private, package, protected, public: દરેક step પર door wider ખૂલે છે.

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 Classes and Objects

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

Access Controls, Object creation · Java Programming Language · Gri-Learn