You are on page 1of 19

(OOP ABAP Topics

)

o
o o

o o o o
o

OOP-Language Features: Encapsulation, Inheritance, Polymorphism, & Abstraction. Class, Reference Variable, and Object. Components of a class: Attributes, Methods, Interfaces & Events. Visibility sections in a class: Public, Protected and Private. A class with public attributes & methods CREATE OBJECT command. A class with Private attributes and Public methods. Constructor Inheritance (Super class and Sub class) Without Overriding With Overriding Containment Is-A & Has-A relationships between classes Functional & Non-functional methods. Different syntaxes to call Functional & Non-functional methods. Class memory & Instance/Object memory Static members (a.k.a Class members) & Non-Static members (a.k.a Instance members) Friends Interfaces Single interface inheritance Aliasing Multiple interface inheritance Global classes (SE24) Events Simple event Event that carries data OOP ALV Interactive OOP ALV (using OOP events)

o
o

o o o
o

o o
o

o o

o

o

then too we are defining a new data type. Protected 3. Attributes (also called as ‘Data Components’) 2. but in addition to this it can have its own processing logic in the form of methods. A data type defined as a class is known as ‘Reference Type’. Private These sections determine the visibility (access) levels of the ‘components of a class’ (* Visibility/access levels will be dealt with later. Protected Section: There is one restriction on the components placed in protected section. All the above mentioned components of a class (except for Interfaces) can be declared in any of the three sections (public. & private sections) of a class. A class in ABAP OOP is coded in two parts: 1. a ‘Class Definition’ must precede ‘Class Implementation’. 2. Attributes / Data Components: Attributes of a class are the data fields (variables & constants) declared in a class. Outside the class’ inheritance hierarchy. except for that they are contained in a class. which include: 1. Class Definition. Two types of classes in ABAP: Global Class: These types of classes are created using class builder tool and will be available globally for all ABAP programs. A ‘Class Definition’ contains declarations of fields and methods but not processing logic. protected. The components of this section are visible / accessible: . Public 2. but a structure has no processing logic of its own (processing logic: code to assign. Attributes of a class can be declared using the keywords DATA or CLASS-DATA. Interfaces and 4. Methods: Methods are similar to subroutines or function modules in the sense that they can accept parameters. A ‘class‘ is similar to a structure in a way that it can hold a set of related fields. (Defined using T.. That is the public components are visible / accessible: 1. read and/or manipulate data in the fields of that structure). we are essentially defining a new data type. In the subclasses. Interfaces and Events will be dealt with later. When we define a structure. 2. Class Implementation. Methods 3.e. Methods of a class can be declared using the keywords METHODS or CLASSMETHODS.) Members / Components of a Class: The contents of a class are referred to as ‘members of a class’ or ‘components of a class’.Code SE24) Local Class: These types of classes are created in an ABAP program and will ba available only to that program where it defined. A structure is a collection of related fields. Every class can have 3 sections: 1. i. Within class in which the components are declared.CLASS: When we define a class. Interfaces can be placed only in public section. and 3. The processing logic is provided in ‘Class Implementation’. Public Section: There are no restrictions on the visibility / access of the components placed in public section. Therefore. contain some code (processing logic) to act on the parameters. Events. the definition has to be coded first and only then the implementation can be coded.

CREATE OBJECT: e1. 2.1. ENDIF. e2. START-OF-SELECTION. Not accessible outside the class’ inheritance hierarchy. 'DNo:'. but 3. "lcl_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp. "show_details ENDCLASS. dno. ename = im_ename. WRITE: / 'ENo:'. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. Example: The class LCL_EMP defined below has only one section (public) and has only attributes defined in it. eno = im_eno. e1->set_details( EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10 ). ENDMETHOD. e3 TYPE REF TO lcl_emp. ENDMETHOD. In the subclasses. dno = im_dno. 'EName:'. eno. Private Section: The visibility of the components placed in private section is highly restricted as they are visible only within the class and nowhere else. ename. PUBLIC SECTION. e2 TYPE REF TO lcl_emp. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. Within class in which the components are declared. DATA: eno TYPE i. "set_details METHOD show_details. ename TYPE string. . show_details. dno TYPE i. METHODS: set_details IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i. METHOD set_details. ENDCLASS. Syntax for creating objects: CREATE OBJECT cref [TYPE class].

WRITE: / 'ENo:'. show_details. METHODS: set_details IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i. "set_details METHOD show_details. *& Bypassing the validation in set_details method *& for valid dept number. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. DATA: eno TYPE i. skip 2. START-OF-SELECTION. e2 TYPE REF TO lcl_emp. ENDIF. dno TYPE i. *& The below line of code if uncommented results in runtime *& error as the object for e3 is not yet created * e3->set_details( EXPORTING im_eno = 103 * im_ename = 'Vinay' * im_dno = 20 ). e2->show_details( ). "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. eno = im_eno. METHOD set_details. dno = im_dno.e2->set_details( EXPORTING im_eno = 102 im_ename = 'Rajesh' im_dno = 30 ). ENDMETHOD. ename = im_ename. dno. . "show_details ENDCLASS. 'DNo:'. e1->show_details( ). . ename TYPE string. 'EName:'. ENDMETHOD. PRIVATE SECTION. by directly accessing the *& the attribute 'dno' (To avoid this place the attributes *& in PRIVATE SECTION) e2->dno = 55. ENDCLASS. ename. *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. e2->show_details( ). eno. "lcl_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp. PUBLIC SECTION.

the constructor method cannot be called explicitly. "set_details . There is no need to explicitly call the constructor method. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. *& The below line of code "e2->dno = 55. Returns the reference of the object to the associated reference variable. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i. eno = im_eno. PUBLIC SECTION. in fact. show_details. PRIVATE SECTION. CONSTRUCTOR: Constructor is a special method that gets implicitly called immediately after an object is created using the CREATE OBJECT command. Allocates the memory required for an object’s attributes (* Instance attributes only) . e2->show_details( ). e1->set_details( EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10 ). e2->show_details( ). METHOD constructor. ENDMETHOD. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. DATA: eno TYPE i. dno TYPE i. *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. The name of this method has to be “CONSTRUCTOR”. *& As the attributes are placed in private section they *& cannot be accessed directly outside the class definition. ename = im_ename. dno = im_dno. 2. ENDCLASS. e2. ENDIF. e2->set_details( EXPORTING im_eno = 102 im_ename = 'Rajesh' im_dno = 30 ).CREATE OBJECT: e1. The CREATE OBJECT command performs 3 tasks: 1." is a syntax error e2->dno = 55. SKIP 2. Calls the method with the name “constructor” of the corresponding class. ename TYPE string. e1->show_details( ).

"show_details ENDCLASS. "lcl_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp. *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. ENDCLASS. methods and so on) and replace or extend inherited methods with its own implementations. 'EName:'. The sub class can also add new components (attributes. show_details. e2->show_details( ). eno = im_eno. WRITE: / 'ENo:'. e1->show_details( ). Inheritance: Inheritance is a relationship in which one class (the sub class) inherits all the main characteristics of another class (the Super class). "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. dno = im_dno. 'DNo:'. CREATE OBJECT: e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10. ename TYPE string. .METHOD show_details. eno. e3 TYPE REF TO lcl_emp. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. ENDMETHOD. "set_details METHOD show_details. ENDIF. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i. ENDMETHOD. METHOD constructor. WRITE: / 'ENo:'. START-OF-SELECTION. The addition ‘INHERITING FROM’ is used in the class definition to indicate from which other class the current class is inheriting from. PUBLIC SECTION. DATA: eno TYPE i. e2 EXPORTING im_eno = 102 im_ename = 'Rajesh' im_dno = 30. ename. ename = im_ename. PRIVATE SECTION. dno. e2 TYPE REF TO lcl_emp. eno. dno TYPE i.

PRIVATE SECTION. "lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_hwage_emp DEFINITION INHERITING FROM lcl_emp. METHOD constructor. "show_details ENDCLASS. 'DNo:'. he1->show_details( ). ENDCLASS. *& it has been inherited from lcl_emp. *& The show_details method is not defined in the class lcl_hwage_emp. "lcl_hwage_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp. WRITE: 'HWage:'. hwage = im_hwage. hwage. show_hwage. CREATE OBJECT e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10. SKIP 2.'EName:'. CREATE OBJECT he1 EXPORTING im_eno = 108 im_ename = 'Suresh' im_dno = 20 im_hwage = 180. he1 TYPE REF TO lcl_hwage_emp. he1->show_hwage( ). PUBLIC SECTION. DATA: hwage TYPE i. ename. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i im_hwage TYPE i. "constructor METHOD show_hwage. START-OF-SELECTION. e1->show_details( ). dno. ENDMETHOD. ENDMETHOD. Method Overriding: Redefining a method in the subclass. super->constructor( im_eno = im_eno im_ename = im_ename im_dno = im_dno ). "show_hwage ENDCLASS. ENDMETHOD. which has been inherited from its superclass. "lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS lcl_hwage_emp IMPLEMENTATION. is called method overriding. While redefining an inherited method the functionality defined in the superclass .

and the sequence of parameters. 3) While overriding a method if the keyword SUPER is not used. dno = im_dno. ename. NOTE: 1) While overriding a method in the subclass.can be retained as is and some more additional functionality can be provided. PUBLIC SECTION. *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. 'EName:'. 2) In the subclass method. the signature of the method has to exactly match with the signature of the corresponding method in the superclass. PRIVATE SECTION. during redefinition (overriding). "show_details ENDCLASS. "lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_hwage_emp DEFINITION INHERITING FROM lcl_emp. WRITE: / 'ENo:'. ENDIF. type of parameters (importing. ENDMETHOD. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. ENDCLASS. . dno TYPE i. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. eno. it means that the method’s functionality as implemented in the superclass is being nullified in the subclass and an altogether new functionality is being provided. dno. Method Signature: The signature of a method includes the number of parameters. show_details REDEFINITION. ename = im_ename. 'DNo:'. or the inherited functionality can be nullified and altogether a new functionality can be provided. ENDMETHOD. exporting and/or changing. eno = im_eno. ename TYPE string. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i im_hwage TYPE i. PRIVATE SECTION. "set_details METHOD show_details. and the data types). METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i. METHOD constructor. if we have to access the implementation of the corresponding method in the superclass we will have to use the keyword SUPER. PUBLIC SECTION. DATA: eno TYPE i. The keyword SUPER is a reference (pseudo reference) to the current class’ superclass implementation. show_details.

he1 TYPE REF TO lcl_hwage_emp. hwage = im_hwage. "lcl_hwage_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_hwage_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_hwage_emp IMPLEMENTATION. he1->show_details( ). ENDMETHOD. METHOD constructor. START-OF-SELECTION. METHODS: constructor IMPORTING im_ad_line1 TYPE string im_city TYPE string im_state TYPE string. ENDCLASS. WRITE: 'HWage:'. state TYPE string. then it is said that X contains a reference to Y. CREATE OBJECT e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10. SKIP 2. "lcl_hwage_emp IMPLEMENTATION ************************** DATA: e1 TYPE REF TO lcl_emp. Has-A Relationship between classes: If a class X has an attribute which is a reference type of class Y. CREATE OBJECT he1 EXPORTING im_eno = 108 im_ename = 'Suresh' im_dno = 20 im_hwage = 180. "show_hwage ENDCLASS. PUBLIC SECTION. hwage. e1->show_details( ). super->constructor( im_eno = im_eno im_ename = im_ename im_dno = im_dno ). ENDMETHOD. PRIVATE SECTION. .DATA: hwage TYPE i. *----------------------------------------------------------------------* * CLASS lcl_address DEFINITION *----------------------------------------------------------------------* CLASS lcl_address DEFINITION. "lcl_address DEFINITION *----------------------------------------------------------------------* * CLASS lcl_address IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_address IMPLEMENTATION. super->show_details( ). The relation between Class X and Class Y is known as Has-A relation (X has-a reference to Y). city TYPE string. ENDCLASS. DATA: ad_line1 TYPE string. show_address. "constructor METHOD show_details.

"constructor METHOD show_address. state. address->show_address( ). create object a1 exporting im_ad_line1 = '#12/A Road No:12' im_city = 'Hyderabad' im_state = 'AP'. ad_line1. eno. START-OF-SELECTION. 'DNo:'. show_details. PRIVATE SECTION. CREATE OBJECT e1 EXPORTING im_eno = 101 im_ename = 'Ajay' im_dno = 10 im_address = a1. ENDMETHOD. ename = im_ename. METHODS: constructor IMPORTING im_eno TYPE i im_ename TYPE string im_dno TYPE i im_address type ref to lcl_address. . dno TYPE i. address = im_address. city. "show_address ENDCLASS. state = im_state. IF im_dno = 10 OR im_dno = 20 OR im_dno = 30. ENDMETHOD. "lcl_emp IMPLEMENTATION ************************** DATA: a1 type ref to lcl_address. 'EName:'. e1 TYPE REF TO lcl_emp. ename. dno. ENDMETHOD. ENDIF. eno = im_eno. ENDCLASS. DATA: eno TYPE i. "lcl_emp DEFINITION *----------------------------------------------------------------------* * CLASS lcl_emp IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_emp IMPLEMENTATION. WRITE: / 'ENo:'. PUBLIC SECTION. address type ref to lcl_address. city = im_city. "lcl_address IMPLEMENTATION *----------------------------------------------------------------------* * CLASS lcl_emp DEFINITION *----------------------------------------------------------------------* CLASS lcl_emp DEFINITION. WRITE: / 'Address'. dno = im_dno. ENDMETHOD. "set_details METHOD show_details. ename TYPE string.METHOD constructor. "show_details ENDCLASS. METHOD constructor. ad_line1 = im_ad_line1.

e1->show_details( ). .

ENDMETHOD. diff TYPE i. get_diff IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_diff) TYPE i. • . diff. START-OF-SELECTION. PARAMETERS: p1 TYPE i. "get_sum METHOD get_diff.im_p2. a functional method cannot be called as part of WRITE statement. Functional methods can be called in assignment operation. • RETURNING parameters are always passed by value and not by reference. and also as part of an expression (logical expressions & arithmetic expressions). METHODS: get_sum IMPORTING im_p1 TYPE i im_p2 TYPE i EXPORTING ex_sum TYPE i.Functional Methods: Methods with RETURNING parameters are known as functional methods (and those without RETURNING parameters are known as non-functional methods. ex_sum = im_p1 + im_p2. However. CREATE OBJECT arith. sum TYPE i. re_diff = im_p1 . / 'Difference:'. PUBLIC SECTION. *& Calling a functional method diff = arith->get_diff( im_p1 = p1 im_p2 = p2 ). *& Calling a non-functional method arith->get_sum( EXPORTING im_p1 = p1 im_p2 = p2 IMPORTING ex_sum = sum ).) A method defined with RETURNING parameters cannot have EXPORTING & CHANGING parameters. p2 TYPE i. WRITE: / 'Sum:'. which is not possible with non-functional methods. "get_diff ENDCLASS. ENDMETHOD. "lcl_arithmetic IMPLEMENTATION *********************************** DATA: arith TYPE REF TO lcl_arithmetic. METHOD get_sum. "lcl_arithmetic DEFINITION CLASS lcl_arithmetic IMPLEMENTATION. ENDCLASS. CLASS lcl_arithmetic DEFINITION. sum.

For this reason the class components can be accessed even before an object of that class is created. memory is allocated separately. and this can be done by accessing the class components through the class name. Which means a method defined as CLASS-METHOD can access only the attributes that have been defined as CLASS-DATA and the methods that have defined as CLASSMETHODS. class_name=>static_member 2. . The class implementation is loaded into the memory only once and that too when the class is referred to for the first time. and the same copy is shared between the class implementation and all the objects of that class. *& Calling a functional method CALL METHOD arith->get_diff EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff. ref_variable->static-member Through the class name (can be accessed before and also after an object of the class has been created). *& Calling a non-functional method CALL METHOD arith->get_sum EXPORTING im_p1 = p1 im_p2 = p2 IMPORTING ex_sum = sum. Class methods can access other class components of the class but cannot access the instance components. CLASS-DATA & CLASS-METHODS Attributes and methods declared CLASS-DATA and CLASS-METHODS. The implementation of a class is loaded only once in the memory. while for each object of a given class that has been created with CREATE OBJECT command. are known as ‘class components’ or ‘static components’. respectively. while those declared DATA and METHODS are known as ‘instance components’ of ‘non-static components’. Each class attribute occupies space in the class memory and hence there can be only one copy of each class attribute.NOTE: There are other valid syntaxes to call methods. instance methods can access both class components as well as instance components of the class. but not those defined as DATA and METHODS. Notes about class & instance components: 1. On the other hand. 4. *& Calling a functional method arith->get_diff( EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff ). which is referred to as Instance-Memory. class_name=>static_member 5. A class member can be accessed in two ways: Through the object reference (only when the reference variable points to an object of the class). which is referred to as ClassMemory. 3.

'Count value thru s2->get_count( ):'. DATA: s1 type ref to lcl_student. stu_count = lcl_student=>get_count( ). DATA: stu_count TYPE i. CREATE OBJECT: s2 exporting imc_id = 102 imc_name = 'Swaroop Kumar'. PRIVATE SECTION. METHOD constructor. WRITE: /3 'ID:'. id. WRITE: /3 'Count after creating first object:'.e. ENDMETHOD. ENDMETHOD. but . s2 type ref to lcl_student. stu_count WRITE: /3 stu_count WRITE: /3 stu_count WRITE: /3 = s1->get_count( ). stu_count. ADD 1 TO count. cnt = count. stu_count. "Count of student objects created ENDCLASS. ENDCLASS. WRITE: /3 'Count after creating second object:'. DATA: id TYPE i. stu_count. "constructor METHOD show_details. The friendship is unidirectional. PUBLIC SECTION. CLASS-METHODS: get_count RETURNING value(cnt) TYPE i. stu_count. 'Name:'. irrespective of whether there is any inheritance relationship between the two. skip 2. stu_count. stu_count = lcl_student=>get_count( ). METHODS: constructor IMPORTING imc_id TYPE i imc_name TYPE string. = lcl_student=>get_count( ). name. METHOD get_count. then the class Y will have access to the private members of class X. name = imc_name. in the above example only class Y will have access to private members of class X. "lcl_student DEFINITION CLASS lcl_student IMPLEMENTATION. = s2->get_count( ). show_details. name TYPE string. 'Count value thru lcl_student=>get_count( ):'.CLASS lcl_student DEFINITION. 'Count value thru s1->get_count( ):'. CLASS-DATA: count TYPE i. stu_count. i. CREATE OBJECT: s1 exporting imc_id = 101 imc_name = 'Vijay Anand'. WRITE: /3 'Count before creating any objects:'. id = imc_id. Friend Classes: If a class X defines another class Y as its friend. "show_details "get_count "lcl_student IMPLEMENTATION ************************************************ START-OF-SELECTION. ENDMETHOD. stu_count = lcl_student=>get_count( ).

but we cannot create an object of an interface. ENDCLASS. ENDMETHOD. Classes that inherit from an interface and provide the implementation are known as the implementation classes of the interface.class X will not have access to the private members of class Y. *& The current code in the class lcl_b can invoke private methods of *& the class lcl_a. Interfaces: An interface is like a class definition but without any implementation of its own. CREATE OBJECT a. WRITE: /3 'From public method of class lcl_b. ENDCLASS. A given class can have multiple other classes defined as its friends. All the components of an interface are public by default. METHOD meth_b. CLASS lcl_a DEFINITION FRIENDS lcl_b. METHOD pvt_method. CLASS lcl_a IMPLEMENTATION. "pvt_method ENDCLASS. *----------------------------------------------------------------------* * INTERFACE lif_add *----------------------------------------------------------------------* INTERFACE lif_add. ENDCLASS. METHODS: meth_b. as the class lcl_a has defined the current class *& lcl_b as its friend a->pvt_method( ). DATA b TYPE REF TO lcl_b. "lif_add . The reference variable of an interface can hold the reference of the object of any of the classes that inherit from the interface and provide implementation to the interface methods. WRITE: /3 'From PRIVATE method of class lcl_a'. METHODS: pvt_method. ENDINTERFACE. PUBLIC SECTION. CLASS lcl_b DEFINITION. DATA: a TYPE REF TO lcl_a. ******************************************** START-OF-SELECTION. CLASS lcl_b IMPLEMENTATION. METHODS: get_sum IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_sum) TYPE i. CLASS lcl_b DEFINITION DEFERRED. CREATE OBJECT b. b->meth_b( ). for this reason visibility specification is not allowed in an interface definition. PRIVATE SECTION. ENDMETHOD. Public is the only visibility scope that is applicable to interface components. A class can inherit from multiple interfaces.'. We can declare reference variables of an interface. The implementation can be provided in the classes that inherit from the interface.

METHODS: get_diff IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_diff) TYPE i. CALL METHOD if_add->get_sum EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING . CALL METHOD arith->get_diff EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff. if_add TYPE REF TO lif_add. "lif_add~get_sum ENDCLASS. METHOD get_diff.im_p2. ENDCLASS. diff. / 'Difference:'. re_sum = im_p1 + im_p2. sum. "get_diff METHOD lif_add~get_sum. "lcl_arithmetic IMPLEMENTATION **************************************************** DATA: arith TYPE REF TO lcl_arithmetic. CALL METHOD arith->lif_add~get_sum EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_sum = sum. re_diff = im_p1 . START-OF-SELECTION. WRITE: / 'Sum:'. if_add = arith. sum TYPE i. "lcl_arithmetic DEFINITION *----------------------------------------------------------------------* * CLASS lcl_arithmetic IMPLEMENTATION *----------------------------------------------------------------------* CLASS lcl_arithmetic IMPLEMENTATION. ENDMETHOD. INTERFACES: lif_add. p2 TYPE i. CREATE OBJECT arith. ENDMETHOD.*----------------------------------------------------------------------* * CLASS lcl_arithmetic DEFINITION *----------------------------------------------------------------------* CLASS lcl_arithmetic DEFINITION. PARAMETERS: p1 TYPE i. diff TYPE i. PUBLIC SECTION.

diff TYPE i. ENDCLASS. div type i. METHODS: get_diff IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_diff) TYPE i. "lcl_arithmetic IMPLEMENTATION **************************************************** DATA: arith TYPE REF TO lcl_arithmetic.re_sum = sum. ENDMETHOD. "lif_add INTERFACE lif_div. sum TYPE i. re_diff = im_p1 . ENDINTERFACE. ENDIF. ENDMETHOD. METHODS: get_sum IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_sum) TYPE i. ENDMETHOD. ENDINTERFACE. ALIASES: get_sum FOR lif_add~get_sum. "lif_add~get_sum METHOD lif_div~get_div. * CALL METHOD if_add->get_diff * EXPORTING * im_p1 = p1 * im_p2 = p2 * RECEIVING * re_diff = diff. . get_div FOR lif_div~get_div. METHOD get_diff. "lcl_arithmetic DEFINITION CLASS lcl_arithmetic IMPLEMENTATION.im_p2. "lif_div~get_div ENDCLASS. "lif_div CLASS lcl_arithmetic DEFINITION. re_div = im_p1 / im_p2. INTERFACES: lif_add. "get_diff METHOD lif_add~get_sum. if_add TYPE REF TO lif_add. Multiple interface inheritane & Aliasing: INTERFACE lif_add. METHODS: get_div IMPORTING im_p1 TYPE i im_p2 TYPE i RETURNING value(re_div) TYPE i. PUBLIC SECTION. IF im_p2 <> 0. lif_div. re_sum = im_p1 + im_p2.

WRITE: / 'Sum:'. p2 TYPE i. CREATE OBJECT arith. / 'Division:'. diff. / 'Difference:'. CALL METHOD arith->get_sum EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_sum = sum. CALL METHOD arith->get_div EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_div = div. "(OR) arith->lif_add~get_sum "(OR) arith->lif_add~get_div Events: . CALL METHOD arith->get_diff EXPORTING im_p1 = p1 im_p2 = p2 RECEIVING re_diff = diff. sum. START-OF-SELECTION.PARAMETERS: p1 TYPE i. div.

(Whenever the method in the former class raises the event. *class_name = Name of the class that contains the event and raises the event. 5) Register the event-handler-method with the object of the class that raises the event. The method that raises the event should be in the same class in which the event has been declared (or in any of its subclasses where the event has been inherited. (Events can be declared in any of the 3 visibility sections of a class. Syntax for associating a method in the handler class with an event: METHODS: <method_name> FOR EVENT <event_name> OF <*class_name>. 2) Define and implement a method that raises (triggers) the event. and the class containing this method as handler-class.) 3) Define another class with a method that is associated with the event of the former class. the method of current class will react to it. .An event is a mechanism set within a class which can help a class to trigger methods of another class. Syntax to declare an event: EVENTS <event_name>. Syntax to register event-handler-method with the object of the class that raises the event: SET HANDLER <event-handler-method> FOR <*object_ref> *object ref = reference variable pointing to the object of the class that raises it. the method handles the event. Steps to code an application using events: 1) Declare an event in a class. in other words.) Syntax to raise (trigger) an event: RAISE EVENT <event_name>. 6) Call the method that raises the event. or in an interface too.) 4) Create objects of the class that raises the event and the handler class. For this reason the method that handles the event is called event-handler-method.