Classes
A class is used to specify the form of an object and it combines data and methods for manipulating that data into one neat package.
Class Definition and Implementation
The following syntax shows how to define a class −
CLASS <class_name> DEFINITION.
..........
..........
ENDCLASS.
..........
..........
ENDCLASS.
A class definition starts with the keyword CLASS followed by the class name, DEFINITION and the class body. The definition of a class can contain various components of the class such as attributes, methods, and events. When we declare a method in the class declaration, the method implementation must be included in the class implementation. The following syntax shows how to implement a class −
CLASS <class_name> IMPLEMENTATION.
...........
..........
ENDCLASS.
...........
..........
ENDCLASS.
Note − Implementation of a class contains the implementation of all its methods. In ABAP Objects, the structure of a class contains components such as attributes, methods, events, types, and constants.
Attributes
Attributes are data fields of a class that have a data type. They are declared in the tab "Attributes" of class declaration. These attributes can be divided into 2 categories: instance and static attributes.
An instance attribute defines the specific state of an object. The states are different for different objects. An instance attribute is declared by using the DATA statement.
Static attributes define a common state of a class that is shared by all the instances of the class. That is, if you change a static attribute in one object of a class, the change is affected to all other objects of the class as well. A static attribute is declared by using the CLASS-DATA statement.
Methods
A method is a procedure that represents the behavior of an object in the class.
Method Parameters
The definition of a method can contain parameters, so that you can supply the values to these parameters at the call of method.
Parameter type can use a type in a type group (Properties --> define Type group/Object type).
Type of parameters can be Importing, Exporting, Changing and Returning.
With Return type, the method will pass value to its parameter and return after its call by using an assignment operator.
CLASS cls DEFINITION.
PUBLIC SECTION.
METHODS
meth
IMPORTING
name TYPE string
RETURNING
VALUE(result) TYPE string.
ENDCLASS.
CLASS cls IMPLEMENTATION.
METHOD meth.
result = |Hello { name }!|.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
cl_demo_output=>display(
NEW cls( )->meth( CONV #( sy-uname ) ) ).
Accessing Attributes and Methods
Class components can be defined in public, private, or protected visibility sections that control how these components could be accessed. The private visibility section is used to deny access to components from outside of the class. Such components can only be accessed from inside the class such as a method.
Components defined in the public visibility section can be accessed from any context. By default all the members of a class would be private. Practically, we define data in private section and related methods in public section so that they can be called from outside of the class as shown in the following program.
- The attributes and methods declared in Public section in a class can be accessed by that class and any other class, sub-class of the program.
- When the attributes and methods are declared in Protected section in a class, those can be accessed by that class and sub classes (derived classes) only.
- When the attributes and methods are declared in Private section in a class, those can be accessed by only that class and not by any other class.
Example
Report ZAccess1.
CLASS class1 Definition.
PUBLIC Section.
Data: text1 Type char25 Value 'Public Data'.
Methods meth1.
PROTECTED Section.
Data: text2 Type char25 Value 'Protected Data'.
PRIVATE Section.
Data: text3 Type char25 Value 'Private Data'.
ENDCLASS.
CLASS class1 Implementation.
Method meth1.
Write: / 'Public Method:',
/ text1,
/ text2,
/ text3.
Skip.
EndMethod.
ENDCLASS.
Start-Of-Selection.
Data: Objectx Type Ref To class1.
Create Object: Objectx.
CALL Method: Objectx→meth1.
Write: / Objectx→text1.
CLASS class1 Definition.
PUBLIC Section.
Data: text1 Type char25 Value 'Public Data'.
Methods meth1.
PROTECTED Section.
Data: text2 Type char25 Value 'Protected Data'.
PRIVATE Section.
Data: text3 Type char25 Value 'Private Data'.
ENDCLASS.
CLASS class1 Implementation.
Method meth1.
Write: / 'Public Method:',
/ text1,
/ text2,
/ text3.
Skip.
EndMethod.
ENDCLASS.
Start-Of-Selection.
Data: Objectx Type Ref To class1.
Create Object: Objectx.
CALL Method: Objectx→meth1.
Write: / Objectx→text1.
The above code produces the following output −
Public Method:
Public Data
Protected Data
Private Data
Public Data
Public Data
Protected Data
Private Data
Public Data
Friend Class
There are certain special 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.
If a class A declares itself as a friend to class B then class B will have access to all the private and protected attributes and methods of class A.
CREATE PROTECTED/PRIVATE Addition
CLASS class DEFINITON CREATE PROTECTED|PRIVATE.
When using the extension CREATE PROTECTED, the class can only be instantiated from the methods of the class itself, the friend class or by the subclasses. This makes for unique instance management.
Using the extension CREATE PRIVATE, the class can only be instantiated from the methods of the class itself and its friend class.
Object
An object is a special kind of variable that has distinct characteristics and behaviors. The characteristics (attributes) of an object are used to describe the state of an object, and behaviors (methods) represent the actions performed by an object.
An object is a pattern or instance of a class. It represents a real-world entity such as a person or a programming entity like variables and constants. For example, accounts and students are examples of real-world entities.
Creating an Object
The object creation usually includes the following steps −
- Creating a reference variable with reference to the class. The syntax for which is −
DATA: <object_name> TYPE REF TO <class_name>.
- Creating an object from the reference variable. The syntax for which is −
CREATE Object: <object_name>.
Example
REPORT ZDEMO_OBJECT.
CLASS Class1 Definition.
Public Section.
DATA: text1(45) VALUE 'ABAP Objects.'.
METHODS: Display1.
ENDCLASS.
CLASS Class1 Implementation.
METHOD Display1.
Write:/ 'This is the Display method.'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: Class1 TYPE REF TO Class1.
CREATE Object: Class1.
Write:/ Class1->text1.
CALL METHOD: Class1->Display1.
CLASS Class1 Definition.
Public Section.
DATA: text1(45) VALUE 'ABAP Objects.'.
METHODS: Display1.
ENDCLASS.
CLASS Class1 Implementation.
METHOD Display1.
Write:/ 'This is the Display method.'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: Class1 TYPE REF TO Class1.
CREATE Object: Class1.
Write:/ Class1->text1.
CALL METHOD: Class1->Display1.
The above code produces the following output −
ABAP Objects.
This is the Display method.
This is the Display method.
No comments:
Post a Comment