- SAP ABAP - Web Dynpro
- SAP ABAP - Business Add-Ins
- SAP ABAP - User Exits
- SAP ABAP - Customer Exits
- SAP ABAP - SAPscripts
- SAP ABAP - Smart Forms
- SAP ABAP - Dialog Programming
- SAP ABAP - Report Programming
- SAP ABAP - Object Events
- SAP ABAP - Interfaces
- SAP ABAP - Encapsulation
- SAP ABAP - Polymorphism
- SAP ABAP - Inheritance
- SAP ABAP - Classes
- SAP ABAP - Objects
- SAP ABAP - Object Orientation
- SAP ABAP - Deleting Internal Tables
- SAP ABAP - Reading Internal Tables
- SAP ABAP - Copying Internal Tables
- ABAP - Populating Internal Tables
- SAP ABAP - Creating Internal Tables
- SAP ABAP - Internal Tables
- SAP ABAP - Native SQL Overview
- SAP ABAP - Open SQL Overview
- SAP ABAP - Include Programs
- SAP ABAP - Function Modules
- SAP ABAP - Macros
- SAP ABAP - Subroutines
- SAP ABAP - Modularization
- SAP ABAP - Lock Objects
- SAP ABAP - Search Help
- SAP ABAP - Views
- SAP ABAP - Structures
- SAP ABAP - Tables
- SAP ABAP - Data Elements
- SAP ABAP - Domains
- SAP ABAP - Dictionary
- SAP ABAP - Exception Handling
- SAP ABAP - Formatting Data
- SAP ABAP - Date & Time
- SAP ABAP - Strings
- SAP ABAP - Decisions
- SAP ABAP - Loop Control
- SAP ABAP - Operators
- SAP ABAP - Constants & Literals
- SAP ABAP - Variables
- SAP ABAP - Data Types
- SAP ABAP - Basic Syntax
- SAP ABAP - Screen Navigation
- SAP ABAP - Environment
- SAP ABAP - Overview
- SAP ABAP - Home
SAP ABAP Useful Resources
- SAP ABAP - Discussion
- SAP ABAP - Useful Resources
- SAP ABAP - Quick Guide
- SAP ABAP - Questions Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SAP ABAP - Classes
A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
Class Definition and Implementation
When you define a class, you define a blueprint for a data type. This doesn t actually define any data, but it does define what the class name means, what an object of the class will consist of, and what operations can be performed on such an object. That is, it defines the abstract characteristics of an object, such as attributes, fields, and properties.
The following syntax shows how to define a class −
CLASS <class_name> DEFINITION. .......... .......... 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.
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 can have any data type such as C, I, F, and N. They are declared in the class declaration. These attributes can be spanided into 2 categories: instance and static attributes. An instance attribute defines the instance 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 visible 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 function or procedure that represents the behavior of an object in the class. The methods of the class can access any attribute of the class. The definition of a method can also contain parameters, so that you can supply the values to these parameters when methods are called. The definition of a method is declared in the class declaration and implemented in the implementation part of a class. The METHOD and ENDMETHOD statements are used to define the implementation part of a method. The following syntax shows how to implement a method −
METHOD <m_name>. .......... .......... ENDMETHOD.
In this syntax, <m_name> represents the name of a method. Note − You can call a method by using the CALL METHOD statement.
Accessing Attributes and Methods
Class components can be defined in pubpc, private, or protected visibipty sections that control how these components could be accessed. The private visibipty 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 pubpc visibipty 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 pubpc section so that they can be called from outside of the class as shown in the following program.
The attributes and methods declared in Pubpc 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 Pubpc 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: / Pubpc 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 −
Pubpc Method: Pubpc Data Protected Data Private Data Pubpc Data
Static Attributes
A Static attribute is declared with the statement CLASS-DATA. All the objects or instances can use the static attribute of the class. Static attributes are accessed directly with the help of class name pke class_name⇒name_1 = Some Text .
Example
Following is a program where we want to print a text with pne number 4 to 8 times. We define a class class1 and in the pubpc section we declare CLASS-DATA (static attribute) and a method. After implementing the class and method, we directly access the static attribute in Start-Of-Selection event. Then we just create the instance of the class and call the method.
Report ZStatic1. CLASS class1 Definition. PUBLIC Section. CLASS-DATA: name1 Type char45, data1 Type I. Methods: meth1. ENDCLASS. CLASS class1 Implementation. Method meth1. Do 4 Times. data1 = 1 + data1. Write: / data1, name1. EndDo. Skip. EndMethod. ENDCLASS. Start-Of-Selection. class1⇒name1 = ABAP Object Oriented Programming . class1⇒data1 = 0. Data: Object1 Type Ref To class1, Object2 Type Ref To class1. Create Object: Object1, Object2. CALL Method: Object1→meth1, Object2→meth1.
The above code produces the following output −
Constructors
Constructors are special methods that are called automatically, either while creating an object or accessing the components of a class. Constructor gets triggered whenever an object is created, but we need to call a method to trigger the general method. In the following example, we have declared two pubpc methods method1 and constructor. Both these methods have different operations. While creating an object of the class, the constructor method triggers its operation.
Example
Report ZConstructor1. CLASS class1 Definition. PUBLIC Section. Methods: method1, constructor. ENDCLASS. CLASS class1 Implementation. Method method1. Write: / This is Method1 . EndMethod. Method constructor. Write: / Constructor Triggered . EndMethod. ENDCLASS. Start-Of-Selection. Data Object1 Type Ref To class1. Create Object Object1.
The above code produces the following output −
Constructor Triggered
ME Operator in Methods
When you declare a variable of any type in pubpc section of a class, you can use it in any other implementation. A variable can be declared with an initial value in pubpc section. We may declare the variable again inside a method with a different value. When we write the variable inside the method, the system will print the changed value. To reflect the previous value of the variable, we have to use ‘ME’ operator.
In this program, we have declared a pubpc variable text1 and initiated with a value. We have declared the same variable again, but instantiated with different value. Inside the method, we are writing that variable with ‘ME’ operator to get the previously initiated value. We get the changed value by declaring directly.
Example
Report ZMEOperator1. CLASS class1 Definition. PUBLIC Section. Data text1 Type char25 Value This is CLASS Attribute . Methods method1. ENDCLASS. CLASS class1 Implementation. Method method1. Data text1 Type char25 Value This is METHOD Attribute . Write: / ME→text1, / text1. ENDMethod. ENDCLASS. Start-Of-Selection. Data objectx Type Ref To class1. Create Object objectx. CALL Method objectx→method1.
The above code produces the following output −
This is CLASS Attribute This is METHOD AttributeAdvertisements