05-14-2007 6:03 AM
hi
can any one provide basic material for underatanding object oriented in abap...with many sample programs(preferably)....u can also provide me links....
thank you,
Ginni
05-14-2007 6:51 AM
Hello Ginni ,
Procedure oriented approach
1. Emphasis on tasks
2. Large programs are divided into smaller programs known as functions
3. Most of the functions share global data
4. Data move openly around the system from function to function
<b>Object oriented approach</b>
Emphasis on things that does those tasks
Programs are divided into objects
3. Data structures are designed such that they characterized the objects
4. Functions that operate on the data of an object are tied together in the data structure
5. Data can be hidden and cannot be accessed by external functions
6. New data and functions can be easily added whenever necessary
<b>Object Oriented Approach (key features)</b>
1. Better Programming Structure
2. Real world entity can be modeled very well
3.Stress on data security and access
4. Data encapsulation and abstraction
5. Reduction in code redundancy
Examples...
<b>1.1 Accessibility of different sections of a class</b>
Theme From this program, one will learn:-
1. How to define, implement and instantiate a class.
2. What are the different sections of visibility in a class.
3. How to define instance attributes and get them accessed by external users.
The following program will also show that :-
 Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.
 Data declared in the protected section can be accessed by the class itself, and also by its subclasses but not by external users outside the class.
 Data declared in the private section can be accessed by the class only, but not by its subclasses and by external users outside the class.
Brief Description This program contains a class : parentclass with following attributes in different sections:-
Commondata in public section
Protectdata in private section
Privatedata in private section
The method showval in class : parentclass displays values of all the attributes.
This demonstrates that class can access all its attributes.
Class childclass is a subclass of class parentclass, which has a method : subval.
It displays the value for the data : commondata and protectdata .
Then, it changes the values for both and displays them again.
This demonstrates that subclass can access/change public/ protected attributes of superclass.
In the START-OF-SELECTION event, object : parent is instantiated from class : parentclass and object : child is instantiated from class : childclass.
Then , the method showval of parent(object of parentclass) and method subval of child(object of childclass) is called , which displays the values of different attributes.
Then, the public attribute of object parent is changed and the changed value is displayed.
This demonstrates that external users can change/display public attributes of a class.
Dump of the program:-
REPORT YSUBDEL LINE-SIZE 120.
CLASS parentclass DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value 'Accessible to all'.
METHODS : SHOWVAL.
PROTECTED SECTION.
DATA : protectdata(40) type c value 'Protected data'.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
CLASS parentclass IMPLEMENTATION.
METHOD : SHOWVAL.
write:/5 'All data from parentclass shown:-'.
write:/ sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA,
/5 PRIVATEDATA.
endmethod.
endclass.
CLASS childclass DEFINITION INHERITING FROM parentclass.
PUBLIC SECTION .
METHODS : subval.
ENDCLASS.
CLASS childclass IMPLEMENTATION.
method : subval.
skip 1.
write:/5 'Data of parent shown from child-'.
write:/5 sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA.
Commondata = 'Public data changed in subclass'.
Protectdata = 'Protected data changed in subclass'.
write:/5 sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA.
endmethod.
endclass.
I hope this will help u a lot
<b>Dont forget to reward points</b>
Thanks,
Sachin
05-14-2007 6:51 AM
Hello Ginni ,
Procedure oriented approach
1. Emphasis on tasks
2. Large programs are divided into smaller programs known as functions
3. Most of the functions share global data
4. Data move openly around the system from function to function
<b>Object oriented approach</b>
Emphasis on things that does those tasks
Programs are divided into objects
3. Data structures are designed such that they characterized the objects
4. Functions that operate on the data of an object are tied together in the data structure
5. Data can be hidden and cannot be accessed by external functions
6. New data and functions can be easily added whenever necessary
<b>Object Oriented Approach (key features)</b>
1. Better Programming Structure
2. Real world entity can be modeled very well
3.Stress on data security and access
4. Data encapsulation and abstraction
5. Reduction in code redundancy
Examples...
<b>1.1 Accessibility of different sections of a class</b>
Theme From this program, one will learn:-
1. How to define, implement and instantiate a class.
2. What are the different sections of visibility in a class.
3. How to define instance attributes and get them accessed by external users.
The following program will also show that :-
 Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.
 Data declared in the protected section can be accessed by the class itself, and also by its subclasses but not by external users outside the class.
 Data declared in the private section can be accessed by the class only, but not by its subclasses and by external users outside the class.
Brief Description This program contains a class : parentclass with following attributes in different sections:-
Commondata in public section
Protectdata in private section
Privatedata in private section
The method showval in class : parentclass displays values of all the attributes.
This demonstrates that class can access all its attributes.
Class childclass is a subclass of class parentclass, which has a method : subval.
It displays the value for the data : commondata and protectdata .
Then, it changes the values for both and displays them again.
This demonstrates that subclass can access/change public/ protected attributes of superclass.
In the START-OF-SELECTION event, object : parent is instantiated from class : parentclass and object : child is instantiated from class : childclass.
Then , the method showval of parent(object of parentclass) and method subval of child(object of childclass) is called , which displays the values of different attributes.
Then, the public attribute of object parent is changed and the changed value is displayed.
This demonstrates that external users can change/display public attributes of a class.
Dump of the program:-
REPORT YSUBDEL LINE-SIZE 120.
CLASS parentclass DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value 'Accessible to all'.
METHODS : SHOWVAL.
PROTECTED SECTION.
DATA : protectdata(40) type c value 'Protected data'.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
CLASS parentclass IMPLEMENTATION.
METHOD : SHOWVAL.
write:/5 'All data from parentclass shown:-'.
write:/ sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA,
/5 PRIVATEDATA.
endmethod.
endclass.
CLASS childclass DEFINITION INHERITING FROM parentclass.
PUBLIC SECTION .
METHODS : subval.
ENDCLASS.
CLASS childclass IMPLEMENTATION.
method : subval.
skip 1.
write:/5 'Data of parent shown from child-'.
write:/5 sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA.
Commondata = 'Public data changed in subclass'.
Protectdata = 'Protected data changed in subclass'.
write:/5 sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA.
endmethod.
endclass.
I hope this will help u a lot
<b>Dont forget to reward points</b>
Thanks,
Sachin
05-14-2007 7:49 AM
Hi Ginni,
Hope these below links will be useful.
==========================
<b>http://www.sapgenie.com/abap/OO/syntax.htm
http://www.sapgenie.com/abap/OO/index.htm
http://www.sapgenie.com/abap/OO/defn.htm</b>
these links
http://help.sap.com/saphelp_47x200/helpdata/en/ce/b518b6513611d194a50000e8353423/content.htm
For function module to class
http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htm
for classes
http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm
for methods
http://help.sap.com/saphelp_47x200/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm
for inheritance
http://help.sap.com/saphelp_47x200/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm
for interfaces
http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm
Thanks,
AbyJacob(Bangalore)
05-14-2007 7:58 AM
Hi Ginni,
I found this topic interesting
=================
http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm
Further Reading
There are many books about object orientation, object-oriented programming languages, object-oriented analysis and design, project management for OO projects, patterns and frameworks, and so on. This is a small selection of good books covering the most important topics:
Scott Ambler, The Object Primer, SIGS Books & Multimedia (1996), ISBN: 1884842178
A very good introduction to object orientation for programmers. It provides comprehensive explanations of all essential OO concepts, and contains a procedure model for learning OO quickly and thoroughly. It is easy to read and practical, but still theoretically-founded.
Grady Booch, Object Solutions: Managing the Object-Oriented Project, Addison-Wesley Pub Co (1995), ISBN: 0805305947
A good book about all of the non-technical aspects of OO that are equally important for effective object-oriented programming. Easy to read and full of practical tips.
Martin Fowler, UML Distilled: Applying the Standard Object Modeling Language, Addison-Wesley Pub Co (1997), ISBN: 0201325632
An excellent book about UML (Unified Modeling Language - the new standardized OO language and notation for modeling). Assumes knowledge and experience of object orientation.
Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns. Elements of Reusable Object-Oriented Software, Addison-Wesley Pub Co (1998), ISBN: 0201634988
Provides a pattern, showing how recurring design problems can be solved using objects. This is the first big pattern book, containing many examples of good OO design.
James Rumbaugh, OMT Insights: Perspectives on Modeling from the Journal of Object-Oriented Programming, Prentice Hall (1996), ISBN: 0138469652
A collection of articles addressing the many questions and problems of OO analysis and design, implementation, dependency management, and so on. Highly recommended.
What is Object Orientation?
Object orientation (OO), or to be more precise, object-oriented programming, is a problem-solving method in which the software solution reflects objects in the real world.
A comprehensive introduction to object orientation as a whole would go far beyond the limits of this introduction to ABAP Objects. This documentation introduces a selection of terms that are used universally in object orientation and also occur in ABAP Objects. In subsequent sections, it goes on to discuss in more detail how these terms are used in ABAP Objects. The end of this section contains a list of further reading, with a selection of titles about object orientation.
Objects
An object is a section of source code that contains data and provides services. The data forms the attributes of the object. The services are known as methods (also known as operations or functions). Typically, methods operate on private data (the attributes, or state of the object), which is only visible to the methods of the object. Thus the attributes of an object cannot be changed directly by the user, but only by the methods of the object. This guarantees the internal consistency of the object.
Classes
Classes describe objects. From a technical point of view, objects are runtime instances of a class. In theory, you can create any number of objects based on a single class. Each instance (object) of a class has a unique identity and its own set of values for its attributes.
Object References
In a program, you identify and address objects using unique object references. Object references allow you to access the attributes and methods of an object.
In object-oriented programming, objects usually have the following properties:
Encapsulation
Objects restrict the visibility of their resources (attributes and methods) to other users. Every object has an interface, which determines how other objects can interact with it. The implementation of the object is encapsulated, that is, invisible outside the object itself.
Polymorphism
Identical (identically-named) methods behave differently in different classes. Object-oriented programming contains constructions called interfaces. They enable you to address methods with the same name in different objects. Although the form of address is always the same, the implementation of the method is specific to a particular class.
Inheritance
You can use an existing class to derive a new class. Derived classes inherit the data and methods of the superclass. However, they can overwrite existing methods, and also add new ones.
Uses of Object Orientation
Below are some of the advantages of object-oriented programming:
Complex software systems become easier to understand, since object-oriented structuring provides a closer representation of reality than other programming techniques.
In a well-designed object-oriented system, it should be possible to implement changes at class level, without having to make alterations at other points in the system. This reduces the overall amount of maintenance required.
Through polymorphism and inheritance, object-oriented programming allows you to reuse individual components.
In an object-oriented system, the amount of work involved in revising and maintaining the system is reduced, since many problems can be detected and corrected in the design phase.
Achieving these goals requires:
Object-oriented programming languages
Object-oriented programming techniques do not necessarily depend on object-oriented programming languages. However, the efficiency of object-oriented programming depends directly on how object-oriented language techniques are implemented in the system kernel.
Object-oriented tools
Object-oriented tools allow you to create object-oriented programs in object-oriented languages. They allow you to model and store development objects and the relationships between them.
Object-oriented modeling
The object-orientation modeling of a software system is the most important, most time-consuming, and most difficult requirement for attaining the above goals. Object-oriented design involves more than just object-oriented programming, and provides logical advantages that are independent of the actual implementation.
05-14-2007 8:08 AM
05-14-2007 8:15 AM
Methods in ABAP Objects - Example
The following example shows how to declare, implement, and use methods in ABAP Objects.
Overview
This example uses three classes called C_TEAM, C_BIKER, and C_BICYCLE. A user (a program) can create objects of the class C_TEAM. On a selection screen, the class C_TEAM asks for the number of members of each team.
Each object in the class C_TEAM can create as many instances of the class C_BIKER as there are members in the team. Each instance of the class C_BIKER creates an instances of the class C_BICYCLE.
Each instance of the class C_TEAM can communicate with the program user through an interactive list. The program user can choose individual team members for actions. The instances of the class C_BIKER allow the program user to choose the action on a further selection screen.
Constraints
The ABAP statements used for list processing are not yet fully available in ABAP Objects. However, to produce a simple test output, you can use the following statements:
WRITE [AT] /<offset>(<length>) <f>
ULINE
SKIP
NEW-LINE
Note: The behavior of formatting and interactive list functions in their current state are not guaranteed. Incompatible changes could occur in a future release.
Declarations
This example is implemented using local classes, since selection screens belong to an ABAP program, and cannot be defined or called in global classes. Below are the definitions of the two selection screens and three classes:
*******************************************************************
Global Selection Screens
*******************************************************************
SELECTION-SCREEN BEGIN OF: SCREEN 100 TITLE TIT1, LINE.
PARAMETERS MEMBERS TYPE I DEFAULT 10.
SELECTION-SCREEN END OF: LINE, SCREEN 100.
*----
SELECTION-SCREEN BEGIN OF SCREEN 200 TITLE TIT2.
PARAMETERS: DRIVE RADIOBUTTON GROUP ACTN,
STOP RADIOBUTTON GROUP ACTN,
GEARUP RADIOBUTTON GROUP ACTN,
GEARDOWN RADIOBUTTON GROUP ACTN.
SELECTION-SCREEN END OF SCREEN 200.
*******************************************************************
Class Definitions
*******************************************************************
CLASS: C_BIKER DEFINITION DEFERRED,
C_BICYCLE DEFINITION DEFERRED.
*----
CLASS C_TEAM DEFINITION.
PUBLIC SECTION.
TYPES: BIKER_REF TYPE REF TO C_BIKER,
BIKER_REF_TAB TYPE STANDARD TABLE OF BIKER_REF
WITH DEFAULT KEY,
BEGIN OF STATUS_LINE_TYPE,
FLAG(1) TYPE C,
TEXT1(5) TYPE C,
ID TYPE I,
TEXT2(7) TYPE C,
TEXT3(6) TYPE C,
GEAR TYPE I,
TEXT4(7) TYPE C,
SPEED TYPE I,
END OF STATUS_LINE_TYPE.
CLASS-METHODS: CLASS_CONSTRUCTOR.
METHODS: CONSTRUCTOR,
CREATE_TEAM,
SELECTION,
EXECUTION.
PRIVATE SECTION.
CLASS-DATA: TEAM_MEMBERS TYPE I,
COUNTER TYPE I.
DATA: ID TYPE I,
STATUS_LINE TYPE STATUS_LINE_TYPE,
STATUS_LIST TYPE SORTED TABLE OF STATUS_LINE_TYPE
WITH UNIQUE KEY ID,
BIKER_TAB TYPE BIKER_REF_TAB,
BIKER_SELECTION LIKE BIKER_TAB,
BIKER LIKE LINE OF BIKER_TAB.
METHODS: WRITE_LIST.
ENDCLASS.
*----
CLASS C_BIKER DEFINITION.
PUBLIC SECTION.
METHODS: CONSTRUCTOR IMPORTING TEAM_ID TYPE I MEMBERS TYPE I,
SELECT_ACTION,
STATUS_LINE EXPORTING LINE
TYPE C_TEAM=>STATUS_LINE_TYPE.
PRIVATE SECTION.
CLASS-DATA COUNTER TYPE I.
DATA: ID TYPE I,
BIKE TYPE REF TO C_BICYCLE,
GEAR_STATUS TYPE I VALUE 1,
SPEED_STATUS TYPE I VALUE 0.
METHODS BIKER_ACTION IMPORTING ACTION TYPE I.
ENDCLASS.
*----
CLASS C_BICYCLE DEFINITION.
PUBLIC SECTION.
METHODS: DRIVE EXPORTING VELOCITY TYPE I,
STOP EXPORTING VELOCITY TYPE I,
CHANGE_GEAR IMPORTING CHANGE TYPE I
RETURNING VALUE(GEAR) TYPE I
EXCEPTIONS GEAR_MIN GEAR_MAX.
PRIVATE SECTION.
DATA: SPEED TYPE I,
GEAR TYPE I VALUE 1.
CONSTANTS: MAX_GEAR TYPE I VALUE 18,
MIN_GEAR TYPE I VALUE 1.
ENDCLASS.
*******************************************************************
Note that none of the three classes has any public attributes. The states of the classes can only be changed by their methods. The class C_TEAM contains a static constructor CLASS_CONSTRUCTOR. C_TEAM and C_BIKER both contain instance constructors.
Implementations
The implementation parts of the classes contain the implementations of all of the methods declared in the corresponding declaration parts. The interfaces of the methods have already been defined in the declarations. In the implementations, the interface parameters behave like local data.
Methods of Class C_TEAM
The following methods are implemented in the section
CLASS C_TEAM IMPLEMENTATION.
...
ENDCLASS.
CLASS_CONSTRUCTOR
METHOD CLASS_CONSTRUCTOR.
TIT1 = 'Team members ?'.
CALL SELECTION-SCREEN 100 STARTING AT 5 3.
IF SY-SUBRC NE 0.
LEAVE PROGRAM.
ELSE.
TEAM_MEMBERS = MEMBERS.
ENDIF.
ENDMETHOD.
The static constructor is executed before the class C_TEAM is used for the first time in a program. It calls the selection screen 100 and sets the static attribute TEAM_MEMBERS to the value entered by the program user. This attribute has the same value for all instances of the class C_TEAM.
CONSTRUCTOR
METHOD CONSTRUCTOR.
COUNTER = COUNTER + 1.
ID = COUNTER.
ENDMETHOD.
The instance constructor is executed directly after each instance of the class C_TEAM is created. It is used to count the number of instance of C_TEAM in the static attribute COUNTER, and assigns the corresponding number to the instance attribute ID of each instance of the class.
CREATE_TEAM
METHOD CREATE_TEAM.
DO TEAM_MEMBERS TIMES.
CREATE OBJECT BIKER EXPORTING TEAM_ID = ID
MEMBERS = TEAM_MEMBERS.
APPEND BIKER TO BIKER_TAB.
CALL METHOD BIKER->STATUS_LINE IMPORTING LINE = STATUS_LINE.
APPEND STATUS_LINE TO STATUS_LIST.
ENDDO.
ENDMETHOD.
The public instance method CREATE_TEAM can be called by any user of the class containing a reference variable with a reference to an instance of the class. It is used to create instances of the class C_BIKER, using the private reference variable BIKER in the class C_TEAM. You must pass both input parameters for the instance constructor of class C_BIKER in the CREATE OBJECT statement. The references to the newly-created instances are inserted into the private internal table BIKER_TAB. After the method has been executed, each line of the internal table contains a reference to an instance of the class C_BIKER. These references are only visible within the class C_TEAM. External users cannot address the objects of class C_BIKER.
CREATE_TEAM also calls the method STATUS_LINE for each newly-created object, and uses the work area STATUS_LINE to append its output parameter LINE to the private internal table STATUS_LIST.
SELECTION
METHOD SELECTION.
CLEAR BIKER_SELECTION.
DO.
READ LINE SY-INDEX.
IF SY-SUBRC <> 0. EXIT. ENDIF.
IF SY-LISEL+0(1) = 'X'.
READ TABLE BIKER_TAB INTO BIKER INDEX SY-INDEX.
APPEND BIKER TO BIKER_SELECTION.
ENDIF.
ENDDO.
CALL METHOD WRITE_LIST.
ENDMETHOD.
The public instance method SELECTION can be called by any user of the class containing a reference variable with a reference to an instance of the class. It selects all of the lines in the current list in which the checkbox in the first column is selected. For these lines, the system copies the corresponding reference variables from the table BIKER_TAB into an additional private internal table BIKER_SELECTION. SELECTION then calls the private method WRITE_LIST, which displays the list.
EXECUTION
METHOD EXECUTION.
CHECK NOT BIKER_SELECTION IS INITIAL.
LOOP AT BIKER_SELECTION INTO BIKER.
CALL METHOD BIKER->SELECT_ACTION.
CALL METHOD BIKER->STATUS_LINE IMPORTING LINE = STATUS_LINE.
MODIFY TABLE STATUS_LIST FROM STATUS_LINE.
ENDLOOP.
CALL METHOD WRITE_LIST.
ENDMETHOD.
The public instance method EXECUTION can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method calls the two methods SELECT_ACTION and STATUS_LINE for each instance of the class C_BIKER for which there is a reference in the table BIKER_SELECTION. The line of the table STATUS_LIST with the same key as the component ID in the work area STATUS_LINE is overwritten and displayed by the private method WRITE_LIST.
WRITE_LIST
METHOD WRITE_LIST.
SET TITLEBAR 'TIT'.
SY-LSIND = 0.
SKIP TO LINE 1.
POSITION 1.
LOOP AT STATUS_LIST INTO STATUS_LINE.
WRITE: / STATUS_LINE-FLAG AS CHECKBOX,
STATUS_LINE-TEXT1,
STATUS_LINE-ID,
STATUS_LINE-TEXT2,
STATUS_LINE-TEXT3,
STATUS_LINE-GEAR,
STATUS_LINE-TEXT4,
STATUS_LINE-SPEED.
ENDLOOP.
ENDMETHOD.
The private instance method WRITE_LIST can only be called from the methods of the class C_TEAM. It is used to display the private internal table STATUS_LIST on the basic list (SY-LSIND = 0) of the program.
Methods of Class C_BIKER
The following methods are implemented in the section
CLASS C_BIKER IMPLEMENTATION.
...
ENDCLASS.
CONSTRUCTOR
METHOD CONSTRUCTOR.
COUNTER = COUNTER + 1.
ID = COUNTER - MEMBERS * ( TEAM_ID - 1).
CREATE OBJECT BIKE.
ENDMETHOD.
The instance constructor is executed directly after each instance of the class C_BIKER is created. It is used to count the number of instance of C_BIKER in the static attribute COUNTER, and assigns the corresponding number to the instance attribute ID of each instance of the class. The constructor has two input parameters - TEAM_ID and MEMBERS - which you must pass in the CREATE OBJECT statement when you create an instance of C_BIKER.
The instance constructor also creates an instance of the class C_BICYCLE for each new instance of the class C_BIKER. The reference in the private reference variable BIKE of each instance of C_BIKER points to a corresponding instance of the class C_BICYCLE. No external user can address these instances of the class C_BICYCLE.
SELECT_ACTION
METHOD SELECT_ACTION.
DATA ACTIVITY TYPE I.
TIT2 = 'Select action for BIKE'.
TIT2+24(3) = ID.
CALL SELECTION-SCREEN 200 STARTING AT 5 15.
CHECK NOT SY-SUBRC GT 0.
IF GEARUP = 'X' OR GEARDOWN = 'X'.
IF GEARUP = 'X'.
ACTIVITY = 1.
ELSEIF GEARDOWN = 'X'.
ACTIVITY = -1.
ENDIF.
ELSEIF DRIVE = 'X'.
ACTIVITY = 2.
ELSEIF STOP = 'X'.
ACTIVITY = 3.
ENDIF.
CALL METHOD BIKER_ACTION( ACTIVITY).
ENDMETHOD.
The public instance method SELECT_ACTION can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method calls the selection screen 200 and analyzes the user input. After this, it calls the private method BIKER_ACTION of the same class. The method call uses the shortened form to pass the actual parameter ACTIVITY to the formal parameter ACTION.
BIKER_ACTION
METHOD BIKER_ACTION.
CASE ACTION.
WHEN -1 OR 1.
CALL METHOD BIKE->CHANGE_GEAR
EXPORTING CHANGE = ACTION
RECEIVING GEAR = GEAR_STATUS
EXCEPTIONS GEAR_MAX = 1
GEAR_MIN = 2.
CASE SY-SUBRC.
WHEN 1.
MESSAGE I315(AT) WITH 'BIKE' ID
' is already at maximal gear!'.
WHEN 2.
MESSAGE I315(AT) WITH 'BIKE' ID
' is already at minimal gear!'.
ENDCASE.
WHEN 2.
CALL METHOD BIKE->DRIVE IMPORTING VELOCITY = SPEED_STATUS.
WHEN 3.
CALL METHOD BIKE->STOP IMPORTING VELOCITY = SPEED_STATUS.
ENDCASE.
ENDMETHOD.
The private instance method BIKER_ACTION can only be called from the methods of the class C_BIKER. The method calls other methods in the instance of the class C_BICYCLE to which the reference in the reference variable BIKE is pointing, depending on the value in the input parameter ACTION.
STATUS_LINE
METHOD STATUS_LINE.
LINE-FLAG = SPACE.
LINE-TEXT1 = 'Biker'.
LINE-ID = ID.
LINE-TEXT2 = 'Status:'.
LINE-TEXT3 = 'Gear = '.
LINE-GEAR = GEAR_STATUS.
LINE-TEXT4 = 'Speed = '.
LINE-SPEED = SPEED_STATUS.
ENDMETHOD.
The public instance method STATUS_LINE can be called by any user of the class containing a reference variable with a reference to an instance of the class. It fills the structured output parameter LINE with the current attribute values of the corresponding instance.
Methods of Class C_BICYCLE
The following methods are implemented in the section
CLASS C_BICYCLE IMPLEMENTATION.
...
ENDCLASS.
DRIVE
METHOD DRIVE.
SPEED = SPEED + GEAR * 10.
VELOCITY = SPEED.
ENDMETHOD.
The public instance method DRIVE can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method changes the value of the private attribute SPEED and passes it to the caller using the output parameter VELOCITY.
STOP
METHOD STOP.
SPEED = 0.
VELOCITY = SPEED.
ENDMETHOD.
The public instance method STOP can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method changes the value of the private attribute SPEED and passes it to the caller using the output parameter VELOCITY.
CHANGE_GEAR
METHOD CHANGE_GEAR.
GEAR = ME->GEAR.
GEAR = GEAR + CHANGE.
IF GEAR GT MAX_GEAR.
GEAR = MAX_GEAR.
RAISE GEAR_MAX.
ELSEIF GEAR LT MIN_GEAR.
GEAR = MIN_GEAR.
RAISE GEAR_MIN.
ENDIF.
ME->GEAR = GEAR.
ENDMETHOD.
The public instance method CHANGE_GEAR can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method changes the value of the private attribute GEAR. Since the formal parameter with the same name obscures the attribute in the method, the attribute has to be addressed using the self-reference ME->GEAR.
Using the Classes in a Program
The following program shows how the above classes can be used in a program. The declarations of the selection screens and local classes, and the implementations of the methods must also be a part of the program.
REPORT OO_METHODS_DEMO NO STANDARD PAGE HEADING.
*******************************************************************
Declarations and Implementations
*******************************************************************
...
*******************************************************************
Global Program Data
*******************************************************************
TYPES TEAM TYPE REF TO C_TEAM.
DATA: TEAM_BLUE TYPE TEAM,
TEAM_GREEN TYPE TEAM,
TEAM_RED TYPE TEAM.
DATA COLOR(5).
*******************************************************************
Program events
*******************************************************************
START-OF-SELECTION.
CREATE OBJECT: TEAM_BLUE,
TEAM_GREEN,
TEAM_RED.
CALL METHOD: TEAM_BLUE->CREATE_TEAM,
TEAM_GREEN->CREATE_TEAM,
TEAM_RED->CREATE_TEAM.
SET PF-STATUS 'TEAMLIST'.
WRITE ' Select a team! ' COLOR = 2.
*----
AT USER-COMMAND.
CASE SY-UCOMM.
WHEN 'TEAM_BLUE'.
COLOR = 'BLUE '.
FORMAT COLOR = 1 INTENSIFIED ON INVERSE ON.
CALL METHOD TEAM_BLUE->SELECTION.
WHEN 'TEAM_GREEN'.
COLOR = 'GREEN'.
FORMAT COLOR = 5 INTENSIFIED ON INVERSE ON.
CALL METHOD TEAM_GREEN->SELECTION.
WHEN 'TEAM_RED'.
COLOR = 'RED '.
FORMAT COLOR = 6 INTENSIFIED ON INVERSE ON.
CALL METHOD TEAM_RED->SELECTION.
WHEN 'EXECUTION'.
CASE COLOR.
WHEN 'BLUE '.
FORMAT COLOR = 1 INTENSIFIED ON INVERSE ON.
CALL METHOD TEAM_BLUE->SELECTION.
CALL METHOD TEAM_BLUE->EXECUTION.
WHEN 'GREEN'.
FORMAT COLOR = 5 INTENSIFIED ON INVERSE ON.
CALL METHOD TEAM_GREEN->SELECTION.
CALL METHOD TEAM_GREEN->EXECUTION.
WHEN 'RED '.
FORMAT COLOR = 6 INTENSIFIED ON INVERSE ON.
CALL METHOD TEAM_RED->SELECTION.
CALL METHOD TEAM_RED->EXECUTION.
ENDCASE.
ENDCASE.
*******************************************************************
The program contains three class reference variables that refer to the class C_TEAM. It creates three objects from the class, to which the references in the reference variables then point. In each object, it calls the method CREATE_TEAM. The method CLASS_CONSTRUCTOR of class C_TEAM is executed before the first of the objects is created. The status TEAMLIST for the basic list allows the user to choose one of four functions:
When the user chooses a function, the event AT USER-COMMAND is triggered and public methods are called in one of the three instances of C_TEAM, depending on the users choice. The user can change the state of an object by selecting the corresponding line in the status list.
05-14-2007 8:24 AM
Hi,
<u>/people/thomasalexander.ritter/blog/2007/04/19/how-to-encapsulate-an-internal-table-with-abap-objects
05-14-2007 10:38 AM
Hi Ginni,
Please refer the below program
BCALV_TREE_SIMPLE_DEMO
ABY
===
05-14-2007 10:41 AM
&----
*& Report BCALV_TREE_SIMPLE_DEMO *
*& *
&----
*& *
*& *
&----
report bcalv_tree_simple_demo.
class cl_gui_column_tree definition load.
class cl_gui_cfw definition load.
data tree1 type ref to cl_gui_alv_tree_simple.
include <icon>.
include bcalv_simple_event_receiver.
data: gt_sflight type sflight occurs 0, "Output-Table
gt_fieldcatalog type lvc_t_fcat, "Fieldcatalog
gt_sort type lvc_t_sort, "Sortiertabelle
ok_code like sy-ucomm. "OK-Code
start-of-selection.
end-of-selection.
call screen 100.
&----
*& Form BUILD_FIELDCATALOG
&----
text
----
form build_fieldcatalog.
get fieldcatalog
call function 'LVC_FIELDCATALOG_MERGE'
exporting
i_structure_name = 'SFLIGHT'
changing
ct_fieldcat = gt_fieldcatalog.
change fieldcatalog
data: ls_fieldcatalog type lvc_s_fcat.
loop at gt_fieldcatalog into ls_fieldcatalog.
case ls_fieldcatalog-fieldname.
when 'CARRID' or 'CONNID' or 'FLDATE'.
ls_fieldcatalog-no_out = 'X'.
ls_fieldcatalog-key = ''.
when 'PRICE' or 'SEATSOCC' or 'SEATSMAX' or 'PAYMENTSUM'.
ls_fieldcatalog-do_sum = 'X'.
endcase.
modify gt_fieldcatalog from ls_fieldcatalog.
endloop.
endform. " BUILD_FIELDCATALOG
&----
*& Form BUILD_OUTTAB
&----
text
----
--> p1 text
<-- p2 text
----
form build_outtab.
select * from sflight into table gt_sflight.
up to 1 rows.
endform. " BUILD_OUTTAB
&----
*& Form BUILD_SORT_TABLE
&----
text
----
--> p1 text
<-- p2 text
----
form build_sort_table.
data ls_sort_wa type lvc_s_sort.
create sort-table
ls_sort_wa-spos = 1.
ls_sort_wa-fieldname = 'CARRID'.
ls_sort_wa-up = 'X'.
ls_sort_wa-subtot = 'X'.
append ls_sort_wa to gt_sort.
ls_sort_wa-spos = 2.
ls_sort_wa-fieldname = 'CONNID'.
ls_sort_wa-up = 'X'.
ls_sort_wa-subtot = 'X'.
append ls_sort_wa to gt_sort.
ls_sort_wa-spos = 3.
ls_sort_wa-fieldname = 'FLDATE'.
ls_sort_wa-up = 'X'.
append ls_sort_wa to gt_sort.
endform. " BUILD_SORT_TABLE
&----
*& Module PBO OUTPUT
&----
text
----
module pbo output.
if tree1 is initial.
perform init_tree.
endif.
set pf-status 'MAIN100'.
endmodule. " PBO OUTPUT
&----
*& Module PAI INPUT
&----
text
----
module pai input.
case ok_code.
when 'EXIT' or 'BACK' or 'CANC'.
perform exit_program.
when others.
call method cl_gui_cfw=>dispatch.
endcase.
clear ok_code.
endmodule. " PAI INPUT
&----
*& Form exit_program
&----
free object and leave program
----
form exit_program.
call method tree1->free.
leave program.
endform. " exit_program
&----
*& Form register_events
&----
text
----
--> p1 text
<-- p2 text
----
form register_events.
define the events which will be passed to the backend
data: lt_events type cntl_simple_events,
l_event type cntl_simple_event.
define the events which will be passed to the backend
l_event-eventid = cl_gui_column_tree=>eventid_node_context_menu_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_item_context_menu_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_header_context_men_req.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_expand_no_children.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_header_click.
append l_event to lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_item_keypress.
append l_event to lt_events.
call method tree1->set_registered_events
exporting
events = lt_events
exceptions
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3.
set Handler
data: l_event_receiver type ref to lcl_tree_event_receiver.
create object l_event_receiver.
set handler l_event_receiver->on_add_hierarchy_node
for tree1.
endform. " register_events
&----
*& Form build_header
&----
build table for html_header
----
--> p1 text
<-- p2 text
----
form build_comment using
pt_list_commentary type slis_t_listheader
p_logo type sdydo_value.
data: ls_line type slis_listheader.
*
LIST HEADING LINE: TYPE H
clear ls_line.
ls_line-typ = 'H'.
LS_LINE-KEY: NOT USED FOR THIS TYPE
ls_line-info = 'ALV-tree-simple-Demo: flight-overview'. "#EC NOTEXT
append ls_line to pt_list_commentary.
STATUS LINE: TYPE S
clear ls_line.
ls_line-typ = 'S'.
ls_line-key = 'valid to'. "#EC NOTEXT
ls_line-info = 'January 29 1999'. "#EC NOTEXT
append ls_line to pt_list_commentary.
ls_line-key = 'time'.
ls_line-info = '2.00 pm'. "#EC NOTEXT
append ls_line to pt_list_commentary.
ACTION LINE: TYPE A
clear ls_line.
ls_line-typ = 'A'.
LS_LINE-KEY: NOT USED FOR THIS TYPE
ls_line-info = 'up-to-date data'. "#EC NOTEXT
append ls_line to pt_list_commentary.
p_logo = 'ENJOYSAP_LOGO'.
endform.
&----
*& Form init_tree
&----
text
----
--> p1 text
<-- p2 text
----
FORM init_tree.
perform build_fieldcatalog.
perform build_outtab.
perform build_sort_table.
create container for alv-tree
data: l_tree_container_name(30) type c,
l_custom_container type ref to cl_gui_custom_container.
l_tree_container_name = 'TREE1'.
create object l_custom_container
exporting
container_name = l_tree_container_name
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
create tree control
create object tree1
exporting
i_parent = l_custom_container
i_node_selection_mode =
cl_gui_column_tree=>node_sel_mode_multiple
i_item_selection = 'X'
i_no_html_header = ''
i_no_toolbar = ''
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
illegal_node_selection_mode = 5
failed = 6
illegal_column_name = 7.
create info-table for html-header
data: lt_list_commentary type slis_t_listheader,
l_logo type sdydo_value.
perform build_comment using
lt_list_commentary
l_logo.
repid for saving variants
data: ls_variant type disvariant.
ls_variant-report = sy-repid.
register events
perform register_events.
create hierarchy
call method tree1->set_table_for_first_display
exporting
it_list_commentary = lt_list_commentary
i_logo = l_logo
i_background_id = 'ALV_BACKGROUND'
i_save = 'A'
is_variant = ls_variant
changing
it_sort = gt_sort
it_outtab = gt_sflight
it_fieldcatalog = gt_fieldcatalog.
expand first level
call method tree1->expand_Tree
exporting
i_level = 1.
optimize column-width
call method tree1->column_optimize
exporting
i_start_column = tree1->c_hierarchy_column_name
i_end_column = tree1->c_hierarchy_column_name.
ENDFORM. " init_tree
=============================
----
INCLUDE BCALV_SIMPLE_EVENT_RECEIVER *
----
class lcl_tree_event_receiver definition.
public section.
methods: on_add_hierarchy_node
for event on_add_hierarchy_node of cl_gui_alv_tree_simple
importing grouplevel
index_outtab.
endclass.
class lcl_tree_event_receiver implementation.
method on_add_hierarchy_node.
data ls_outtab_line type sflight.
ls_outtab_line-planetype = 'Note'. "#EC NOTEXT
call method tree1->set_hierarchy_data
exporting
is_outtab_line = ls_outtab_line.
endmethod.
endclass.
05-14-2007 11:20 AM
Hi,
Please check out this Excellent BLOG from Ravikumar Allampallam
<b>ABAP OO in Action</b>
<u>/people/ravikumar.allampallam/blog/2005/02/11/abap-oo-in-action