MainframeMaster

COBOL Tutorial

COBOL PROTECTED - OO Access Control

Progress0 of 0 lessons

Overview

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.

🏛️ Analogy

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.

Key Concepts

  • Visibility: class + subclasses; not visible to unrelated code
  • Encapsulation: hide internals while enabling safe extension points
  • Overriding: allow child classes to adjust behavior
  • Stability: reduce accidental external dependencies

How to Use PROTECTED

Declare members as PROTECTED when they are internal extension points meant for subclasses. Expose stable PUBLIC methods for general use.

Subclass Access Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
*> 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.

Non-Code Example

  • A bank exposes PUBLIC operations like deposit/withdraw.
  • It keeps PROTECTED hooks like apply-fee or reconcile used by derived account types.
  • External callers cannot trigger internal hooks; only specialized account types can.

Quick Reference

ActionConceptNotes
Mark memberPROTECTED data/methodVisible to class + subclasses
OverrideMETHOD ... OVERRIDEChild adjusts behavior
AccessFrom subclassExternal access blocked

Best Practices and Pitfalls

Best Practices

  • Expose minimal PUBLIC surface; keep extension points PROTECTED
  • Document PROTECTED contracts for subclass authors
  • Keep invariants enforced by PRIVATE state, validated in PROTECTED hooks
  • Favor composition if many PROTECTED hooks indicate class doing too much

Common Mistakes

MistakeProblemFix
Using PUBLIC for internal hooksExternal coupling to internalsUse PROTECTED, document as internal
Too many PROTECTED membersClass surface becomes complexRefactor or compose smaller classes
Assuming runtime securityVisibility is compile-time onlyUse platform security for runtime protection

Test Your Knowledge

1. What does PROTECTED mean in OO COBOL?

  • Accessible by any program
  • Accessible only within the class and its subclasses
  • Accessible only within the same paragraph
  • Accessible only by the operating system

2. Which is true about PROTECTED methods?

  • They can be invoked by any object in any program
  • They can only be invoked from the defining class
  • They can be invoked from the defining class and subclasses
  • They cannot be overridden

3. Why use PROTECTED instead of PUBLIC for a method?

  • To improve CPU performance
  • To prevent external callers from depending on internal hooks
  • To reduce memory usage
  • To allow dynamic typing

4. Which access levels commonly appear with OO COBOL?

  • PRIVATE, PROTECTED, PUBLIC
  • SECURE, GUARDED, OPEN
  • HIDDEN, VISIBLE
  • LOCAL, EXTERNAL

5. What happens when external code tries to access a PROTECTED property?

  • It compiles and runs
  • It compiles but fails at runtime
  • It fails to compile (visibility error)
  • It silently succeeds

Frequently Asked Questions