Professional Documents
Culture Documents
Program namespaces start with A TO X are reserved for sap use means designed and
developed by SAP.
Modifications and programming only possible with a developer key and correct
configuration set up.
DECLARING VARAIBLE:
Data num1 type i.
Data num2 type I value 5.
Data name type c length 20 value ‘Sunil’.
Data num2 like num1.
Data salary type I value ‘2222.66’. ||when specifying decimals u need to use single
Quotes because period will give error.
Chained statements
Data : num1 type I,
String type string,
……
……
DECLRING CONSTANTS:
Fixed value data objects that we cannot change the values.
Constants country code type c value ‘GB’.
Constants pi type f value ‘3.14’.
Structure syst is having many system defined variables.
If we want to see structures click on data type and give structure name.
INTERNAL TABLES:
ABAP allows us to copy data between two structures even if they ae not the same type.
Example is we can transfer data between work areas like wa_vbap and wa_vbak.
This will done based on actual component name(field name) of the structure.
SUBROUTINES :
Similar to methods / functions in other languages.
Provides encapsulation of functionality.
Permits passing of parameters.
Form mysub.
1 or more statements.
END form.
Perform mysub.
Subroutines are placed at the end of the program.
Subroutines can see and manipulate data – bad practice to do so.
Breaks encapsulation principles.
SUB ROUTINES :
Actual parameters are copied /transferred to the parameters listed in the subroutine
interface.
Methods of passing parameters:
Call by value – the parameter value is copied to the subroutine.
Call by reference – The parameter memory location is passed to the subroutine
resulting in the subroutine operating on actual parameter.
Call by value and result – The parameter value is copied to the subroutine. If the end of
the subroutine is reached with out any errors, the subroutine parameters values are
copied back to the actual (caller ) parameters.
Form mysub.
1 or more statements
…
..
END FORM.
Perform mysub.
STATIC ATTRIBUTES:
A static attribute is a shared attribute across all objects based on a class.
It is not dependent on instantiation of each object. This means we can access a static
attribute without creating an object.
Used for things like object counters.
All of the objects in a class have access to static attributes.
Declare them using the class-data statement.
Class myclass definition.
Public section. [private section]
Class-data name type type.
ENDCLASS.
Changing the value of a static attribute will make the changed value visible in all other
objects created from that class.
Static attributes are useful when we want to keep track of things like object counters.
Suppose we created five objects from a specific class, but we can use a static attribute to
keep track. Then we can find how many we created.
All of the objects have access to that same static attribute.
They all reference the same memory area.
Changing the value of a static attribute will make the change to value visible in any
object that is instantiated from that class.
Static attribute declaration:
CLASS-DATA: count type i.
METHODS:
the method definition indicates a methods:
.interface :
Method parameters
Method return type (method signature)
Method exception.
Methods may be designated as public or private.
A private method may only be called from within another method of the
class.
Just as we have class data(static attributes) we can have class-methods too.
These methods may only access static attributes.
Class methods may be invoked using the name of the class or an object of
the class.
An object does not need to be instantiated to invoke a class method.
METHOD DEFINITION
Within the class definition, We define the method definition which specifies the
method’s interface.
.method parameters
.method return type(method signature)
.Method exception.
Methods may be specified as public or private.
A private method may only be called from within another method of the class.
Functional methods
if you want your methods to return a single value , you can create a functional method.
Use RETURNING in the methods interface.
When using returning, the exporting and changing parameters can not be used.
To return the value, you assign the value to the name of the data object identified in the
RETURNING statement.
You do not use a RETURN statement.
This gives us the ability to create Methods that can be called within other statements
such as IF,CASE,MOVE and COMPUTE.
CLASS myclass DEFINITION.
PUBLIC SECTION
…
Methods Method1 IMPORTING var1 TYPE I OPTIONAL
Value(var2) Type i
Exporting var3 type I
Changing var4 type i.
Methods method2 importing var1 type I default 3
RETURNING value(retval) type i.
Private section
… “attributes/method definitions.
Endclass
METHODS – Implementation
Place the code for the method functionality in the Class implementation.
All parameters and types have already been defined in definition.
Class myclass IMPLEMENTATION.
METHOD Method1.
“
“
ENDMETHOD.
METHOD Method2.
“
Endmethod.
ENDCLASS.
Foe definition, we have to use keyword METHODS but for the implementation we will
use Keyword method.
METHOD viewcar.
WRITE: / ‘Make = ‘, 19 make.
….some write statements.
ENDMETHOD.
METHOD setnumseats.
Numseats = newseatnum.
ENDMETHOD.
METHOD gofaster.
DATA tmspeed TYPE i.
Tmspeed = speed + increment.
IF tmspeed <= maxSpeed.
Speed = speed + increment
ENDIF.
Result = speed.
ENDMETHOD.
METHOD goslower.
DATA tmspeed TYPE i.
Tmspeed = speed - increment.
IF tmspeed >= 0.
Speed = speed - increment
ENDIF.
Result = speed.
ENDCLASS.
START-OF-SELECTION.
DATA car1 TYPE REF TO car.
CREATE OBJECT car1
EXPORTING
Make = ‘Audi’
Model = ‘A4’
Numseats = 5
Maxspeed = 120.
Constructor is executed when an object is instantiated.
Here the tutor moved the attributes to private section because this class updates these
attributes by its own methods. That means a call in program will not be able to update
these attributes directly.
Creating an Object.
Define an object reference variable.
This is a pointer to object of a class.
Create the object
We create an instance(variable name) of the class.
Object definintions
START-OF-SELECTION.
DATA objectname TYPE REF TO classname.
CREATE OBJECT objectname.
constructor method will get executed for each instance of the class that is created.
Self - reference
When referring to components of a class inside the class itself we normally just use the
component name.
Where we have some ambiguity like in our example we can use the Self-reference
syntax to refer to an objects attributes ,methods and the object itself.
Me->attribute_name
This makes use of the local reference variable me that refers to the object.
STATIC -CONSTRUCTOR
Similar to a normal constructor but is executed the first time a:
. Object of a class is created.
.static attribute of the class is used.
.Static method of the class is used.
Must be declared in the PUBLIC SECTION
No interface parameters can be defined.
Can only access static attributes of their class is called class constructor.
Syntax: CLASS-METHODS class_constructor
First class constructor will be called and later normal constructor method will be called.
Parameters passing types of the calling program correspond to the parameter types in a
method. But they are not same.
CODING EXAMPLES OF USING ATTRIBUTES AND METHODS IN A
CALLING PROGRAM
SAPFLI table :
The program provided by SAP to fill these tables in your system is a standard called,
‘SAPBC_DATA_GENERATOR’, which can also be accessed through the standard
transaction ‘BC_DATA_GEN’.
Note: Advisable that you run this program in background mode.
You need to give tcode as bc_data_gen
INHERITANCE
Inheritance allows us to define NEW classes from existing ones.
The new class inherits all the attributes and methods of the higher level class.
We can then expand and enhance the new class.
VEHICLE
CAR BOAT TRAIN
BMW AUDI SAIL BOAT SPEED BOAT ELECTRICTRAIN STEAM TRAIN