Certain Such Cases where a class would want access to other classes private attributes and methods in such scenario we can make use of the friends concept in classes.
Scenario :-
Saving Account Class Define itself friend of Account holder so This Will Allow Account Holder To Access Private data of Saving account Class .
Perform Friends Operation For above example using Local Class.
Procedure :-
Step 1. Create Class Definition & Class Implementation For Saving Account.
Class Definition

Class Implementation

Step 3. Create Inherited Class Definition & Class Implementation For Account
Holder.

Step 4. Create Inherited Class Definition & Class Implementation For Other
Person.

Step 5. Create Referral Object For Account Holder & Other Person Methods .&
Call Method To Display Amount.

Step 6. Save & Check . It Gives Error Bcz Other Person Don't Have Permission
to Withdraw bcz He is Not a Friend Of Saving Account . For Execution
Comment Withdraw Display Statement.

Step 7. Execute Program It Gives Following Output

Scenario :-
Three classes has been defined SAVING_ACCOUNT ( BANK SAVING
ACCOUNT ), ACCOUNT_HOLDER (SAVING ACCOUNT OWNER) , OTHER_PERSON ( NON ACCOUNT HOLDER) .
Both account holder & other person class inherit from Saving Account class.Saving Account Class Define itself friend of Account holder so This Will Allow Account Holder To Access Private data of Saving account Class .
Perform Friends Operation For above example using Local Class.
Procedure :-
Step 1. Create Class Definition & Class Implementation For Saving Account.
Class Definition

Class Implementation

Step 3. Create Inherited Class Definition & Class Implementation For Account
Holder.

Step 4. Create Inherited Class Definition & Class Implementation For Other
Person.

Step 5. Create Referral Object For Account Holder & Other Person Methods .&
Call Method To Display Amount.

Step 6. Save & Check . It Gives Error Bcz Other Person Don't Have Permission
to Withdraw bcz He is Not a Friend Of Saving Account . For Execution
Comment Withdraw Display Statement.

Step 7. Execute Program It Gives Following Output

0 comments:
Post a Comment