PROTECTED is an object-oriented access modifier. It lets the defining class and its subclasses access a member (data or method), while blocking unrelated external code. It balances encapsulation and extensibility.
Think of a company building with floors: PUBLIC areas are the lobby (anyone enters), PROTECTED areas are staff-only floors (employees and trainees can enter), and PRIVATE areas are executive offices (only the specific team can enter). Subclasses are like trainees who get access to staff-only floors.
Declare members as PROTECTED when they are internal extension points meant for subclasses. Expose stable PUBLIC methods for general use.
123456789101112131415161718192021222324252627*> Conceptual OO COBOL-style example (dialect-specific) CLASS-ID. ACCOUNT. DATA DIVISION. WORKING-STORAGE SECTION. 01 BALANCE PIC 9(9)V99. 01 MIN-BALANCE PIC 9(5)V99 VALUE 100.00. *> PROTECTED member 01 PROTECTED-FEE PIC 9(5)V99 VALUE 5.00. METHOD-ID. APPLY-FEE PROTECTED. PROCEDURE DIVISION. SUBTRACT PROTECTED-FEE FROM BALANCE EXIT METHOD. END METHOD APPLY-FEE. END CLASS ACCOUNT. CLASS-ID. SAVINGS-ACCOUNT INHERITS FROM ACCOUNT. METHOD-ID. MONTH-END-CLOSE PUBLIC. PROCEDURE DIVISION. *> Subclass can use protected hook INVOKE SELF "APPLY-FEE" IF BALANCE < MIN-BALANCE DISPLAY "Below minimum." END-IF EXIT METHOD. END METHOD MONTH-END-CLOSE. END CLASS SAVINGS-ACCOUNT.
Subclasses can invoke PROTECTED members. External programs cannot access them directly.
Action | Concept | Notes |
---|---|---|
Mark member | PROTECTED data/method | Visible to class + subclasses |
Override | METHOD ... OVERRIDE | Child adjusts behavior |
Access | From subclass | External access blocked |
Mistake | Problem | Fix |
---|---|---|
Using PUBLIC for internal hooks | External coupling to internals | Use PROTECTED, document as internal |
Too many PROTECTED members | Class surface becomes complex | Refactor or compose smaller classes |
Assuming runtime security | Visibility is compile-time only | Use platform security for runtime protection |
1. What does PROTECTED mean in OO COBOL?
2. Which is true about PROTECTED methods?
3. Why use PROTECTED instead of PUBLIC for a method?
4. Which access levels commonly appear with OO COBOL?
5. What happens when external code tries to access a PROTECTED property?