Вы находитесь на странице: 1из 12

What is Object Orientation?

In the past, information systems used to be defined primarily by their functionality: Data and functions were kept separate and linked together by means of input and output relations. The object-oriented approach, however, focuses on objects that represent abstract or concrete things of the real world. These objects are first defined by their character and their properties, which are represented by their internal structure and their attributes (data). The behavior of these objects is described by methods (functionality). Comparison between Procedural and Object Oriented Programming
Features Emphasis Procedure Oriented approach Emphasis on tasks. Object Oriented approach Emphasis on things that does those tasks. Programs are organized into classes and objects and the functionalities are embedded into methods of a class. Data can be hidden and cannot be accessed by external sources. New data and functions can be easily added whenever necessary.

Modularization

Programs are divided into smaller programs known as functions. Most of the functions share global data. Relatively more time consuming to modify for extending existing functionality.

Data security

Extensibility

Object Oriented Approach - key features 1. 2. 3. 4. 5. 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). They form a capsule which combines the character to the respective behavior. Objects should enable programmers to map a real problem and its proposed software solution on a one-to-one basis. 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. Better Programming Structure. Real world entity can be modeled very well. Stress on data security and access. Reduction in code redundancy. Data encapsulation and abstraction.

Local and Global Classes As mentioned earlier a class is an abstract description of an object. Classes in ABAP Objects can be declared either globally or locally. Global Class Global classes and interfaces are defined 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 Class Local classes are define in an ABAP program (Transaction SE38) and can only be used in the program in which they are defined. Global Class Accessed By Stored In Created By Namespace Any program In the Class Repository Created using transaction SE24 Must begin with Y or Z Local Class Only the program where it is defined. Only in the program where it is defined. Created using SE38 Can begin with any character

Local Classes Every class will have two sections. (1) Definition. (2) Implementation. Definition This section is used to declare the components of the classes such as attributes, methods, events .They are enclosed in the ABAP statements CLASS ... ENDCLASS. CLASS <class> DEFINITION. .... .... ENDCLASS. Implementation This section of a class contains the implementation of all methods of the class. The implementation part of a local class is a processing block.

CLASS <class> IMPLEMENTATION. ... ... ENDCLASS. Structure of a Class The following statements define the structure of a class:
1. 2. 3.

A class contains components. Each component is assigned to a visibility section. Classes implement methods.

1. Components of a Class are as follow: y y Attributes: Any data, constants, types declared within a class form the attribute of the class. Methods: Block of code, providing some functionality offered by the class. Can be compared to function modules. They can access all of the attributes of a class. Methods are defined in the definition part of a class and implement it in the implementation part using the following processing block: METHOD <meth>. ... ENDMETHOD. Methods are called using the CALL METHOD statement. y Events: class. A mechanism set within a class which can help a class to trigger methods of other

Interfaces: Interfaces are independent structures that you can implement in a class to extend the scope of that class.

Instance and Static Components: y Instance components exist separately in each instance (object) of the class and are referred using instance component selector using . Static components only exist once per class and are valid for all instances of the class. They are declared with the CLASS- keywords. Static components can be used without even creating an instance of the class and are referred to using static component selector =>.

2. Visibility of Components y 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. CLASS <class> DEFINITION. PUBLIC SECTION. ... PROTECTED SECTION. ... PRIVATE SECTION. ... ENDCLASS.
Example on Visibility of Components

The yellow block of code is CLASS Definition The Green block of code is CLASS Implementation The Grey block of code is for object creation. This object creation includes two steps: Step1: is Create a reference variable with reference to the class. Syntax: DATA : <object name> TYPE REF TO <class name>. Step 2: Create an object from the reference variable:Syntax: CREATE OBJECT <object name> . Output for the above code is

Attributes of Object Oriented Programming


y y y y Inheritance. Abstraction. Encapsulation. Polymorphism

Inheritance Inheritance is the concept of adopting the features from the parent and reusing them. It involves passing the behavior of a class to another class. You can use an existing class to derive a new class. Derived classes inherit the data and methods of the super class. However, they can overwrite existing methods, and also add new ones. Inheritance is of two types: 1) Single Inheritance 2) Multiple Inheritance

Single Inheritance Single Inheriting: Acquiring the properties from a single parent. (Children can be more).

Example for Single Inheritance Multiple inheritance Acquiring the properties from more than one parent. Example Tomato4 (Best Color, Size, Taste) Tomato1 (Best Color) Tomato2 (Best Size) Tomato3 (Best Tast)

Syntax: CLASS <subclass> DEFINITION INHERITING FROM <superclass>.

Let us see a very simple example for creating subclass(child) from a superclass(parent)

Multiple Inheritance is not supported by ABAP. Output is as follows:

Abstraction: Everything is visualized in terms of classes and objects. Encapsulation The wrapping up of data and methods into a single unit (called class) is known as Encapsulation. The data is not accessible to the outside world only those methods, which are wrapped in the class, can access it. Polymorphism: Methods of same name behave differently in different classes. Identical (identicallynamed) 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.

Sample Program for Local Class. TABLES: RBKP, RSEG. TYPES: BEGIN OF TY_RSEG, BELNR TYPE RE_BELNR, BUZEI TYPE RBLGP, MATNR TYPE MATNR, MENGE TYPE MENGE_D, END OF TY_RSEG. SELECT-OPTIONS: INVOICE FOR RBKP-BELNR. CLASS CL DEFINITION. PUBLIC SECTION. METHODS: GETDATA IMPORTING IRLOW TYPE RE_BELNR IRHIGH TYPE RE_BELNR, DISPLAYDATA. DATA: WA TYPE TY_RSEG, ITAB TYPE STANDARD TABLE OF TY_RSEG. ENDCLASS. CLASS CL IMPLEMENTATION. METHOD GETDATA. SELECT BELNR BUZEI MATNR MENGE FROM RSEG INTO TABLE ITAB WHERE BELNR BETWEEN IRLOW AND IRHIGH. ENDMETHOD. METHOD DISPLAYDATA. IF NOT ITAB[] IS INITIAL. SORT ITAB[] BY BELNR. LOOP AT ITAB[] INTO WA. WRITE:/ WA-BELNR,

WA-BUZEI, WA-MATNR, WA-MENGE. ENDLOOP. ENDIF. ENDMETHOD. ENDCLASS. DATA: OBJ TYPE REF TO CL. START-OF-SELECTION. CREATE OBJECT OBJ. CALL METHOD OBJ->GETDATA EXPORTING IRLOW = INVOICE-LOW IRHIGH = INVOICE-HIGH. CALL METHOD OBJ->DISPLAYDATA.

Reference: http://www.saptechnical.com/Tutorials/OOPS/Concepts/page1.htm http://wiki.sdn.sap.com/wiki/display/ABAP/Object+Oriented http://wiki.sdn.sap.com/wiki/display/ABAP/Object+Oriented+ABAP+%28OO-ABAP%29 http://wiki.sdn.sap.com/wiki/display/ABAP/Polymorphism+using+OO+ABAP

http://forums.sdn.sap.com/thread.jspa?threadID=728250 http://help.sap.com/saphelp_nw04/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

http://help.sap.com/saphelp_46c/helpdata/EN/87/56d00722c011d2954a0000e8353423/content.ht m#U--------------------------------------------SAP COMMANDS

Questions on OO ABAP 1. 2. 3. 4. 5. 6. Principles of oops? What is difference between procedural & OO Programming? What is class? What is object? Can we instantiate a class within implementation of other class? What is deferred key word?

7. How we can refer to a class without defining it? 8. Can we put non declarative statement e.g. START-OF-SELECTION within a class 9. What is static attribute & method? 10. How to create a global class? 11. How can we pass importing parameter? Pass by value/pass by reference 13. Can we changed pass by reference in any method? 14. What is preferred parameter? more than one optional & no mandatory 15. Can we pass returing parameter by reference? NO only pass by value 16. Can static method use instance attribute ? 17. Can a method call itself? 18. What is me variable? 19. What is constructor? What are types of constructor? When it is called? 20. Can we have export parameter in Instance constructor? 21. Can instance constructor raise exception? 22. When static constructor is called? 23. Can we have interface for static class or constructor? 24. What is abstract class? 25. Can we implement abstract method in abstract class? If not then where it can be implemented? 26. What is final class & Method? 27. Can subclass call super class constructor? 28. Can we call static constructor more than once in a program? 29. What is method redefinition? 30. What is interface? 31. Can we implement interface in private section of any class? 32. Is it mandatory to implement all the methods of interface? 33. What is alias? Instead of specifying full name of interface methods we can assign it a name which can directly trigger. 34. What is Friendship? 35. What is event handler method? 36. Can we have more than one event handler method for same event? 37. Can event have import parameter? 38. How you handled exception during programming? 39. What is cleanup section? 40. What is BADI? 41. What is check box for multiple usse in BADI? 42. How to search a BADI ? 43. What is Value table and Check table, Difference between them? 44. What are secondary indexs? 45. What is the draw back of secondary indexs? 46. What are conversion routines? 47. At which level are they mantained? 48. what are Predeifined data types? 49. Which predefined data type uses conversion routines? 50. What are logical units of work?

51. When is difference btw native and open sql 52. Difference between Modify and Update 53. Which is more efficient for all entries or joins? 54. When is implicit commit triggered. 55. What are RFC? 56. How do u create a destination system? 57. What are different types of commits used? 58. What are search helps? 59. Types of tables? 60. Difference between pool tables and cluster tables? 61. What is a delivery class? 62. What are the types of delivery class? 63. Difference between System tables and control tables? 64. What is normalization? 65. What is BCNF? 66. What is persistant class?

http://wiki.sdn.sap.com/wiki/display/Snippets/Binding+in+ABAP+OOP http://www.saptechnical.com/Tutorials/OOPS/Binding/Procedure.htm http://www.scribd.com/doc/35821441/Ooabap-Examples-------------***************

Difference between static and instance variables


Static attributes:
1. 2. 3. 4. The attributes are created only once. That means memory allocation for the attribute is only once. We can call next time the same attribute by using object then the same memory space is referred. We can change the value in the memory.

Instance attributes:
1. 2. These attributes are created for every time. That means for each and every call statement, the attribute creates a new memory location at every time.

Go to SE38 and create the following program.


*&---------------------------------------------------------------------* *& Report ZLOCALCLASS_VARIABLES * *& * *&---------------------------------------------------------------------*

*& By * *& Vikram.C for SAPTechnical.COM * *&---------------------------------------------------------------------* REPORT ZLOCALCLASS_VARIABLES. *General local class by using static and instance. *Define the class CLASS CL_LC DEFINITION. PUBLIC SECTION. DATA: A TYPE I VALUE 10. CLASS-DATA: B TYPE I VALUE 20. METHODS DISPLAY. CLASS-METHODS DISP. ENDCLASS. *Implement the class CLASS CL_LC IMPLEMENTATION. METHOD DISPLAY. WRITE:/ 'IT IS INSTANCE METHOD', / 'INSTANCE VARIABLE = ', A. ENDMETHOD. METHOD DISP. WRITE:/ 'IT IS STATIC METHOD' COLOR 2, / 'STATIC VARIABLE = ', B COLOR 2. ENDMETHOD. ENDCLASS. *Create the object DATA OBJ TYPE REF TO CL_LC. START-OF-SELECTION. CREATE OBJECT OBJ. *Call the instance method CALL METHOD OBJ->DISPLAY. *Call the static method. CALL METHOD CL_LC=>DISP.

Save it, check it, activate it after that execute it. Then the output is like this.