25-Comp-B11 Advanced Software Design · Undated paper
Question 25 of 28: C++ Private-Member Access for Other Classes, and Why Java Disallows It
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Notes on this paper
17-Comp-B11 Advanced Software Design — National Exams, May 2019. 3 hours, closed book exam with two aid sheets allowed (written on both sides), no calculator permitted. The paper is organized into five parts, and candidates were instructed to answer any five (5) questions in Part I, any three (3) in Part II, any four (4) in Part III, any two (2) in Part IV, and any five (5) in Part V — only the first questions answered, in each part, as they appear in the answer book are marked. All questions carry equal weight, so the 19 questions actually marked (5+3+4+2+5 of 28) each count for 100/19 ≈ 5.26% of the paper. All 28 questions are answered below for completeness.
Reference texts: Sommerville, Software Engineering (10th ed., Pearson) — software processes, requirements engineering, design principles, testing, dependability, reuse; Pressman, Software Engineering: A Practitioner's Approach (9th ed.) — supplementary process/metrics/quality coverage; Gamma, Helm, Johnson & Vlissides (GoF), Design Patterns: Elements of Reusable Object-Oriented Software — pattern-language structure, the GoF pattern catalogue, and the "favor object composition over class inheritance" / "program to an interface, not an implementation" principles; Sebesta, Concepts of Programming Languages (12th ed.) — polymorphism, dynamic binding, visibility, encapsulation, interfaces; Bertrand Meyer, Object-Oriented Software Construction — design by contract, preconditions/postconditions/class invariants; Barbara Liskov's 1987 substitutability paper for Question 12; Stroustrup, The C++ Programming Language, for friend/access-control semantics (Question 25).
Question 12 prints “Liskpv substitution principle”, a typo in the paper; it is answered as the Liskov substitution principle.
PART I — General Principles (answer any 5 of 7)
Question 25: C++ Private-Member Access for Other Classes, and Why Java Disallows It (Part V)
C++ lets a class explicitly grant a named external class (or function) access to its own private members via the friend declaration:
class Contact {
private:
std::string name;
std::string phone;
friend class AddressBookManager; // grants full access to this one class
public:
Contact(std::string n, std::string p) : name(n), phone(p) {}
};
class AddressBookManager {
public:
void mergeDuplicates(Contact& a, Contact& b) {
a.phone = b.phone; // legal: AddressBookManager is a friend
}
};
AddressBookManager can reach Contact's private fields directly because it was explicitly named as a friend; any other class attempting the same access would fail to compile. Java has no equivalent mechanism — the closest substitute is exposing a package-private (default-visibility) accessor and placing both classes in the same package, which is coarser (it opens access to every class in that package, not to one named class).
Advantages of the C++ approach. It allows two tightly-coupled classes to share representation efficiently (no accessor-method call overhead) and precisely (one named friend, not a whole package) when they are conceptually part of the same larger abstraction (e.g. a manager class and the record it manages).
Disadvantages. It breaks encapsulation's information-hiding guarantee for the friend relationship specifically: Contact's representation can no longer change without also checking every declared friend, coupling grows silently as more friends are added, and the friend relationship is not inherited or transitive, which can also surprise maintainers who expect object-oriented access rules to compose the way inheritance does.
Why Java disallows it. Java's design philosophy favours a simpler, more uniform, more strictly enforced encapsulation model: allowing an explicit, arbitrary, one-off access grant like friend was judged to invite exactly the tight, hard-to-track coupling described above, so Java forces all such sharing through the coarser but simpler and safer package-visibility mechanism instead.