Friend Class Indeed

   Introduction:   

  • We will cover the following basic concepts:
  1. Understand the basics of Friend Class.
  2. Create an example to understand the working of a friend class.
  3. Create a program to run the methods of the example classes.
  4. Why do we need a Friend Class?
Lets Start

1. First let us understand theoretically the working of the friend class from the below comparison.
  • Let us compare CLASS INHERITANCE and FRIEND CLASS to understand the basics of friendship.



Comparison Table


2. Let us create an example to demonstrate the working of the Friend Class.

  • Let us create a HANA Eclipse Class 'ZCLASS_FRIEND' -> class granting the friendship.
    • Here we will add the Created Interface 'ZGLOBAL_INTERFACE' in the PUBLIC SECTION.
    • Create the Method 'PRIVATE_FRIEND_METHOD' in the class's PRIVATE SECTION.
    • This class will grant friendship to 'ZCLASS_FR_IMPL' class, which we will create in our next step. 
    • We will use the keyword GLOBAL FRIENDS FRIENDCLASSNAME.
      • GLOBAL FRIENDS ZCLASS_FR_IMPL.
    • Let us also implement the defined methods.
      • LINE 16 to 18:We implement the private method of the class.
      • LINE 19 to 22:We implement the interface method and we will even pass value to our one and only attribute of the Interface.
    • Save the Class, do not activate it now. Let us do it after the creation of the friend class  ZCLASS_FR_IMPL.

  • Now, let us create the friend class 'ZCLASS_FR_IMPL'.
  • Let us add Public and Private Methods as shown below.
    • PUBLIC_METHOD
    • PRIVATE_METHOD
  • LINE 14 to 20 :
    • Let us call the private method of the class granting friendship-> ZCLASS_FRIEND.
    • And also we will write the value of the Interface Attribute-> IV_INTERFACE_ATTR.


  • Now let's create a Sub Class  'ZCLASS_FR_IMPL_SUB' for Super Class 'ZCLASS_FR_IMPL' and extend our example.
  • Interestingly, we can call components of the ZCLASS_FRIEND->Class granting the friendship.
  • Even the PRIVATE SECTION of the ZCLASS_FRIEND Class is accessible here.

  • Now let us try to access the components of the Inherited Class of our ZCLASS_FRIEND Class.

  • And interestingly we will not be able to call the inherited Components of the class ZCLASS_FRIEND.
  • Hence, we will get the following error.



3. Now let us create a program to demonstrate the working of the above friend class.
  • We will call methods of the sub-class ZCLASS_FR_IMPL_SUB( ) of the friend class ZCLASS_FR_IMPL( ).
  • Below example shows how the inheritance tree accesses the private sections in the friendship.



4. So, why Friend Class then?
  • From the above example, we know that the class granting friendship provides access to all the visibility sections' components.
  • Even the sub-class of the friend class gets this privilege of accessing all the components of the class granting friendship.
  • So, this facility will help a developer with the reusability of the components wherever required.
  • But one has to be careful and even restrict a friend class's inheritance tree from accessing the components in the friendship.
  • We can make the friend class (ZCLASS_FR_IMPL( ) in our case) a FINAL CLASS, this will make friendship from class ZCLASS_FRIEND() exclusive to ZCLASS_FR_IMPL( ).
Example Code:
*Global Interface 
interface ZGLOBAL_INTERFACE
  public.

  CLASS-DATA IV_INTERFACE_ATTR type CHAR25 .

  methods METHOD .
endinterface.


*Friend Class
CLASS zclass_fr_impl DEFINITION
  PUBLIC
  CREATE PUBLIC.

  PUBLIC SECTION.
    METHODS:public_method.
  PROTECTED SECTION.
  PRIVATE SECTION.
    METHODS:private_method.
ENDCLASS.

CLASS zclass_fr_impl IMPLEMENTATION.
  METHOD public_method.
    DATA(lr_zclass_friend) = NEW zclass_friend(  ).
    DATA(lr_zclass_friend_sub) = NEW zclass_friend_sub(  ).
    lr_zclass_friend->private_friend_method(  ).
    WRITE /.
    lr_zclass_friend->zglobal_interface~method( ).
    WRITE /.
    WRITE:zclass_friend=>zglobal_interface~iv_interface_attr.
  ENDMETHOD.
  METHOD private_method.
    WRITE 'Private method of the friend Class Method'.
  ENDMETHOD.
ENDCLASS.

*Sub Class of Friend Class
CLASS zclass_fr_impl_sub DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC INHERITING FROM zclass_fr_impl.

  PUBLIC SECTION.
    METHODS:sub_public_method.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zclass_fr_impl_sub IMPLEMENTATION.
  METHOD sub_public_method.
    DATA(lr_zclass_friend) = NEW zclass_friend(  ).
    lr_zclass_friend->private_friend_method(  ).
    WRITE /.
    lr_zclass_friend->zglobal_interface~method( ).
    WRITE /.
    WRITE:zclass_friend=>zglobal_interface~iv_interface_attr.
  ENDMETHOD.
ENDCLASS.


*Class granting friendship
CLASS zclass_friend DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC
  GLOBAL FRIENDS zclass_fr_impl.
  PUBLIC SECTION.
    INTERFACES:zglobal_interface.
  PROTECTED SECTION.
  PRIVATE SECTION.
    METHODS:private_friend_method.
ENDCLASS.

CLASS zclass_friend IMPLEMENTATION.
  METHOD private_friend_method.
    WRITE:'Private Method'.
  ENDMETHOD.
  METHOD zglobal_interface~method.
     zglobal_interface~iv_interface_attr = 'INTERFACE ATTRIBUTE'.
     WRITE:'Interface Method'.
  ENDMETHOD.
ENDCLASS.

* Sub Class of  Class granting friendship
CLASS zclass_friend_sub DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES:zglobal_interface.
  PROTECTED SECTION.
  PRIVATE SECTION.
    METHODS:private_friend_sub_method.
ENDCLASS.

CLASS zclass_friend_sub IMPLEMENTATION.
  METHOD:private_friend_sub_method.
    WRITE:'Sub-Class of the class providing friendship'.
  ENDMETHOD.
  METHOD zglobal_interface~method.
     zglobal_interface~iv_interface_attr = 'INTERFACE ATTRIBUTE'.
     WRITE:'Interface Method of the Sub-Class of the class providing friendship'.
  ENDMETHOD.
ENDCLASS.

*Program
REPORT zfriend_pgm.
DATA(lr_zclass_fr_impl_sub) = NEW zclass_fr_impl_sub(  ).
lr_zclass_fr_impl_sub->public_method(  ).
WRITE /.
lr_zclass_fr_impl_sub->sub_public_method(  ).

Comments

Popular posts from this blog

Class Visibility

Special Method: Constructor

Class Inheritance