Theory
helper જે backstage stay થવું જોઈએ
circulation package ની અંદર તમે FineTable લખો છો, નાનું helper જે days-late ને rupees માં map કરે છે. એ plumbing છે: જો reports package (તમારા teammate નું code) એને directly call કરવાનું start કરે, તમે જે દરેક change કરો છો એ તેના month-end report ને break કરે છે.
તમે FineTable ને દરેક circulation class દ્વારા usable અને બાકીના બધા માટે invisible જોઈએ છે.
Unit 2 ના mysterious no-modifier access level ને યાદ કરો, જેને "the whole package" તરીકે define કરવામાં આવ્યું હતું? આ એ lesson છે જ્યાં એ તેનું living earn કરે છે.
Theory
package ની અંદર શું lives કરે છે
package ના contents છે:
- classes (Book, IssueRegister, FineTable)
- interfaces (Printable, Payable)
- exception classes (OverdueException: એ પણ class છે)
- subpackages (circulation.fines), જે separate packages છે જે merely name prefix ને share કરે છે
એક physical rule જે exam weight ધરાવે છે: public class ને exactly તેના name પછી named file માં declare કરવું જોઈએ (public class IssueRegister ફક્ત IssueRegister.java માં), અને file માં at most one public class હોઈ શકે છે.
At a glance
Access levels, બીજા package થી જોવામાં
| Member marked | Same package એને જુએ છે? | Other package એને જુએ છે? |
|---|---|---|
| private | No (own class only) | No |
| (default, no modifier) | Yes | No |
| protected | Yes | ફક્ત તેના subclasses |
| public | Yes | Yes |
Practical
default દ્વારા Backstage
// file: circulation/FineTable.java
package circulation;
class FineTable { // 'public' નથી: package-private CLASS
static int fineFor(int daysLate) {
return daysLate * 2; // Rs 2 per late day
}
}
// file: circulation/ReturnDesk.java
package circulation;
public class ReturnDesk { // package નું public face
public int settle(int daysLate) {
return FineTable.fineFor(daysLate); // same package: fine
}
}
// package reports માં, નીચેની બંને lines COMPILE ERRORS હોત:
// circulation.FineTable.fineFor(3); class visible નથી
// new circulation.FineTable(); class visible નથી
Quiz
class Helper { } (no modifier) package circulation માં sits કરે છે. package reports માં code new circulation.Helper(); try કરે છે. શું થાય છે?
- Works: fully qualified name access rules ને bypass કરે છે
- Compile error: default-access class તેના package ની બહાર invisible છે
- Works, પણ ફક્ત Helper ના public members usable છે
- Constructor execute થાય ત્યારે runtime exception
Show the answer
Compile error: default-access class તેના package ની બહાર invisible છે
CLASS પર no modifier એટલે package-private: બાકીના દરેક package માટે, Helper simply exist નથી કરતું, અને compiler એ કહે છે. Option A એ tempting myth છે જે આ question kill કરે છે: fully qualified name એ address છે, key નહીં; એ ક્યારેય access control ને override નથી કરતો. Option C ને class પહેલા visible હોવું જોઈએ. Option D too late છે: Java માં દરેક access violation ની જેમ, આ compile time પર dies થાય છે.
Think first
બે Reports, no fight
તમારું circulation package અને તમારા teammate નું reports package બંને Summary નામની class declare કરે છે. tap કરતા પહેલા: project હજુ compile થાય છે, અને Java એમને કેવી રીતે tell apart કરે છે?
Show the answer
એ happily compile થાય છે. class નું real name એ તેનું fully qualified name છે: circulation.Summary અને reports.Summary એ entirely different names છે જે merely end alike છે, જેમ 2 Riya નામના students ને surname દ્વારા distinguish કરવામાં આવે છે. આ packages છે જે namespaces તરીકે act કરે છે, બીજું big reason જે exist કરે છે access control સિવાય. એક file જે બંને ને at once જોઈએ છે એ neither ને import કરતું નથી અને full names ને out લખે છે.
Watch out
જ્યાં package access લોકો ને surprise કરે છે
class પર public ને forget કરવું: અંદરનું everything public હોઈ શકે છે, પણ જો CLASS default હોય, તો કોઈ બીજું package એના કંઈ ને touch નથી કરી શકતું. જ્યારે import mysteriously fail થાય ત્યારે class ના own modifier ને પહેલા check કરો.
packages આરપાર protected: protected member બીજા package સુધી ONLY subclass ની અંદર reaches કરે છે. Unrelated classes ત્યાં કંઈ જુએ નથી; અને Java નું protected હજુ whole home package ને include કરે છે, BCA304 ના C++ rule કરતાં wider.
Theory
boundary સાથે design કરો
Professional Java package line ને deliberately વાપરે છે: થોડા public classes shelf ના front તરીકે (ReturnDesk), helpers તેમની પાછળ package-private left (FineTable). plumbing ને freely change કરો; outsiders ક્યારેય એના પર depend નહોતા. એ Unit 2 ની encapsulation છે જે one class થી આખી shelf સુધી scaled up થઈ છે. છેલ્લો package lesson પૂછે છે કે machinery આ બધા shelves ને run time પર કેવી રીતે FIND કરે છે: folders, classpath, અને Package object.
Summary
Key takeaways
- Package classes, interfaces, exception classes અને subpackages ને contain કરે છે.
- Default (no-modifier) access = package-private: package ની અંદર everywhere visible, બહાર invisible.
- default-access CLASS અન્ય packages થી unusable છે, whatever its members કહે છે.
- public class Name એ Name.java માં live થવું જોઈએ; એક file માં one public class.
- protected = whole home package + other packages માં subclasses.
- 2 packages માં same class name fine છે: fully qualified names real names છે.
- Memory hook: package એ shelf છે; no-modifier એટલે staff only.