{"id":7644,"date":"2025-08-27T13:08:43","date_gmt":"2025-08-27T07:38:43","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=how-to-implement-friend-class-in-sap"},"modified":"2025-08-27T13:08:43","modified_gmt":"2025-08-27T07:38:43","slug":"how-to-implement-friend-class-in-sap","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=how-to-implement-friend-class-in-sap","title":{"rendered":"How to implement Friend Class in SAP ?"},"content":{"rendered":"<p>A <em>friend class<\/em> can access the <strong>protected<\/strong> and <strong>private<\/strong> components of another class. This is useful when you want to allow controlled access to internal details of a class without making them public.<\/p>\n<p>CLASS main_class DEFINITION.<br \/>\nPUBLIC SECTION.<br \/>\nMETHODS: display.<br \/>\nPRIVATE SECTION.<br \/>\nDATA: secret TYPE string VALUE &#8216;Top Secret&#8217;.<br \/>\nFRIENDS friend_class. &#8221; grant access<br \/>\nENDCLASS.<\/p>\n<p>CLASS main_class IMPLEMENTATION.<br \/>\nMETHOD display.<br \/>\nWRITE: \/ &#8216;Main class display:&#8217;, secret.<br \/>\nENDMETHOD.<br \/>\nENDCLASS.<\/p>\n<p>CLASS friend_class DEFINITION.<br \/>\nPUBLIC SECTION.<br \/>\nMETHODS: reveal IMPORTING obj TYPE REF TO main_class.<br \/>\nENDCLASS.<\/p>\n<p>CLASS friend_class IMPLEMENTATION.<br \/>\nMETHOD reveal.<br \/>\n&#8221; Directly access the private attribute of main_class<br \/>\nWRITE: \/ &#8216;Friend class can access:&#8217;, obj-&gt;secret.<br \/>\nENDMETHOD.<br \/>\nENDCLASS.<\/p>\n<p>START-OF-SELECTION.<br \/>\nDATA(main) = NEW main_class( ).<br \/>\nDATA(frd) = NEW friend_class( ).<\/p>\n<p>main-&gt;display( ).<br \/>\nfrd-&gt;reveal( main ).<\/p>\n<p><strong>Output:\u00a0<\/strong><\/p>\n<p>Main class display: Top Secret<br \/>\nFriend class can access: Top Secret<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,376,377,177],"class_list":["post-7644","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-freind-class","question_tags-oop","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7644"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7644"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7644"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}