MainframeMaster

COBOL Tutorial

COBOL PROPERTY - Object-Oriented Properties

Progress0 of 0 lessons

What is PROPERTY?

PROPERTY in COBOL is a fundamental concept in object-oriented programming that provides controlled access to data fields within a class. Think of it as a smart container that not only holds data but also controls how that data can be accessed and modified - it's like having a security guard that checks credentials before allowing access to valuable information.

🏠 Real-World Analogy

Imagine a smart home security system:

  • PROPERTY: Like a secure room with controlled access
  • GET Method: Like checking what's in the room (read access)
  • SET Method: Like putting something in the room (write access)
  • Validation: Like security checks before allowing entry
  • Encapsulation: Like keeping the room's contents private

PROPERTY in COBOL works the same way - it protects and controls access to your data.

Key Features

  • Data Encapsulation - Protects data from direct access
  • Controlled Access - GET and SET methods for data access
  • Validation Support - Can validate data before assignment
  • Business Logic - Can include business rules in access methods
  • Inheritance Support - Can be inherited and overridden
  • Type Safety - Ensures data type consistency
  • Error Handling - Can handle invalid data gracefully
  • Documentation - Self-documenting interface

How to Use PROPERTY

Using PROPERTY in COBOL involves defining the property data field and implementing GET and SET methods to control access to that data.

Basic PROPERTY Implementation

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
* Basic PROPERTY implementation CLASS-ID. CUSTOMER-CLASS. ENVIRONMENT DIVISION. CONFIGURATION SECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-NAME PIC X(30). 01 CUSTOMER-AGE PIC 9(3). 01 CUSTOMER-EMAIL PIC X(50). OBJECT SECTION. CLASS CUSTOMER-CLASS. * Property definitions PROPERTY NAME IS CUSTOMER-NAME PROPERTY AGE IS CUSTOMER-AGE PROPERTY EMAIL IS CUSTOMER-EMAIL * GET method for NAME property METHOD-ID. GET-NAME. DATA DIVISION. LINKAGE SECTION. 01 RETURN-NAME PIC X(30). PROCEDURE DIVISION RETURNING RETURN-NAME. MOVE CUSTOMER-NAME TO RETURN-NAME EXIT METHOD. END METHOD GET-NAME. * SET method for NAME property METHOD-ID. SET-NAME. DATA DIVISION. LINKAGE SECTION. 01 NEW-NAME PIC X(30). PROCEDURE DIVISION USING NEW-NAME. * Validation logic IF NEW-NAME = SPACES DISPLAY "Error: Name cannot be empty" ELSE MOVE NEW-NAME TO CUSTOMER-NAME END-IF EXIT METHOD. END METHOD SET-NAME. * GET method for AGE property METHOD-ID. GET-AGE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-AGE PIC 9(3). PROCEDURE DIVISION RETURNING RETURN-AGE. MOVE CUSTOMER-AGE TO RETURN-AGE EXIT METHOD. END METHOD GET-AGE. * SET method for AGE property with validation METHOD-ID. SET-AGE. DATA DIVISION. LINKAGE SECTION. 01 NEW-AGE PIC 9(3). PROCEDURE DIVISION USING NEW-AGE. * Age validation IF NEW-AGE < 0 OR NEW-AGE > 150 DISPLAY "Error: Invalid age value" ELSE MOVE NEW-AGE TO CUSTOMER-AGE END-IF EXIT METHOD. END METHOD SET-AGE. END CLASS CUSTOMER-CLASS.

This example shows basic property implementation with GET and SET methods including validation.

PROPERTY Operations

OperationSyntaxExample
Define propertyPROPERTY name IS data-fieldPROPERTY NAME IS CUSTOMER-NAME
GET methodMETHOD-ID. GET-name. RETURNING return-fieldMETHOD-ID. GET-NAME. RETURNING RETURN-NAME
SET methodMETHOD-ID. SET-name. USING input-fieldMETHOD-ID. SET-NAME. USING NEW-NAME
Access propertyINVOKE object "GET-name" RETURNING valueINVOKE CUSTOMER "GET-NAME" RETURNING NAME
Set propertyINVOKE object "SET-name" USING valueINVOKE CUSTOMER "SET-NAME" USING NEW-NAME

Practical Examples

Let's look at some real-world examples of how PROPERTY is used in COBOL applications.

Bank Account with Properties

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
* Bank account class with properties CLASS-ID. BANK-ACCOUNT. DATA DIVISION. WORKING-STORAGE SECTION. 01 ACCOUNT-NUMBER PIC 9(10). 01 ACCOUNT-BALANCE PIC 9(8)V99. 01 ACCOUNT-HOLDER PIC X(30). 01 ACCOUNT-TYPE PIC X(10). 01 MIN-BALANCE PIC 9(8)V99 VALUE 100.00. OBJECT SECTION. CLASS BANK-ACCOUNT. * Property definitions PROPERTY ACCOUNT-NUMBER IS ACCOUNT-NUMBER PROPERTY BALANCE IS ACCOUNT-BALANCE PROPERTY HOLDER IS ACCOUNT-HOLDER PROPERTY TYPE IS ACCOUNT-TYPE * GET method for ACCOUNT-NUMBER (read-only) METHOD-ID. GET-ACCOUNT-NUMBER. DATA DIVISION. LINKAGE SECTION. 01 RETURN-NUMBER PIC 9(10). PROCEDURE DIVISION RETURNING RETURN-NUMBER. MOVE ACCOUNT-NUMBER TO RETURN-NUMBER EXIT METHOD. END METHOD GET-ACCOUNT-NUMBER. * SET method for ACCOUNT-NUMBER with validation METHOD-ID. SET-ACCOUNT-NUMBER. DATA DIVISION. LINKAGE SECTION. 01 NEW-NUMBER PIC 9(10). PROCEDURE DIVISION USING NEW-NUMBER. * Account number validation IF NEW-NUMBER = 0 DISPLAY "Error: Invalid account number" ELSE MOVE NEW-NUMBER TO ACCOUNT-NUMBER END-IF EXIT METHOD. END METHOD SET-ACCOUNT-NUMBER. * GET method for BALANCE METHOD-ID. GET-BALANCE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-BALANCE PIC 9(8)V99. PROCEDURE DIVISION RETURNING RETURN-BALANCE. MOVE ACCOUNT-BALANCE TO RETURN-BALANCE EXIT METHOD. END METHOD GET-BALANCE. * SET method for BALANCE with business rules METHOD-ID. SET-BALANCE. DATA DIVISION. LINKAGE SECTION. 01 NEW-BALANCE PIC 9(8)V99. PROCEDURE DIVISION USING NEW-BALANCE. * Balance validation IF NEW-BALANCE < 0 DISPLAY "Error: Balance cannot be negative" ELSE IF NEW-BALANCE < MIN-BALANCE DISPLAY "Warning: Balance below minimum" MOVE NEW-BALANCE TO ACCOUNT-BALANCE ELSE MOVE NEW-BALANCE TO ACCOUNT-BALANCE END-IF EXIT METHOD. END METHOD SET-BALANCE. * Deposit method using property METHOD-ID. DEPOSIT. DATA DIVISION. LINKAGE SECTION. 01 DEPOSIT-AMOUNT PIC 9(8)V99. PROCEDURE DIVISION USING DEPOSIT-AMOUNT. * Validate deposit amount IF DEPOSIT-AMOUNT <= 0 DISPLAY "Error: Invalid deposit amount" ELSE COMPUTE ACCOUNT-BALANCE = ACCOUNT-BALANCE + DEPOSIT-AMOUNT DISPLAY "Deposit successful. New balance: " ACCOUNT-BALANCE END-IF EXIT METHOD. END METHOD DEPOSIT. * Withdraw method using property METHOD-ID. WITHDRAW. DATA DIVISION. LINKAGE SECTION. 01 WITHDRAW-AMOUNT PIC 9(8)V99. 01 SUCCESS-FLAG PIC X(1). PROCEDURE DIVISION USING WITHDRAW-AMOUNT RETURNING SUCCESS-FLAG. * Validate withdrawal IF WITHDRAW-AMOUNT <= 0 MOVE "N" TO SUCCESS-FLAG DISPLAY "Error: Invalid withdrawal amount" ELSE IF WITHDRAW-AMOUNT > ACCOUNT-BALANCE MOVE "N" TO SUCCESS-FLAG DISPLAY "Error: Insufficient funds" ELSE COMPUTE ACCOUNT-BALANCE = ACCOUNT-BALANCE - WITHDRAW-AMOUNT MOVE "Y" TO SUCCESS-FLAG DISPLAY "Withdrawal successful. New balance: " ACCOUNT-BALANCE END-IF EXIT METHOD. END METHOD WITHDRAW. END CLASS BANK-ACCOUNT.

This example shows a bank account class with properties for account data and business logic.

Employee Management System

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
* Employee class with computed properties CLASS-ID. EMPLOYEE. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-ID PIC 9(6). 01 EMPLOYEE-NAME PIC X(30). 01 EMPLOYEE-SALARY PIC 9(8)V99. 01 EMPLOYEE-YEARS PIC 9(2). 01 BONUS-RATE PIC 9V99 VALUE 0.10. OBJECT SECTION. CLASS EMPLOYEE. * Property definitions PROPERTY ID IS EMPLOYEE-ID PROPERTY NAME IS EMPLOYEE-NAME PROPERTY SALARY IS EMPLOYEE-SALARY PROPERTY YEARS IS EMPLOYEE-YEARS * GET method for computed bonus property METHOD-ID. GET-BONUS. DATA DIVISION. LINKAGE SECTION. 01 RETURN-BONUS PIC 9(8)V99. PROCEDURE DIVISION RETURNING RETURN-BONUS. * Calculate bonus based on salary and years COMPUTE RETURN-BONUS = EMPLOYEE-SALARY * BONUS-RATE * EMPLOYEE-YEARS EXIT METHOD. END METHOD GET-BONUS. * SET method for SALARY with validation METHOD-ID. SET-SALARY. DATA DIVISION. LINKAGE SECTION. 01 NEW-SALARY PIC 9(8)V99. PROCEDURE DIVISION USING NEW-SALARY. * Salary validation IF NEW-SALARY < 0 DISPLAY "Error: Salary cannot be negative" ELSE IF NEW-SALARY < 20000 DISPLAY "Warning: Salary below minimum wage" MOVE NEW-SALARY TO EMPLOYEE-SALARY ELSE MOVE NEW-SALARY TO EMPLOYEE-SALARY END-IF EXIT METHOD. END METHOD SET-SALARY. * GET method for YEARS with computed seniority METHOD-ID. GET-SENIORITY. DATA DIVISION. LINKAGE SECTION. 01 SENIORITY-LEVEL PIC X(10). PROCEDURE DIVISION RETURNING SENIORITY-LEVEL. * Determine seniority level EVALUATE EMPLOYEE-YEARS WHEN 0 THRU 2 MOVE "Junior" TO SENIORITY-LEVEL WHEN 3 THRU 5 MOVE "Mid-Level" TO SENIORITY-LEVEL WHEN 6 THRU 10 MOVE "Senior" TO SENIORITY-LEVEL WHEN OTHER MOVE "Expert" TO SENIORITY-LEVEL END-EVALUATE EXIT METHOD. END METHOD GET-SENIORITY. * Method to display employee information METHOD-ID. DISPLAY-INFO. DATA DIVISION. LINKAGE SECTION. 01 BONUS-AMOUNT PIC 9(8)V99. 01 SENIORITY PIC X(10). PROCEDURE DIVISION. * Get computed properties INVOKE SELF "GET-BONUS" RETURNING BONUS-AMOUNT INVOKE SELF "GET-SENIORITY" RETURNING SENIORITY * Display employee information DISPLAY "Employee ID: " EMPLOYEE-ID DISPLAY "Name: " EMPLOYEE-NAME DISPLAY "Salary: " EMPLOYEE-SALARY DISPLAY "Years: " EMPLOYEE-YEARS DISPLAY "Seniority: " SENIORITY DISPLAY "Bonus: " BONUS-AMOUNT EXIT METHOD. END METHOD DISPLAY-INFO. END CLASS EMPLOYEE.

This example demonstrates computed properties and business logic in an employee management system.

Advanced PROPERTY Features

PROPERTY supports advanced features for complex object-oriented programming scenarios.

Read-Only and Write-Only Properties

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
* Properties with different access levels CLASS-ID. SECURE-DATA. DATA DIVISION. WORKING-STORAGE SECTION. 01 SECRET-KEY PIC X(20). 01 ACCESS-COUNT PIC 9(5) VALUE 0. 01 LAST-ACCESS-DATE PIC 9(8). OBJECT SECTION. CLASS SECURE-DATA. * Read-only property (only GET method) PROPERTY ACCESS-COUNT IS ACCESS-COUNT * Write-only property (only SET method) PROPERTY SECRET-KEY IS SECRET-KEY * GET method for ACCESS-COUNT (read-only) METHOD-ID. GET-ACCESS-COUNT. DATA DIVISION. LINKAGE SECTION. 01 RETURN-COUNT PIC 9(5). PROCEDURE DIVISION RETURNING RETURN-COUNT. MOVE ACCESS-COUNT TO RETURN-COUNT EXIT METHOD. END METHOD GET-ACCESS-COUNT. * SET method for SECRET-KEY (write-only) METHOD-ID. SET-SECRET-KEY. DATA DIVISION. LINKAGE SECTION. 01 NEW-KEY PIC X(20). PROCEDURE DIVISION USING NEW-KEY. * Validation and logging IF NEW-KEY = SPACES DISPLAY "Error: Secret key cannot be empty" ELSE MOVE NEW-KEY TO SECRET-KEY ADD 1 TO ACCESS-COUNT MOVE FUNCTION CURRENT-DATE(1:8) TO LAST-ACCESS-DATE DISPLAY "Secret key updated successfully" END-IF EXIT METHOD. END METHOD SET-SECRET-KEY. * Method to check if secret key is set METHOD-ID. HAS-SECRET-KEY. DATA DIVISION. LINKAGE SECTION. 01 HAS-KEY PIC X(1). PROCEDURE DIVISION RETURNING HAS-KEY. IF SECRET-KEY = SPACES MOVE "N" TO HAS-KEY ELSE MOVE "Y" TO HAS-KEY END-IF EXIT METHOD. END METHOD HAS-SECRET-KEY. END CLASS SECURE-DATA.

This example shows read-only and write-only properties with different access levels.

Property Inheritance and Overriding

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
* Property inheritance example CLASS-ID. VEHICLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 VEHICLE-MAKE PIC X(20). 01 VEHICLE-MODEL PIC X(20). 01 VEHICLE-YEAR PIC 9(4). OBJECT SECTION. CLASS VEHICLE. * Base properties PROPERTY MAKE IS VEHICLE-MAKE PROPERTY MODEL IS VEHICLE-MODEL PROPERTY YEAR IS VEHICLE-YEAR * GET method for MAKE METHOD-ID. GET-MAKE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-MAKE PIC X(20). PROCEDURE DIVISION RETURNING RETURN-MAKE. MOVE VEHICLE-MAKE TO RETURN-MAKE EXIT METHOD. END METHOD GET-MAKE. * SET method for MAKE METHOD-ID. SET-MAKE. DATA DIVISION. LINKAGE SECTION. 01 NEW-MAKE PIC X(20). PROCEDURE DIVISION USING NEW-MAKE. MOVE NEW-MAKE TO VEHICLE-MAKE EXIT METHOD. END METHOD SET-MAKE. END CLASS VEHICLE. * Car class inheriting from Vehicle CLASS-ID. CAR INHERITS FROM VEHICLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 CAR-DOORS PIC 9(1). 01 CAR-COLOR PIC X(15). OBJECT SECTION. CLASS CAR. * Additional properties for Car PROPERTY DOORS IS CAR-DOORS PROPERTY COLOR IS CAR-COLOR * Override SET method for MAKE with validation METHOD-ID. SET-MAKE OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 NEW-MAKE PIC X(20). PROCEDURE DIVISION USING NEW-MAKE. * Car-specific validation IF NEW-MAKE = "INVALID" DISPLAY "Error: Invalid make for car" ELSE * Call parent method INVOKE SUPER "SET-MAKE" USING NEW-MAKE END-IF EXIT METHOD. END METHOD SET-MAKE. * SET method for DOORS with validation METHOD-ID. SET-DOORS. DATA DIVISION. LINKAGE SECTION. 01 NEW-DOORS PIC 9(1). PROCEDURE DIVISION USING NEW-DOORS. * Door validation IF NEW-DOORS < 2 OR NEW-DOORS > 5 DISPLAY "Error: Invalid number of doors" ELSE MOVE NEW-DOORS TO CAR-DOORS END-IF EXIT METHOD. END METHOD SET-DOORS. END CLASS CAR.

This example demonstrates property inheritance and method overriding in COBOL.

Best Practices and Tips

Following these best practices will help you use PROPERTY effectively in your COBOL applications.

PROPERTY Best Practices

  • Always validate in SET methods - Ensure data integrity through validation
  • Use meaningful property names - Choose descriptive names for properties
  • Implement proper error handling - Handle validation failures gracefully
  • Document property behavior - Document expected values and constraints
  • Use computed properties wisely - For derived values that don't need storage
  • Maintain consistency - Use consistent naming and access patterns
  • Consider access levels - Use read-only, write-only, or read-write as appropriate
  • Test property behavior - Thoroughly test GET and SET methods

Common Mistakes to Avoid

MistakeProblemSolution
No validation in SET methodsInvalid data can be storedAlways implement validation logic
Exposing internal dataBreaks encapsulationUse properties for controlled access
Inconsistent namingConfusing and hard to maintainUse consistent naming conventions
No error handlingProgram crashes on invalid dataImplement proper error handling
Over-complex propertiesHard to understand and debugKeep properties simple and focused
Ignoring inheritanceMissed opportunities for reuseUse inheritance appropriately

PROPERTY Quick Reference

ActionSyntaxExample
Define propertyPROPERTY name IS data-fieldPROPERTY NAME IS CUSTOMER-NAME
GET methodMETHOD-ID. GET-name. RETURNING return-fieldMETHOD-ID. GET-NAME. RETURNING RETURN-NAME
SET methodMETHOD-ID. SET-name. USING input-fieldMETHOD-ID. SET-NAME. USING NEW-NAME
Access propertyINVOKE object "GET-name" RETURNING valueINVOKE CUSTOMER "GET-NAME" RETURNING NAME
Set propertyINVOKE object "SET-name" USING valueINVOKE CUSTOMER "SET-NAME" USING NEW-NAME
Override methodMETHOD-ID. method-name OVERRIDEMETHOD-ID. SET-NAME OVERRIDE

Test Your Knowledge

1. What is PROPERTY in COBOL?

  • A data field for storing values
  • A mechanism for encapsulating data with controlled access in object-oriented COBOL
  • A file organization method
  • A program structure element

2. In which COBOL division is PROPERTY typically defined?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. How do you access a PROPERTY in COBOL?

  • Directly using the property name
  • Using GET and SET methods
  • Using MOVE statements
  • Using DISPLAY statements

4. What is the main benefit of using PROPERTY in COBOL?

  • Faster program execution
  • Data encapsulation and controlled access
  • Reduced memory usage
  • Simpler program structure

5. Which COBOL feature is commonly used with PROPERTY for validation?

  • VALIDATE statement
  • GET and SET methods
  • IF statements
  • All of the above

Frequently Asked Questions