Ooabap Demo

42
07/09/15 07/09/15

description

oo abap...

Transcript of Ooabap Demo

  • *

  • * 2005 Intelligroup, Inc.Confidential and proprietaryUnstructured Programming.Procedural Programming.Object Oriented Programming.

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryConsists of only one main program. Data access done always globallyCharacteristicsDifficult to manage once the program becomes large. Same sequence of statements are repeated at multiple places.Disadvantagesreport 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.

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryA 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.write:/5 sal , itax , net_sal.ENDFORM.

    2005 Intelligroup, Inc.Confidential and proprietary

  • What's an Object and Class?

    Class A class is a set of objects that share a commonstructure and a common behavior.

    Object An object has state, behavior, and identity; thestructure and behavior of similar objects are definedin their common class; the terms instance and objectare interchangeable.

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Some Classes & Their Objects* 2005 Intelligroup, Inc.Confidential and proprietary

    Maruthi 800Rajas Maruthi,Prasads Maruthi,Ramanis MaruthiCustomerPepsi,bike,samsung,tvSalesOrderOR2643789, OR2643799 OR2643776, OR9999999Cricket TeamIndianTeam, Australian Team, SrilankanTeamYour Desktop PCIGA51097 etc.

    2005 Intelligroup, Inc.Confidential and proprietary

  • Sample Attributes & Methods* 2005 Intelligroup, Inc.Confidential and proprietary

    ClassAttributesMethodsCricketTeamCaptain,VC, WC,FB1,FB2,FB3SP1,SP2,SUBDoSingle,DoDoubleDoBowl,DoCatchDoRunout,HitSix,Doplay,HitBowndaryICICI S.A.Account Number,Balance,CreditLimitCheckBooksATM_Transfer,E_transfer, Withdraw, Check_Credit_limit, Issue_check_book, Track_transactionsProductionPO#,SSD,SED,ASD,AED,Comp,CoOptn,Oper,WorkcenterStart_Production,End_production,Start_opr,Send_to_WS Etc

    2005 Intelligroup, Inc.Confidential and proprietary

  • Basic Object-oriented PrinciplesAbstraction

    Encapsulation

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Abstraction Abstraction is used to manage complexity Abstraction is the process of hiding the details and exposing only the essential features of a particular object.

    Abstraction is used specifically in the Object Oriented Programming (OOP) sense hiding the details of the implementation of an object behind the interface composed of its methods.* 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Encapsulation Wrapping of data and methods into a single unit (called class) is known as encapsulation.

    The data is not acceseble to the outside world only those methods ,Which are wrapped in the class can only access it by using following visibility sections. Public, Protected and Private, are the basis for one of the important features of object orientation - encapsulation.

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Object Oriented Approach - key features1. Better Programming Structure2. Real world entity can be modeled very well3.Stress on data security and access4. Data encapsulation and abstraction5. Reduction in code redundancy

  • Classes Class describes an object

    components of the class describe the state and behavior of objects.

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Classes

    Global ClassLocal Class-> Defined in transaction se24->Accessed by any program

    ->Must begin with y or z->Stored in Class Repository-> Defined in trasaction se38->Accessed by only in the program where it is defined.->Begin with any character->* 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietary A Class basically contains the following:-Attributes:- Any data,constant ,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.

    Events:- A mechanism set within a class which can help a class to trigger methods of other class.

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

    2005 Intelligroup, Inc.Confidential and proprietary

  • ClassesDefining Local Classes:A complete class definition consists of a declaration part and, if required, an implementation part. The declaration part of a class CLASS DEFINITION. ... ENDCLASS. It contains the declaration for all components (attributes, methods, events) of the class. The declaration part belongs to the global program data.

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • ClassesIf you declare methods in the declaration part of a class, you must also write an implementation part for it. This consists of a further statement block: CLASS IMPLEMENTATION. ... ENDCLASSThe implementation part of a local class is a processing block. Subsequent coding that is not itself part of a processing block is therefore not accessible. * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryREPORT YSUBOOPS17 .CLASS c1 DEFINITION.PUBLIC SECTION. data : w_num type i value 5. methods : m1.ENDCLASS.CLASS c1 IMPLEMENTATION. METHOD M1. WRITE:/5 'I am M1 in C1'. ENDMETHOD.ENDCLASS.START-OF-SELECTION.DATA : oref1 TYPE REF TO c1 .CREATE OBJECT : oref1.write:/5 oref1->w_num.CALL METHOD : oref1->m1 .

    Defined in the global area of a local program :-CLASS DEFINITION...ENDCLASS.All the attributes , methods, events and interfaces are declared here.Cannot be declared inside a subroutine/function module.Class definition cannot be nested.

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryLocal class in a program is implemented as follows:- CLASS IMPLEMENTATION. .. ENDCLASS.Methods used by the class are described here.A class can be implementedAt the end of the program( like subroutines).After the class definition. REPORT YSUBOOPS17 .CLASS c1 DEFINITION.PUBLIC SECTION. data : w_num type i value 5. methods : m1.ENDCLASS.CLASS c1 IMPLEMENTATION. METHOD M1. WRITE:/5 'I am M1 in C1'. ENDMETHOD.ENDCLASS.START-OF-SELECTION.DATA : oref1 TYPE REF TO c1 .CREATE OBJECT : oref1.write:/5 oref1->w_num.CALL METHOD : oref1->m1 .

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryClass implemented at the end of the programClass implemented after Definition

    2005 Intelligroup, Inc.Confidential and proprietary

  • Constructors:Special Methods:CONSTRUCTOR: Cannot call with CALL METHOD statement.Called automatically when you create an objectCLASS_CONSTRUCTOR:Called when you first access the components of a class Events:Objects or classes can use events to trigger event handler methods in other objects or classes. When an event is triggered, any number of event handler methods can be called. the handler determines the events to which it wants to react. There does not have to be a handler method registered for every event. * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Classes : Visibility Sections You can divide the declaration part of a class into up to three visibility areas:CLASS DEFINITION. PUBLIC SECTION. ... PROTECTED SECTION. ... PRIVATE SECTION. ...ENDCLASS.They define the external interface of the class to its usersEncapsulation :The public components of global classes may not be changed once you have released the class. As well as defining the visibility of an attribute, you can also protect it from changes using the READ-ONLY addition. * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • .CLASS C1 DEFINITION.PUBLIC SECTION.DATA:METHODS:EVENTS:PROTECTED SECTION.DATA:METHODS:EVENTS:PRIVATE SECTION.DATA:METHODS:EVENTS:ENDCLASS.A Class puts its components under three distinct sections:-Public Section:- Components placed here form the external interface of the class they are visible to all users of the class as well as to methods within the class and to methods of subclasses*Protected Section:- Components placed in protected section are visible to the children of the class(subclass) as well as within the classPrivate Section:-Components placed here are accessible by the class itself.* 2005 Intelligroup, Inc.Confidential and proprietaryThere is no default visibility section in a class.This sequence of visibility must be maintained in a class

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryclass c2 definition inheriting from c1.public section .methods : m2.endclass.

    class c2 implementation.method m2.write:/5 From subclass' , w_num .endmethod. endclass.

    REPORT YSUBOOPS17 .CLASS c1 DEFINITION. PUBLIC SECTION. data : w_num type i value 5. methods m1.ENDCLASS.

    CLASS c1 IMPLEMENTATION.method m1. write:/5 From class : ' , w_num.endmethod. ENDCLASS.START-OF-SELECTION.DATA : oref1 TYPE REF TO c1 , oref2 type ref to c2 .CREATE OBJECT : oref1.write:/5 As an user ' ,oref1->w_num.Call method oref1->m1.Call method oref2->m2.

    ClassitselfSubclass of the classExternal user

    2005 Intelligroup, Inc.Confidential and proprietary

  • Self-ReferenceInternally, each method has an implicit self-reference variable, the reserved word meA method can access the components of its class simply by their name; however,It may use me simply for clarityIf a method declares a local variable with the same name as one of the class components, to avoid ambiguity it must use me to address the variable originally belonging to the class.A method must use me to export a reference to itself (that is, its object)* 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Inheritance Inheritance allows you to derive a new class from an existing class. CLASS DEFINITION INHERITING FROM . The new class inherits all of the components of the existing class . However, only the public and protected components of the super class are visible in the subclass. You can declare private components in a subclass that have the same names as private components of the super class. Each class works with its own private components.

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietarySubclasses can be created from its superclass using the syntax:-CLASS DEFINITION INHERITING FROM .Subclass inherits all the public and protected components of the superclass.Superclass should not be declared as a FINAL class.

    2005 Intelligroup, Inc.Confidential and proprietary

  • InheritanceYou can add new components to the subclass. This allows you to turn the subclass into a specialized version of the super class. A class can have more than one direct subclass, but it may only have one direct super class. This is called single inheritance. The root node of all inheritance trees in ABAP Objects is the predefined empty class OBJECT. * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • InheritanceRedefining Methods: you can use the REDEFINITION addition in the METHODS statement to redefine an inherited public or protected instance method in a subclass and make its function more specialized. The implementation of the redefinition in the subclass obscures the original implementation in the super class. Any reference that points to an object of the subclass uses the redefined method, even if the reference was defined with reference to the superclass. This particularly applies to the self-reference ME->. Within a redefined method, you can use the pseudo reference SUPER-> to access the obscured method. * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryFinal classes cannot have subclasses. Only the class can be instantiated. CLASS DEFINITION FINAL.A final method cannot be redefined in subclassesMETHODS .FINAL

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryTo redefine a public/protected method of a superclass in one of its subclasses, use the syntax in the subclass definition:- METHOD REDEFINITION

    The interface and visibility of a method cannot be changed while redefining it.

    The method declaration and implementation in the superclass is not affected when you redefine the method in a subclass.

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryOne cannot create an object from an abstract class. Only subclasses can be derived from them.CLASS DEFINITION ABSTRACT.Abstract methods cannot be implemented in the same class. Only the subclasses of that class can implement it. METHODS .ABSTRACTAny class containing an abstract method has to be an abstract class. All subsequent subclasses that do not implement the method must also be abstract. To implement an abstract method in a subclass, one need to redefine this subclass using the REDEFINITION addition.

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryStatic attributes only exist once in each inheritance tree. One can change them from outside the class using the class component selector with any class name, or within any class in which they are shared. They are visible in all classes in the inheritance tree. CLASS c1 DEFINITION. PUBLIC SECTION . CLASS-DATA : num TYPE I VALUE 5 .ENDCLASS. CLASS c1 IMPLEMENTATION. ENDCLASS.CLASS c2 DEFINITION INHERITING FROM c1.ENDCLASS.CLASS c2 IMPLEMENTATION.ENDCLASS. START-OF-SELECTION. c2=>num = 7. write:/5 c1=>num .Output : 7

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryWith inheritance, a reference variable defined with respect to a class may not only point to instances of that but also to instances of subclasses of the same. One can even create subclass objects using a reference variable typed with respect to a super class.Polymorphism through inheritance can be achieved by playing with static and dynamic type of a reference variable.Instances of a subclass may be used through the super class's interface. When this is done, a client can't access all components defined in the subclass, only those inherited from the respective super class. class c1 definition. . . . . . . .endclass.

    class c1 implementation.. . . . . .endclass.

    class c2 definition inheriting from c1. . . . . . .endclass.class c2 implementation. . . . . . . .endclass. start-of-selection. data : oref1 type ref to c1, oref11 type ref to c1, oref2 type ref to c2. create object oref1 type c2 . create object oref2. oref11 = oref2. write:/5 oref1->num , oref11->num .

    2005 Intelligroup, Inc.Confidential and proprietary

  • InheritanceInheritance Overview:

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • InheritanceInheritance and Reference Variables * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Interfaces Interfaces are independent structures that you can implement in a class to extend the scope of that class.a universal point of contact.

    They provide one of the pillars of polymorphism, since they allow a single method within an interface to behave differently in different classes. Global & Local InterfacesThe definition of a local interface is enclosed in the statements:INTERFACE ....ENDINTERFACE.The definition contains the declaration for all components (attributes, methods, events) of the interface. They automatically belong to the public section of the class in which the interface is implemented. * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Triggering and Handling Events To trigger an event, a class must Declare the event in its declaration part Trigger the event in one of its methodsEVENTS EXPORTING... VALUE() TYPE type [OPTIONAL].. To declare static events, use the following statement: CLASS-EVENTS ... Both statements have the same syntax. * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • * 2005 Intelligroup, Inc.Confidential and proprietaryClass-based exceptions are handled using TRYCATCHENDTRY block.TRY.< code to be checked for exception>CATCH cx1 .cxn [ into ref].< exception handling code>.ENDTRY.REPORT YSUBCLASS_EXCEPTION. DATA: i TYPE i VALUE 1.START-OF-SELECTION. TRY. i = i / 0. CATCH cx_sy_zerodivide. write:/5 'Divide by zero caught'. ENDTRY.

    2005 Intelligroup, Inc.Confidential and proprietary

  • 5 Reasons OO Programming is better than Procedural Programming

    Data EncapsulationInstantiationCode ReuseInterfacesEvents

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • ABAP Objects is more Explicit and Simpler to Use

    ABAP Objects is much simpler and less error-prone because : Classes contains attributes and methods. Objects are instances of Classes. Objects are addressed via references. Objects have clearly defined interfaces.* 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Comparison between Procedural ABAP and Object oriented ABAP Procedural ABAP

    Contains Obsolete Statements. Supports Overlapping and and some specialized objects. Shows Implicit Behavior. Appears difficult to learn. Object Oriented ABAP

    Prohibits obsolete statements and additions. Requires implicit syntax completions to be explicit. Detecting and preventing incorrect data handling.

    * 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

  • Improve your Procedural Programming using ABAP Objects

    Use methods as much as possible. Replace the use of Subroutines using static methods. Use function modules only when technically necessary. Disentangle procedural ABAP from ABAP Objects. Decouple screen programming from application programming. Never uncheck the Unicode checks active checkbox.* 2005 Intelligroup, Inc.Confidential and proprietary

    2005 Intelligroup, Inc.Confidential and proprietary

    **********