cancel
Showing results for 
Search instead for 
Did you mean: 

Reg. OOPS....friend class....have ur pont.s

Former Member
0 Kudos

Hi all,

we have diff. syntaxes to make friend class say..

CLASS A DEFINTION CREATE PRIVATE FRIEND CLASS B.

CLASS A DEFINTION FRIEND CLASS B.

Pleas reply..

<b>Have ur point.</b>

Regards,

pradeep phogat

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi

<b>Friend Concepts</b>

In rare cases, classes have to work together so closely that they need access to their protected and private components. To avoid making these components available to all users, there is the concept of friendship between classes.

To do this you use the FRIENDS additions to the CLASS statement, in which all classes and interfaces that are to be provided friendship are listed

In principle, providing friendship is one-sided: A class providing friendship is not automatically a friend of its friends. If a class providing friendship wants to access the non-public components of a friend, this friend has to explicitly provide friendship to it.

Classes that inherit from friends and interfaces that contain a friend as a component interface also become friends. However, providing friendship, unlike the attribute of being a friend, is not inherited. A friend of a superclass is therefore not automatically a friend of its subclasses.

yes we can declare in this 2 ways

CLASS A DEFINTION CREATE PRIVATE FRIEND CLASS B.

CLASS A DEFINTION FRIEND CLASS B.

Answers (1)

Answers (1)

Former Member
0 Kudos

The specified object types (classes or interfaces) obj_type_1 ... obj_type_n are declared as friends of the current class. In this way, these object types and all the object types derived from these through inheritance, inclusion, and implementation are allowed to access PROTECTED and PRIVATE components, in addition to PUBLIC components. They can also create instances from the class, irrespective of the specifications in the CREATE addition. All classes and interfaces from the same program as well as global classes and interfaces from the class library can be specified as object types.

Note

If you define global classes and class library interfaces as friends, you should note that the local classes of other ABAP programs will not be visible in them. A static access to the componenents of the class class is not possible in such friends.

Example

interface I1.

...

endinterface.

class C1 definition create private friends I1.

private section.

data A1(10) type C value 'Class 1'.

endclass.

class C2 definition.

public section.

interfaces I1.

methods M2.

endclass.

class C2 implementation.

method M2.

data OREF type ref to C1.

create object OREF.

write OREF->A1.

endmethod.

endclass.

data OREF type ref to C2.

start-of-selection.

create object OREF.

OREF->M2( ).

In this excample, classs C2 is a friend of the interface

I1 and therefore of the implementing class C1. It can

instance these and also access their private components.

Addition 7

... GLOBAL FRIENDS publ_obj_type_1 ... publ_obj_type_n

Effect

This addition must be used only for the definition of global classes. It cannot, therefore be specified locally with the ABAP Editor in an ABAP program, but is automatically generated during the definition of a global class using the tool Class Builder of the ABAP workbench. The specified global object types (classes or interfaces) publ_obj_type_1 ... publ_obj_type_n are declared as friends of the current class. In this way, these objects and all the object types derived from these through inheritance, inclusion, and implementation are allowed to access PROTECTED and PRIVATE components, in addition to the PUBLIC components. They can also create instances from the class, irrespective of the specifications in the addition CREATE

Rewards if useful.......................

Minal