0% found this document useful (0 votes)
116 views3 pages

Class Cls - Name Definition Create Private. ... Endclass

The document discusses private classes in ABAP. A private class can only be instantiated within the methods of the class itself. Immediate subclasses of a private class cannot instantiate it unless they are friends of the class. A private class and its friends can use protected and private components of the class. The document includes an example of a private class CLS_PRIVATE that is instantiated within its own methods and by a friend class CLS_FRND.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views3 pages

Class Cls - Name Definition Create Private. ... Endclass

The document discusses private classes in ABAP. A private class can only be instantiated within the methods of the class itself. Immediate subclasses of a private class cannot instantiate it unless they are friends of the class. A private class and its friends can use protected and private components of the class. The document includes an example of a private class CLS_PRIVATE that is instantiated within its own methods and by a friend class CLS_FRND.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PRIVATE CLASS:

The CREATE addition specifies the context in which the class is instantiated - that is, where the
statementCREATE OBJECT can be executed for this class. A class with the CREATE PUBLIC
addition can be instantiated anywhere that the class is visible. A class with the CREATE
PROTECTED addition can only be instantiated in methods of the class itself and its subclasses.
A class with the CREATE PRIVATE addition can only be instantiated in methods of the class
itself. This means, in particular, that it cannot be instantiated as an inherited component of
subclasses.
Where a class can be instantiated depends on its immediate superclass:
Immediate subclasses of classes with the CREATE PRIVATE addition that are not friends of
the class implicitly receive the CREATE NONE addition. They cannot be instantiated and you
cannot specify any explicit CREATE additions for them.
Immediate subclasses that are friends of the class implicitly inherit the CREATE PRIVATE
addition. All CREATE additions can be specified for all superclasses that can instantiated as
private using friends.
The statement METHODS constructor for the declaration of the instance constructor of a class
declared with the adition CREATE PRIVATE , can not only be listed in the public , but also in
the protected or private visible area . This allows the use of components, which have been
declared there, in the interface of the constructor.
CLASS cls_name DEFINITION CREATE PRIVATE.
...
ENDCLASS.

*&---------------------------------------------------------------------*
*& Report ZOOCL_PRIVATE03
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT

zoocl_private03 NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*
INTERFACE I1
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE i1.
DATA: l_at TYPE i.
ENDINTERFACE.
"I1

CLASS cls_frnd DEFINITION DEFERRED.


*----------------------------------------------------------------------*
*
CLASS CLS_PRIVATE DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls_private DEFINITION FINAL CREATE PRIVATE FRIENDS i1.
PUBLIC SECTION.
CLASS-DATA: obj_pr TYPE REF TO cls_private.
CLASSMETHODS: class_constructor, mt_get_instance RETURNING value(pr_obj) TYPE REF
TO cls_private.
PRIVATE SECTION.
METHODS: mt_write.
ENDCLASS.
"CLS_PRIVATE DEFINITION
*----------------------------------------------------------------------*
*
CLASS CLS_PRIVATE IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls_private IMPLEMENTATION.
METHOD class_constructor.
CREATE OBJECT obj_pr.
ENDMETHOD.
"CLASS_CONSTRUCTOR
METHOD mt_get_instance.
pr_obj = obj_pr.
ENDMETHOD.
"MT_GET_INSTANCE
METHOD mt_write.
WRITE:/ 'PRIVATE CLASS'.
ENDMETHOD.
"MT_WRITE
ENDCLASS.
"CLS_PRIVATE IMPLEMENTATION
*----------------------------------------------------------------------*
*
CLASS CLS_FRND DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls_frnd DEFINITION.
PUBLIC SECTION.
METHODS: mt_obj_private.
INTERFACES: i1.
ENDCLASS.
"CLS_FRND DEFINITION
*----------------------------------------------------------------------*
*
CLASS CLS_FRND IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls_frnd IMPLEMENTATION.
METHOD mt_obj_private.
DATA: obj_pr1 TYPE REF TO cls_private.
obj_pr1 = cls_private=>mt_get_instance( ).
obj_pr1->mt_write( ).
i1~l_at = 10.
WRITE:/ 'INTERFACE ATTRIBUTE: ', i1~l_at.
ENDMETHOD.
"MT_OBJ_PRIVATE
ENDCLASS.
"CLS_FRND IMPLEMENTATION

DATA: obj_fr TYPE REF TO cls_frnd.


START-OF-SELECTION.
CREATE OBJECT obj_fr.
obj_fr->mt_obj_private( ).

You might also like