Showing posts with label Function Modules. Show all posts
Showing posts with label Function Modules. Show all posts

Friday, November 5, 2010

Know the difference between Class and Function Module

Difference between class and a function module?

Ans. Function modules are ABAP routines that are administered in a central function library. They apply across applications and are available throughout the system. You must assign function modules to a function pool that is called a function group. A function group is nothing but a container for the function modules.
Classes are templates for objects. Conversely, you can say that the type of an object is the same as its class. A class is an abstract description of an object. You could say that it is a set of instructions for building an object. The attributes of objects are defined by the components of the class, which describe the state and behavior of objects.

What is wrapper class?

A data structure or software that contains ("wraps around") other data or software, so that the contained elements can exist in the newer system. The term is often used with component software, where a wrapper is placed around a legacy routine to make it behave like an object. This is also called "encapsulation" or "wrapper encapsulation," but is not the same as "object encapsulation," a fundamental concept of object technology.


What are events in classes?

Triggering Events

To trigger an event, a class must

- Declare the event in its declaration part

- Trigger the event in one of its methods

Declaring Events

You declare events in the declaration part of a class or in an interface. To declare instance events, use the following statement:

EVENTS EXPORTING... VALUE() TYPE type [OPTIONAL]..

To declare static events, use the following statement:

CLASS-EVENTS ...

Both statements have the same syntax.

When you declare an event, you can use the EXPORTING addition to specify parameters that are passed to the event handler. The parameters are always passed by value. Instance events always contain the implicit parameter SENDER, which has the type of a reference to the class or the interface in which the event is declared.

Triggering Events

An instance event in a class can be triggered by any method in the class. Static events can be triggered by any static method. To trigger an event in a method, use the following statement:

RAISE EVENT EXPORTING... = ...

For each formal parameter that is not defined as optional, you must pass a corresponding actual parameter in the EXPORTING addition. The self-reference ME is automatically passed to the implicit parameter SENDER.

Handling Events

Events are handled using special methods. To handle an event, a method must

- be defined as an event handler method for that event

- be registered at runtime for the event.


Difference between global and local class?
Local and Global Classes

Classes in ABAP Objects can be declared either globally or locally. You define global classes and interfaces in the Class Builder (Transaction SE24) in the ABAP Workbench. They are stored centrally in class pools in the class library in the R/3 Repository. All of the ABAP programs in an R/3 System can access the global classes. Local classes are defined within an ABAP program. Local classes and interfaces can only be used in the program in which they are defined. When you use a class in an ABAP program, the system first searches for a local class with the specified name. If it does not find one, it then looks for a global class. Apart from the visibility question, there is no difference between using a global class and using a local class.

There is, however, a significant difference in the way that local and global classes are designed. If you are defining a local class that is only used in a single program, it is usually sufficient to define the outwardly visible components so that it fits into that program. Global classes, on the other hand, must be able to be used anywhere. This means that certain restrictions apply when you define the interface of a global class, since the system must be able to guarantee that any program using an object of a global class can recognize the data type of each interface parameter.


Difference between static method and instance method?

Instance Methods

You declare instance methods using the METHODS statement. They can access all of the attributes of a class, and can trigger all of the events of the class.

Static Methods

You declare static methods using the CLASS-METHODS statement. They can only access static attributes and trigger static events.

What is friend class?

In object-oriented programming to allow access to "private" or "protected" data of a class in another class, the latter class is declared as a friend class.

Purpose

A friend class has full access to the private data members of a class without being a member of that class.

Example

A friend class can be declared as shown:

class A

Unknown macro: { private}


What is static constructor and instance constructor?
Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with keyword. A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class.

Since static constructor is a class constructor, they are guaranteed to be called as soon as we refer to that class or by creating an instance of that class.

You may say, why not initialize static data members where we declare them in the code. Like this :

private static int id = 10;
private static string name = "jack";

Static data members can certainly be initialized at the time of their declaration but there are times when value of one static member may depend upon the value of another static member. In such cases we definitely need some mechanism to handle conditional initialization of static members.


What is abstract class?

A class without a instance is abstract classes. Consists of abstract methods whose implementation is to be provided by the user itself. Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.


What is overriding polymorphism?

Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its super-classes. The implementation in the subclass overrides (replaces) the implementation in the super-class.

A subclass can give its own definition of methods which also happen to have the same signature as the method in its super-class. This means that the subclass's method has the same name and parameter list as the super-class's overridden method. Constraints on the similarity of return type vary from language to language, as some languages support covariance on return types.

Method overriding is an important feature that facilitates polymorphism in the design of object-oriented programs.

Some languages allow the programmer to prevent a method from being overridden, or disallow method overriding in certain core classes. This may or may not involve an inability to subclass from a given class.

In many cases, abstract classes are designed — i.e. classes that exist only in order to have specialized subclasses derived from them. Such abstract classes have methods that do not perform any useful operations and are meant to be overridden by specific implementations in the subclasses. Thus, the abstract super-class defines a common interface which all the subclasses inherit.



Monday, December 1, 2008

DATA : lv_stsma TYPE hum_stsma,
lt_status TYPE TABLE OF gty_status,
lk_status TYPE gty_status,
lv_venum TYPE venum,
lv_vhart TYPE vhart.

SELECT SINGLE venum vhart
INTO (lv_venum, lv_vhart)
FROM vekp
WHERE exidv EQ p_exidv.

CONCATENATE gc_hu lv_venum INTO gvc_object.

* Get current status information
SELECT SINGLE stat inact
INTO lk_status
FROM husstat
WHERE objnr EQ gvc_object
AND inact EQ space.

IF sy-subrc = 0.
APPEND lk_status TO lt_status.
ENDIF.

SELECT SINGLE stsma
INTO lv_stsma
FROM tvty
WHERE traty EQ lv_vhart.

CLEAR gk_uphustatus.
REFRESH gt_uphustatus.

gk_uphustatus-objnr = gvc_object.
gk_uphustatus-stat = lk_status-stat.
gk_uphustatus-inact = gc_x.
gk_uphustatus-stsma = lv_stsma.
gk_uphustatus-mod = gc_u.

APPEND gk_uphustatus TO gt_uphustatus.
CLEAR gk_uphustatus.

CALL FUNCTION 'HU_STATUS_UPDATE'
EXPORTING
it_hustatus = gt_uphustatus
it_hustobj = gt_uphustobj.

IF sy-subrc EQ 0.

SELECT SINGLE estat
FROM tj30t
INTO gv_estat_stag
WHERE stsma = lv_stsma
AND spras = gc_e
AND txt04 = gc_stag.

CLEAR gk_uphustatus.
REFRESH gt_uphustatus.

* Check if new status exists, maybe inactive
CLEAR lk_status.
SELECT SINGLE stat inact
INTO lk_status
FROM husstat
WHERE objnr EQ gvc_object
AND stat EQ gv_estat_stag.

IF sy-subrc 0.
gk_uphustatus-mod = gc_i.
ELSE.
gk_uphustatus-mod = gc_u.
ENDIF.

gk_uphustatus-objnr = gvc_object.
gk_uphustatus-stat = gv_estat_stag.
gk_uphustatus-stsma = lv_stsma.

APPEND gk_uphustatus TO gt_uphustatus.
CLEAR gk_uphustatus.

CALL FUNCTION 'HU_STATUS_UPDATE'
EXPORTING
it_hustatus = gt_uphustatus
it_hustobj = gt_uphustobj.

ENDIF.




Wednesday, October 29, 2008

LIST BOX
Drop down list box can be created in a dialog screen(SE51) as well as selection screen.
The sap list box allows to select a value from the list but we cannot enter our own value in the list box .The value list that will be displayed consists of two
fields TEXT field of TYPE 80(C) and internal KEY field of TYPE 40(C).
In screen painter to create a input/output field into list box we use
'L" as a value for dropdown attribute for the i/o field.
In screen painter to determine the type of method that will be used to fill the value
list we use the attribute value list.
If it is blank the value list will be filled by the first column of the input help assigned to the screen field.This input help can be defined in the ABAP Dictionary, on screen using SELECT,VALUES screen statements or in event POV (PROCESS ON VALUE-REQUEST ) and the input help that will be passed to the field should consists of 2 columns ,the key column is filled automatically by the system.SAP recommends value list field should be blank.
or
The value can be 'A' meaning that the value list will be filled in the event PBO(PROCESS BEFORE OUTPUT) or before the screen is displayed.In this method we use function module VRM_SET_VALUES to fill the values and pass it to the i/o field.
If a function code is attached to the list box the selection of a value triggers a PAI
otherwise PAI will not trigger.

LIST BOX in SELECTION SCREEN
List Box is created in selection screen using PARAMETERS staement
with AS LISTBOX addition other attributes like VISIBLE LENGTH (width of listbox)
can be specified with the declaration.
PARAMETERS name(n) AS LISTBOX VISIBLE LENGTH n.
Here n is an integer and name is the name of parameter.
To populate the value list we use the FM VRM_SET_VALUES and the
selection screen event AT SELECTION-SCREEN OUTPUT is used to write the code to fill it.

VRM_SET_VALUES
The function module VRM_SET_VALUES is used to fill the value list associated with a List Box .This FM uses types which are declared in type group VRM. So
we should declare TYPE-POOLS VRM before using this FM.
Some important types declared in the VRM type group are
VRM_ID
It refers to the name of the input/output field associated with list box
VRM_VALUES
It refers to the internal table consisting of two fields TEXT(80C) and KEY(40)C
that will be used to create the list values.
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
ID = name of screen element ,it is of TYPE VRM_ID
VALUES = internal table containing values,of TYPE VRM_VALUES

LIST BOX with value list from input help
In this example the screen element attribute value list is set to blank as such the value list will be filled with the 1st column of the input help,We use PROCESS ON VALUE-REQUEST event to pass the value list to the listbox.In the MODULE call used to fill the value list we can use FM like F4IF_INT_TABLE_VALUE_REQUEST to create input help as explained in the input help.The value of first column will be shown in the field when selected.
PROCESS ON VALUE-REQUEST
FIELD list MODULE fill_list_100
FIELD list MODULE fill_list_100 INPUT
SELECT f1 f2 FROM table INTO int
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'input/output screen field'
value_org = 'S'
TABLES
value_tab = itab "it contains 2 fields that will be shown in the list box
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc <> 0.
...
ENDIF.
ENDMODULE.

VALUE LIST CREATED IN PBO
In this method we set the value list attribute to 'A'.The value list will be filled in the PBO by using FM VRM_SET_VALUES .
TYPE-POOLS : VRM
DATA : field_id TYPE VRM_ID ,
values TYPE VRM_VALUES,
value LIKE LINE OF values.
PROCESS BEFORE OUTPUT
MODULE list_fill_100
MODULE list_fill_100 OUTPUT
SELECT f1 f2 f3 FROM tab WHERE condition.
value-KEY = f1.
value-TEXT = f2
APPEND value TO VALUES
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
id = 'i/o screen field'
values = values.
ENDMODULE.


LIST BOX with Selection Screen
For this the FM VRM_SET_VALUES is used to fill the value table and is passed to the parameter created with TYPE LISTBOX in the selection screen event
AT SELECTION-SCREEN.
PROGRAM zlist
TYPE-POOLS : VRM.
DATA: param TYPE vrm_id,
values TYPE vrm_values,
value LIKE LINE OF values.

PARAMETERS: p_name(10) AS LISTBOX VISIBLE LENGTH 10.
AT SELECTION-SCREEN OUTPUT.
param = 'P_NAME'.
value-key = '1'.
value-text = 'JOHN'.
APPEND value TO values.
value-key = '2'.
value-text = 'PETER'.
APPEND value TO values.
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING id = param
values = values.

ref: http://sap.niraj.tripod.com/id38.html