Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBJECT ORIENTED PROGRAMMING IN ABAP. Overview New model for software construction Researches started in 1960’s SIMULA was the first programming language.

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING IN ABAP. Overview New model for software construction Researches started in 1960’s SIMULA was the first programming language."— Presentation transcript:

1 OBJECT ORIENTED PROGRAMMING IN ABAP

2 Overview New model for software construction Researches started in 1960’s SIMULA was the first programming language Became dominant programming methodology when it was incorporated with C++ in 1990’s Also introduced in ADA, BASIC and others.

3 Different approaches of Programming Unstructured Programming. Procedural Programming. Object Oriented Programming.

4 Unstructured Programming  Consists of only one main program.  The program stands for a sequence of commands which modify data that is global throughout the whole program. Characteristics  Difficult to manage once the program becomes large.  Same sequence of statements are repeated at multiple places, if they are needed at multiple locations. Disadvantages report ysubdel. DATA : sal type p decimals 2, itax type p decimals 2, net_sal type p decimals 2. sal = 12000. IF sal lt 5000. itax = 0. ELSE. itax = sal * '0.01'. ENDIF. net_sal = sal - itax. write:/5 sal, itax, net_sal. sal = 3500. IF sal lt 5000. itax = 0. ELSE. itax = sal * '0.01'. ENDIF. net_sal = sal - itax. write:/5 sal, itax, net_sal.

5 Procedural Programming  Programmer combines related sequences of statements into one single place, called procedure.  A procedure call is used to invoke the procedure.  After the sequence is processed, flow of control proceeds right after the position where the call was made. report ysubdel. DATA : sal type p decimals 2, itax type p decimals 2, net_sal type p decimals 2. sal = 12000. PERFORM sub_calc_tax USING sal itax net_sal. sal = 3500. PERFORM sub_calc_tax USING sal itax net_sal. FORM sub_calc_tax USING P_SAL P_ITAX P_NET_SAL. IF p_sal lt 5000. p_itax = 0. ELSE. p_itax = sal * '0.01'. ENDIF. p_net_sal = p_sal - p_itax. ENDFORM.

6 Object Oriented Programming Class defined and implemented Object created from the class and used  Classes and objects are used to model real world entity.  Methods inside the classes perform the functions.  Data used by the classes are protected between them.

7 What is OOP? A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure.

8 Concepts of OOP: Classes Objects Data Abstraction Encapsulation Inheritance Polymorphism

9 Classes A class is a collection of objects of similar type. Once a class is defined, any number of objects can be created which belong to that class.

10 Classes Contd.. A class is a blueprint or prototype that defines the variables (data) and methods (code) common to all objects of a certain kind. The class serves as a user defined type. A class thus defines a data type that behaves like the built-in types of a programming language. Access to the variables and methods declared in a class depend on whether they are declared private or public.

11 Objects Objects are the basic run-time entities in an object-oriented system. Objects are instances of classes. Each object is denoted by a name and has state. The variables within the object express everything about the object (state) and the methods specify how it can be used (behavior). In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects and can be viewed as an independent 'machine' with a distinct role or responsibility.

12 Lets take an example This object is a box What are the attributes of the box? Outside color is blue (public) inside color is red (private) Functions of the Box?? (methods) It can store things It can occupy space What is the status of the box ? (events) The box is semi open

13 Data Abstraction Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.

14 Encapsulation It is the ability that an object has to contain and restrict the access to its members. It is a key concept of object programming that ensures the autonomy and integrity of the objects

15 Inheritance Inheritance is the ability of an object to inherit the properties and methods of an other object. Child classes inherit some characteristics from their parent class. Parent Class Child Class

16 Inheritance Contd…  A single class can give birth to its subclasses by inheritance.  One parent class can have multiple subclasses, but one subclass can have only one parent class.  Subclasses have full access to all the public and protected components of parent class. They can modify to re-implement those components of their own.Also, subclasses can create new components in them.  Private components of parent classes are not known to subclasses. There can be components with same name in private section in both the parent and subclasses, each one working and being identified separately in their respective classes.

17 Polymorphism The ability of objects to redefine properties inherited from their parents. Identical (identically-named) methods behave differently in different classes. Polymorphism is implemented by redefining methods during inheritance.

18 Emphasis on data rather than procedure Programs are divided into Objects Data is hidden and cannot be accessed by external functions Objects can communicate with each other through functions New data and functions can be easily added whenever necessary Follows bottom-up approach Object Oriented Approach (key features)

19 Advantages of OOP OOP provides a clear modular structure for programs which makes it good for defining abstract data types where implementation details are hidden and the unit has a clearly defined interface. OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones. OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.

20 Procedure oriented approach Emphasis on tasks Large programs are divided into smaller programs known as functions Most of the functions share global data Data move openly around the system from function to function Object oriented approach Emphasis on things that does those tasks Programs are divided into objects Data structures are designed such that they characterized the objects Functions that operate on the data of an object are tied together in the data structure Data can be hidden and cannot be accessed by external functions New data and functions can be easily added whenever necessary Comparison between oop and procedure oriented approach

21 What are ABAP Objects ? New concept in R/3 Release 4.0. The term has two meanings.  It stands for the entire ABAP runtime environment.  It represents the object-oriented extension of the ABAP language.

22 Why do we need OOP for ABAP ??? To understand the recent concept of ABAP such as BAPI, BADI WORKFLOW etc To be at par with the others as this is better methodology For integration of ABAP with Microsoft Technologies and JAVA as these are build on OOP concept To exploit the class resources that has been provided by SAP

23 Types of Classes Local Classes Global Classes

24 Local and Global Classes Global ClassLocal Class Any programOnly the program where it is defined. In the Class RepositoryOnly in the program where it is defined. Created using transaction SE24 Created using SE38 Must begin with Y or ZCan begin with any character Accessed by Stored in Created by Namespace

25 A class declaration has two parts. * Definition * Implementation Example: CLASS test DEFINITION. ENDCLASS. CLASS test IMPLEMENTATION. {method implementation is done here} ENDCLASS. Declaring a Local Class

26 This declares and defines a local class “ test ”. In ABAP program this belongs to Global Section. Class definition cannot be nested. Classes cannot be defined inside subroutines or function modules.

27 Two Additions in Local Class Definition Addition 1 : CLASS class DEFINITION DEFERRED. Used to refer to a class at some point in a code and the class is not defined before the line. CLASS c1 DEFINITION DEFERRED. CLASS c2 DEFINITION. PUBLIC SECTION. DATA c1ref TYPE REF TO c1. ENDCLASS. CLASS c1 DEFINITION. PUBLIC SECTION. DATA c2ref TYPE REF TO c2. ENDCLASS. START-OF-SELECTION. data : obj1 type ref to C1. CREATE OBJECT obj1. create object obj1->o2. write:/5 obj1->o2->num.

28 Addition 2 : CLASS class DEFINITION LOAD The compiler normally loads the description of a global class from the class library the first time you use the class in your program. However, if the first access to a global class in a program is to its static components or in the definition of an event handler method, you must load it explicitly using the statement CLASS class DEFINITION LOAD. This variant has no corresponding ENDCLASS statement.

29 Objects are instances of a Class or in other words they are run time entities. We can create any number of objects for a class. Creating and accessing Objects. Data: obj type ref to test. Create object obj. WRITE : OBJ->ATTRIBUTE1.(i.e. object name ->attribute name) What is an object ?

30 Instance components: DATA for instance attributes METHODSMETHODS for instance methods EVENTSEVENTS for instance events Static components: CLASS-DATACLASS-DATA for static attributes CLASS-METHODSCLASS-METHODS for static methods CLASS-EVENTSCLASS-EVENTS for static events CONSTANTSCONSTANTS for constants (that is, constant static attributes) TYPESTYPES for internal types in classes The components of class

31 visibility section All components must belong to a visibility section. Components can be public, protected or private. The declaration part of the class is divided accordingly into up to three sections: PUBLIC SECTION PROTECTED SECTION PRIVATE SECTION **There is no default visibility section in a class.** **This sequence of visibility must be maintained in a class**

32 PUBLIC SECTION All components defined in this section are visible in the subsequent protected and private sections. These are also externally visible. PROTECTED SECTION All components in this section can access the components from the public section. In turn, they can be accessed by the components of the private section. These components are externally visible only to the inherited class. PRIVATE SECTION All components of this section can access the components of the public and private sections. They are not externally visible.

33 Example…. You are an employee (object of class:- employee). You have put your personal pens on the desk. These pens can be accesses by you(object of main class), our son and daughters( sub-classes) as well as by any other people(users outside the class). So, your pens are in your public section. You have a purse(attribute) in your pocket. Any other person (user outside the class) cannot use your purse(attribute) directly. But, your children(sub-classes of the class on which you are an object) can have full access to it.So, money in the purse is in your protected section. You have a special skill which you have developed by putting constant effort for a long time. You can only access it. Neither your children(sub- classes of your class), nor any user have any access to it directly. So, you can consider that skill to be in your private section.

34

35 Class Components Instance Components - Exist separately for each object in a class. Static Components - Exist only once for whole class

36 Attributes Attributes are internal data fields within a class that can have any ABAP data type. Instance attributes The contents of instance attributes define the instance-specific state of an object There exist one instance attribute for each instance of the class, thus they exist separately for each object. Static attributes Static attributes exist only once for each class. The data are the same for all instances of the class, and can be used e.g. for instance counters. PRIVATE SECTION. CLASS-DATA: counter type i.

37 Methods Methods are internal procedures in a class that define the behavior of an object. Instance Methods They can access all of the attributes of a class, and can trigger all of the events of the class. Static Methods They can only access static attributes and trigger static events. Special Methods As well as normal methods, which you call using CALL METHOD, there are two special methods called CONSTRUCTOR and CLASS_CONSTRUCTOR, which are automatically called when you create an object (CONSTRUCTOR) or when you first access the components of a class (CLASS_CONSTRUCTOR).

38 Constructor method Implicitly, each class has an instance constructor method with the reserved name CONSTRUCTOR and a static constructor method with the reserved name CLASS_CONSTRUCTOR. The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT statement, while the class_constructor is executed exactly once before you first access a class. The constructors are always present. However, to implement a constructor you must declare it explicitly with the METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no parameters.

39 Concept of Constructor What happens if we want to instantiate an object of sub class when: Case 1 Super class doesn’t have explicit constructor and Sub class doesn’t have explicit constructor. No explicit call required for the constructor of the super class Case 2 Super class has explicit constructor and Sub class doesn’t have explicit constructor. No explicit call required for the constructor of the super class

40 Case 3 Super class doesn’t have explicit constructor and Sub class has explicit constructor. Explicit call required for the constructor of the super class Case 4 Super class has explicit constructor and Sub class has explicit constructor. Explicit call required for the constructor of the super class

41 Addressing the Components of Objects Programs can only access the instance components of an object using references in reference variables. The syntax is as follows (where is a reference variable): To access an attribute : -> To call a method : CALL METHOD -> You can access static components using the class name as well as the reference variable. It is also possible to address the static components of a class before an object has been created. Addressing a static attribute : => Calling a static method : CALL METHOD => Within a class, you can use the self-reference ME to access the individual components: To access an attribute in the same class: ME-> To call a method in the same class: CALL METHOD ME->

42 Events Events denote the state of an object, that is what is the current status of an object. Instance Events An instance event can only be triggered in an instance method. Static Events Static events are the only type of event that can be triggered in a static method.

43 Some Definitions Public attributes Public attributes are defined in the PUBLIC section and can be viewed and changed from outside the class. There is direct access to public attributes. As a general rule, as few public attributes should be defined as possible. PUBLIC SECTION. DATA: Counter type i. Private attributes Private attributes are defined in the PRIVATE section and can only be viewed and changed from within the class. There is no direct access from outside the class. PRIVATE SECTION. DATA: name(25) TYPE c, planetype LIKE saplane-planetyp,

44 Public methods Can be called from outside the class PUBLIC SECTION. METHODS: set_attributes IMPORTING p_name(25) TYPE c, p_planetype LIKE saplane-planetyp, Private methods Can only be called from inside the class. They are placed in the PRIVATE section of the class.

45 METHODS Methods are of two types: 1. METHODS meth. 1. METHODS meth. Standard Methods. 2. METHODS meth FOR EVENT evt OF class 2. METHODS meth FOR EVENT evt OF class This type of methods are written to trap events.

46 METHODS meth. 1.... IMPORTING Method that has import parameters 2.... EXPORTING Method that has export parameters 3 …RETURNING VALUE (P) Method that returns values. 4 …CHANGING Method that changes values that is passed. 5.... EXCEPTIONS p1... Methods that raise exceptions. 6.... ABSTRACT This method cannot be instantiated 7. …FINAL This method cannot be inherited 8. …REDEFINITION. For the first Variant, the following additions are there

47 9. CONSTRUCTOR (A Special Method) Each class has one constructor. Constructors are special methods that produce a defined initial state of objects and classes. Constructor is automatically called when an object is created. Constructor is the same name of the class. It is a predefined, public instance method of the class, with the name CONSTRUCTOR. Constructors are necessary when you want to set the initial state of an object dynamically.

48 No return value. With in static method we can only access class attributes. Class-constructor does not have any parameters. Constructor has only import parameters. The state of an object is determined by its instance attributes and static attributes. You can assign contents to attributes using the VALUE addition in the DATA statement. Constructors are executed once for each instance. They are called automatically after you have created an instance with the CREATE OBJECT statement.

49 The events of a class can be triggered in the methods of the same class using the RAISE EVENT statement. You can declare a method of the same or a different class as an event handler method for the event of class using the addition FOR EVENT OF. EVENTS

50 Creating an event Events can be created like normal class component CLASS test DEFINITION. PUBLIC SECTION. EVENTS: test_event. ENDCLASS. Note: events don’t have implementation part

51 Raising an event An event can be raised in by using the ABAP statement “raise event”. Events can be raised anywhere It may be raised in any method of a class or may be raised any where in the main program CLASS testing_event DEFINITION. PUBLIC SECTION. EVENTS my_event. METHODS event_raising_method. ENDCLASS. CLASS testing_event IMPLEMENTATION. METHOD event_raising_method. RAISE EVENT my_event. ENDMETHOD. ENDCLASS

52 Abstract Class A class defined as ABSTRACT cannot be instantiated. Abstract class can only be accessed using its static components or its subclasses. Abstract class serves as a template for subclasses In a abstract class at least one method must be abstract or if a class contains any abstract method the whole class becomes abstract.

53 CLASS order DEFINITION ABSTRACT. PUBLIC SECTION. METHODS: select_order_header ABSTRACT EXCEPTIONS no_data_selected data_found, select_order_item ABSTRACT. PROTECTED SECTION. METHODS: write_hello. ENDCLASS. CLASS order IMPLEMENTATION. METHOD write_hello. WRITE : /10 'hello,this is a protected method call'. ENDMETHOD. ENDCLASS. Example:

54 Concept of Abstract class An abstract class can be inherited in the same way like a normal class. A method, which is Abstract, should be redefined in derived class. CLASS cl_super DEFINITION. PUBLIC SECTION. METHODS: demo1 abstract, demo2. ENDCLASS. CLASS cl_sub DEFINITION inheriting from cl_super. PUBLIC SECTION. METHODS demo1 REDEFINITION. ENDCLASS.

55 Concept of Final class  A final class can not be inherited further. CLASS final_class DEFINITION FINAL. “ this class cannot be inherited........ ENDCLASS. CLASS derived_class DEFINITION inheriting from final_class........... ENDCLASS.  A final method can not be redefined further. CLASS super_class DEFINITION......... methods final_method final. ENDCLASS. CLASS sub_class DEFINITION inheriting from super_class........... methods final_method redefinition. ENDCLASS.

56 The concept of inheritance is used to incorporate the properties of an existing class into a new class.The beauty of this feature is that the methods and the attribute of the existing class need not to be coded into the new class, as well as new features can be added into the new class. Fundamentals of inheritance In OOP, inheritance can be multiple as well as multi- level, but in ABAP only multi-level inheritance is possible. FATHERMOTHER CHILD GRAND FATHER FATHER CHILD

57 Member access. ·The public and protected components of the super class are visible in the subclass. ·Private components of the sub class can have same name as the private component of the super class. ·Public and Protected components can not have the same name as that have been used in super class. Super Class Sub Class Public Section: Super Class Private Section: Super Class Protected Section :Super Class Public Section: Super Class Protected Section: Super Class Note: The Private Section is not visible in the Sub Class.

58 Method Redefinition. A method can be re-implemented in the base class using redefinition of the method. The parameters remain same in the base class as it was in super class.

59 Example 1 CLASS gui_status DEFINITION. PUBLIC SECTION....... METHODS set_gui_status........ ENDCLASS. CLASS sub_class_one DEFINITION INHERITING FROM gui_status. PUBLIC SECTION...... METHODS set_gui_status REDIFINITION........ ENDCLASS. CLASS sub_class_two DEFINITION INHERITING FROM gui_status. PUBLIC SECTION...... METHODS set_gui_status REDIFINITION........ ENDCLASS.

60 CLASS super_class IMPLEMENTATION. METHOD set_gui_status method. SET pf-status ‘ZSUPER’ ENDMETHOD. ENDCLASS. CLASS sub_class_one IMPLEMENTATION. METHOD set_gui_status method. SET pf-status ‘ZSUB1’ ENDMETHOD. ENDCLASS. CLASS sub_class_two IMPLEMENTATION. METHOD set_gui_status method. SET pf-status ‘ZSUB2’ ENDMETHOD. ENDCLASS.

61 Example 2 CLASS super_class DEFINITION...... METHODS test_method. ENDCLASS. CLASS sub_class_one DEFINITION INHERITING FROM super_class.......... METHODS test_method REDEFINTION. ENDCLASS. CLASS sub_class_two DEFINITION INHERITING FROM super_class.......... METHODS test_method REDEFINTION. ENDCLASS. DATA: supr_obj type ref to super_class, sub1_obj type ref to sub_class_one, sub2_obj type ref to sub_class_two. START-OF-SELECTION. CREATE OBJECT sub1_obj. CREATE OBJECT sub2_obj. supr_obj = sub1_obj. CALL METHOD supr_obj->test_method. >> calls the sub_class_one supr_obj = sub2_obj. CALL METHOD supr_obj->test_method. >> calls the sub_class_two

62 Summary  Only Multi-level inheritance.  An abstract class can be inherited in the same way like a normal class but every abstract method should be redefined  Final class can not be inherited  Public and Protected sections are visible to the sub class  A method can be redefined in the sub class but not the Final method.  Super class constructor calling depends upon explicit definition of the Sub class constructor.

63 INTERFACES

64 Introduction to INTERFACE  Interface is similar to abstract class but it has many strong features.  Interface unlike class has only definitions part.  The interface can be implemented only in the class that uses it.  Interface, which is an independent structure, is used to implement in a class to extend the scope of a class  This allows users to address different classes via a universal point of contact. INTERFACE my_interface. Data : name(20). METHODS: I_method1, I_method2. ENDINTERFACE.

65 Classes and Interfaces  Class and interface are interrelated  An interface cannot be implemented without a class.  Interface do not have instances.  Interface can be implemented in a class using the statement INTERFACES in the public section of a class.  The component of the interface are added automatically in the public section of the class  The components of an interface can be identified as if they are components of the class, through the identifier

66 Example INTERFACE my_interface. METHODS my_interface_method exporting num type i. ENDINTERFACE. CLASS interface_class DEFINITION. INTERFACES my_interface. ENDCLASS. CLASS interface_class IMPLEMENTATION. METHOD my_interface~my_interface_method. Write:/ num. ENDMETHOD. ENDCLASS.

67 Note 1. The class must implement the methods of all interfaces implemented in it. The implementation part of the class must contain a method implementation for each interface method : METHOD.... ENDMETHOD. 2.Interfaces can be implemented by different classes. Each of these classes is extended by the same set of components. However, the methods of the interface can be implemented differently in each class.

68 INTERFACE common_interface. METHODS: interface_method. ENDINTERFACE. CLASS interface_class1 DEFINITION. PUBLIC SECTION. INTERFACES common_interface. ENDCLASS. CLASS interface_class1 IMPLEMENTATION. METHOD common_interface~interface_method. “interface method is implemented as type 1 WRITE:/ ‘ METHODS IMPLEMENTATION TYPE 1 ENDMETHOD. ENDCLASS. CLASS interface_class2 DEFINITION. PUBLIC SECTION. INTERFACES common_interface. ENDCLASS. CLASS interface_class2 IMPLEMENTATION. METHOD common_interface~interface_method. “Interface method is implemented astype2 WRITE:/ ‘ METHODS IMPLEMENTATION TYPE 2’. ENDMETHOD. ENDCLASS Example

69 Interface reference and component access Using the class reference variable : To access an attribute : -> To call a method : CALL METHOD -><intf~meth Using the interface reference variable : To access an attribute : -> To call a method : CALL METHOD ->

70 Reference variable A reference variable has two characteristics 1. Static -- The static characteristic is the class or interface used in reference variable definition. 2. Dynamic -- The dynamic type is the object to which the reference variable is currently pointing. DATA: Ref1 type ref to father, (static) Ref2 type ref to child. (static) Ref1 = ref2. (dynamic)

71 Reference variable assigning rule. When the static and the dynamic type of a reference variable are different, the principal rule is that the static type is always more general than the dynamic type. For example if the static type is an interface the dynamic type can be a class implementing the interface.

72 INTERFACE my_infc. METHODS: I_method. ENDINTERFACE. CLASS c1 DEFINITION. PUBLIC SECTION. INTERFACES: my_infc. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD my_infc~I_method. Write:/ ‘method for c1’. ENDMETHOD. ENDCLASS.

73 CLASS father DEFINITION. PUBLIC SECTION. METHODS: f_meth. ENDCLASS. CLASS father IMPLEMENTATION. METHOD f_meth. Write: / ‘father method’. ENDMETHOD. ENDCLASS. CLASS child DEFINITION INHERITING FROM FATHER. PUBLIC SECTION. METHODS : f_meth REDEFINITION, C_meth. ENDCLASS. CLASS child IMPLEMENTATION. METHOD f_meth. Write: ‘father method redefined’. ENDMETHOD. METHOD c_meth. Write: ‘child method’. ENDMETHOD. ENDCLASS.

74 START-OF-SELECTION. DATA: F_ref TYPE REF TO father, Ref TYPE REF TO father, C_ref TYPE REF TO child. CREATE OBJECT: C_ref, f_ref. Ref = f_ref. Call method ref->f_meth. Ref = C_ref. Call method ref->f_meth. ** Call method ref->c_meth. “ This assignment is not possible. *************** Output *********************** Father method Father method redefined.

75


Download ppt "OBJECT ORIENTED PROGRAMMING IN ABAP. Overview New model for software construction Researches started in 1960’s SIMULA was the first programming language."

Similar presentations


Ads by Google