oops abap notes

Upload: vbvrk

Post on 02-Mar-2016

168 views

Category:

Documents


8 download

DESCRIPTION

Oops Abap Notes

TRANSCRIPT

  • 1 | P a g e S a n t o s h P

    VINAYAKA

    Features Of OOPS: -

    1) Encapsulation

    2) Data Abstraction

    3) Inheritance

    4) Polymorphism

    Application Of OOPS: -

    1) BAPIS

    2) BADIS

    3) Enhancement Frame Work

    4) Webdynpro

    5) HR-ABAP

    6) CRM-Technical

    7) SRM

    8) EP

    9) BSP

    Class: - A class is a user defined data type which is the collection of different type of components.

    A class only provides a template its doesnt allocate a memory.

    Object: - An instance of a class is called as an object.

    Whenever we instance a class memory will be allocated.

    Access Specifies (Visibility Section): -

    Types Of Class: -

    1) Local Class Local class to program (SE38-ABAP Editor).

    2) Global Class Class Builder Tool (SE24).

    Syntax for creating local classes: -

    1) Definition of class Declaration of components.

    2) Implementation of class Implementation of method.

  • 2 | P a g e S a n t o s h P

    Definition of class

    Class definition.

    Declaration of components.

    Endclass.

    Implementation class

    Class implementation.

    Implementation of methods.

    Endclass.

    Object Creation: -

    1) Create reference for the class

    Syntax: -

    Data type ref to

    Note: - Whenever an Object is created memory will be allocated for the attributes of the class and

    attribute gets initialized to default values.

    Access specifiers in ABAP: -

    1) Public Section

    2) Protected Section

    3) Private Section

    Components of ABAP classes: -

    Attributes Methods Events Interface aliases

    Types Constants Data Special Normal Instance Static

    Instance Instance

    Constructor

    Instance Static

    Static Static

    Constructor

  • 3 | P a g e S a n t o s h P

    1) OOPS Concepts 2) ALV Reporting Using Class 3) ALV Reporting Using Function Modules

    Procedure for creating global classes: -

    1) Define and implement the class in class builder tool (SE24). 2) Access the components of the global class in the repository objects (Executable Programs,

    Include Program, and Subroutine Pool). By instantiating the class.

    At any point of time they object store one set of values.

    Note: - Only public component can be accessed outside of the class.

    Interacting with methods in local classes: -

    1) Declare the method prototype in the class definition. Syntax: -

    Method/Class-Method [parameters].

    2) Implement the method in the class implementation. Syntax: -

    Method .

    Statements.

    Endmethod.

    3) Call the method. Syntax: -

    Call method [parameters]

    Instance method methods Static method class-methods It is recommend to use in the SAP attributes/data members/Instance variables protected/private. Methods/member function public.

    Note: - Whenever a report program contains class implementation and explicitly we need handle

    the event start-of-selection to indicate the starting point of program.

    Method with parameters: -

    Methods: m1 importing X type i,

    Y(20) type c.

  • 4 | P a g e S a n t o s h P

    Methods returning values: -

    Returning Keyword: -

    In case of other object oriented language a method can return exactly one value which done by using return keyword.

    In case of ABAP a method can return any number of values by declaring those many numbers of exporting (or) importing (or) changing parameters.

    To receive the method and return exactly one value we use returning parameters.

    If the method contains returning parameters it cannot contains exporting (or) changing parameters.

    A method can contains only one returning parameter.

    Returning parameter is always passed by value.

    Exception handling in methods: -

    An exception is runtime error which is raised during the program execution if the exception is not handling the program will be terminated.

    Exception handling is process of handling in runtime error and containing program execution.

    The exceptions are provided by SAP as part of standard exceptions class these exceptions are triggered by SAP itself as a developer we need to handle these exceptions by using try

    and catch block.

    Inside try block we need to declare this statement where the possible exception occurs.

    It the exception is raised in try blocked SAP creates the appropriate exception class object and the control is transfer to catch block.

    Inside the catch block we need to handle the exception by writing the appropriate exception handling statements.

    All the exception classes provided by SAP start with the naming standard CX_SY_...........

    Note: - As part of catch block declaration we need to specify the exception class which is

    reasonable for rising exception if the developer is not sure of the exception class we can use the

    exception class CX_ROOT.

    CX_ROOT is a super class for all the exception classes and it can handle any kind of exception.

  • 5 | P a g e S a n t o s h P

    Capturing system defined exception message: -

    Procedure for handling standing exception: -

    These exception are declared and raised SAP as a developer we need to handle try and catch.

    User defines exception: -

    These exceptions are declaring and handle are raised developers itself.

    Procedure for handling user-define exception in local classes: -

    1) Declare the user define exception as part of method declaration. Syntax: -

    Exception

    2) Raise the exception at appropriate place in the method implementation Raise

    3) Handle the exception while calling the method by checking sy-subrc status.

    Instance attribute: -

    These attribute are specific to an object and they are declared by using the keyword DATA in local classes.

    For each instance attribute separate memory will be allocated they can be an accessed only by using the object of the class.

    Static attribute: -

    They are not specific to any object and they are declared by using the keyword CLASS_DATA in local classes.

    For the static attribute memory will be allocated only when the first object is created the remaining objects points to be the same memory location they are also called as class

    variables.

    They can be accessed either by using the object are by using the class name.

    Instance methods: -

    In local classes they are declared by using the keyword METHODS they can be accessed

    only by using the object. They can access both instance and static attributes.

    Static methods: -

    In local class they are declaring the keyword CLASS-METHODS and they can be

    accessed either by using object are by using class name. They can access only static attributes.

  • 6 | P a g e S a n t o s h P

    Constructors: -

    A constructor is a special method used for initialized for attributes of class it special because

    it cannot be called explicitly it will be called implicitly.

    It is always declare in public section. It never returns any values.

    1) Instance 2) Static

    Instance Constructors: - It is declared by using keyword CONSTRUCTORS it is executed automatically whenever we create new instance of a class. It is specific to object.

    It can contain only importing parameters and exceptions.

    Static Constructors: - It is declared by using the keyword CLASS_CONSTRUCTORS it is executed automatically whenever a class is loaded a class will be loaded in cases.

    1) When we accesses the static components of the class using the class name before creating any objects.

    2) When we create the first object of the class. 3) It is not specific to any object it cannot contains any parameters and exception.

    Note: -

    Instance constructor is executed only once in the life time of object. Static constructor is executed only once in a life time of class.

    Note: - If a class contains both instance and static constructor and when we create the first object.

    1st the static constructor is executed and next instance constructor for rest of the objects only

    instance constructor get executed.

    NORMAL METHOD

    SPECIAL METHOD CONSTRUCTOR

    It can be declared in any of the sections. Only in public section.

    It has to be called explicitly. It called implicitly.

    A method can have any types of parameters. Instance constructor can have importing

    parameters and static constructor cannot have

    any parameters.

    Methods can return values. It cannot return values.

    It can be called any know of times in the

    lifetime of an object.

    Instance constructor will be called only once

    in the lifetime of every object where as static

    constructor will be called only once in the

    lifetime of class.

  • 7 | P a g e S a n t o s h P

    User defined exception: -

    Rising: - If a method is a capable of raising the exception that enable to handle the exception then

    we need to use the keyword RAISING as part of method declaration in this case the caller of the

    method as to take the responsibility of handling the exception.

    Friend classes: - By default outside the class of an object can access only public components of the

    class directly.

    By using friend classes to enable the object to access any components of the class directly irresponsibility of the visibility for this consider the following session.

    Consider two independent classes A and B.

    If class A considers class B as friend by inside class B methods we can instantiate class A and use the instance we can access all the component of class A directly irrespective of

    the visibility.

    In the above case the class B should be forward declared by using the keyword DEFERRED.

    Deferred keyword indicates to SAP that the class definition has been delayed and it has been declared same where else in the program.

  • 8 | P a g e S a n t o s h P

    Inheritance: -

    It is the process of acquiring the properties of other entity (class). The advantage of

    inheritance is reusability. They are three types of inheritance.

    1) Single 2) Multiple 3) Multilevel

    The class which gives the properties is called as super class, and the base class which takes the properties is called as subclass (or) derived class.

    Only public and protected components can be inherited.

    In local classes we need to use the keyword INHERITING FROM for achieving inheritance.

    1) Single inheritance: -

    A class derived from single super class.

    2) Multiple inheritance: -

    A class derived from more than one super class.

    Note: - In ABAP we cannot implement multiple inheritances directly we can implement indirectly

    through the concept of INTERFACE.

    3) Multilevel inheritance: -

    A class derived from another derived class.

    Single inheritance

    Multilevel inheritance Multiple inheritance

    CLASS A

    CLASS B

    CLASS A

    CLASS C

    CLASS A CLASS B

    CLASS C

    CLASS B

  • 9 | P a g e S a n t o s h P

    Polymorphism: -

    Poly Many Morph Forms

    Ism behavior

    Examples: - Method overloading

    Method overriding

    Method overloading: -

    If a class contains two methods with the same name but different signature it is called as

    method overloading.

    ABAP doesnt support method overloading.

    Method overriding: -

    If a sub class overwrites a super class method is called as method overriding.

    Whenever a sub class wants to override the super class method a sub class wants to declare the super class method in the subclass by using REDEFINITION keyword.

    While redefined the methods we cannot change the visibility of the method.

    Whenever a subclass overrides the super class method it is always recommended to call the super class method version in the subclass by using keyword. SUPER.

    Super keyword is used for referring to super class components from the sub class.

    To redefine a global method put cursor on the method click on redefine.

    Final keyword: -

    Final keyword can be used at two levels.

    1) Class level 2) Method level

    The class created as final cannot be inherited. A method created as final can be inherited but cannot redefine.

    Hierarchy of constructor execution: -

    When a super class contains static and instance constructor and if sub class contains only the static

    constructor in this case if we instantiate the sub class then the static constructor are executed from

    super class to sub class and then the instantiate constructor of the super class will be executed.

  • 10 | P a g e S a n t o s h P

    Note: - If the super class and sub class contains respective instance constructor it must for subclass

    instance constructor to call the super class instance constructor this is done by using SUPER

    keyword.

    This is the only place where the constructor can be are must be called explicitly.

    Note: - If the super class and sub class contains respective static and instance constructor and if

    instantiate the sub class first the static constructor are executed to super class to subclass and when

    the instantiate constructor will executed subclass to super class.

    Me keyword: - Me keyword refer to current object execution it is used to differentiate both

    attribute and method parameters whenever attribute and parameter names are same.

    Visibility of component level: -

    1) Public section 2) Protected section 3) Private section

    Visibility of class level: -

    1) Public 2) Protected 3) Private 4) Abstract

    Public: - The default visibility of a class is public.

    Public classes can be instantiated. Public classes can be inherited. The sub classes inherited the public class is also created as public by default.

    Protected classes: - Protected classes can be inherited but cannot be instantiated outside the class

    but it can be instantiated within the sub class method.

    The sub class inheriting the protected class is also created as protected by default to create the sub class as explicit public class we need to use extension create public as part of sub

    class definition.

    Private classes: -

    Private classes cannot be instantiated. Private classes can be inherited. The sub class inherited the private class is also created as private class by default. This sub class cannot be created as explicitly public class this can be mode possible if the

    super class considers the subclass as friend.

  • 11 | P a g e S a n t o s h P

    Abstract class: -

    It is a class which contains at least one abstract method. Abstract method is a method which is just declared but not implemented in local class they are declared by using the

    keyword abstract.

    If a class contains at least one abstract method then the entity class should be declared as abstract.

    Abstract methods are always declared in public (or) protected section. We cannot instantiate the abstract classes because they are not fully implemented. The class which ever inheritance the abstract class can implement the abstract method of

    the abstract class otherwise the subclass will declared as abstract.

    Abstract methods are also called as non-concerted methods. We declared method as abstract when we are not sure about the implementation but we

    are sure that the other classes as to use the same methods.

    Interfaces: - It is pure abstract class i.e. by default all methods of interface are abstract. By using

    interfaces we can implement multiple inheritances.

    By default the visibility of the interface components are public. Interface methods contain only declaration but not implementation the implementation

    must be provided in the corresponding class. The class whichever implements the

    interface is called as implementation class and this class should implement all the

    methods of the interface otherwise this class should be declaration as abstract.

    A local class whichever wants to implement the interface must declared the interface in the class definition by using interface keyword.

    Syntax: - interface .

    A class can implement any number of interfaces which is nothing but multiple inheritances

    whenever the interface component is referred the outside of the interface they must be prefixed with

    the name of the interface.

    Interface is always implemented in public section. We cannot the instantiate the interfaces because it is not implemented.

    Syntax for local interfaces: -

    Interface .

    Declaration of components

    End interface.

    Aliases: - aliases are the alternative names provided to the interface components i.e. whenever the

    interface components is referred outside the interface declaration it must be prefixed with the name

    of interface we can avoid the lengthy naming standard by declaring the aliases by the interface

    components these aliases must be declared in the definition of a class whichever the implementing

    the interface. By using aliases we can also change the visibility of the interface components.

  • 12 | P a g e S a n t o s h P

    ABSTRACT CLASSES

    INTERFACES

    Can contain both abstract and non-abstract

    methods.

    Can contain only abstract methods.

    Explicitly we need to use abstract keyword. By default all methods are abstract.

    Abstract methods can be declared in public or

    protected section.

    All components of interface by default are

    public.

    A class can inherit only one abstract class. A class can implement any know of interfaces.

    Abstract class components are directly referred

    in subclass.

    Interface components must be prefixed with the

    name of the interface.

    Persistence service: - It is used for storing the state of an object formality it is similarly to

    serialization java and .net. This service is implemented by using persistence classes. This service is

    implemented in two ways.

    1) By using business key identity. 2) By using GUID (global unique identifier)

    Storing the state of object permanently in the database is called as persistence. By default the lifetime of the scope of an object is within the program where it is created. Persistence class is always global and the naming standard is ZCL_ (or) YCL. Persistence class is always created as protected class. Whenever a persistence class is created SAP automatically create to class.

    1) Base agent class naming standard is ZCB_ (or) YCB_. 2) Agent class or actor class ZCA_ (or) YCA_.

    Base agent class is always created as abstract the class and it is the friend of persistence class.

    Actor class is always created as private class and it is a sub class of base agent class. Once the persistence class is created it needs to mapped with the corresponding database

    table.

    Persistence class using business key identity: -

    In this we consider the primary key fields of the database table as business key identity which is used for identity the object uniquely.

    In this case when the persistence class is mapped with the database tables SAP adds the fields of the database as the attributes of the persistence class.

    Also it creates the following methods as part of the base agent class.

  • 13 | P a g e S a n t o s h P

    1) Create_persistence. 2) Delete_persistence. 3) Get_persistence.

    The above three method are public instance methods which gets inherited to actor class. We need to use the above methods to interact with the persistence service.

    A part from this SAP also generates getter and setter methods as part of persistence class. Getter method is generated for all the fields of the database and setter methods are

    generated for non-primary key fields of the table.

    To access the above three methods we require the object of base agent class. But the base agent class is always created as abstract class and therefore cannot be instated.

    Since the above three method are inherited to actor class we need to instantiate the actor class and access these methods. But actor class is created as private class and therefore

    cannot be instated.

    We use the following mechanize to access these methods. Actor class is created as singleton class. As part of the actor class SAP as provided a public static attribute agent. We need to access this public static attribute agent using the actor class name. When

    accessed internal it execute the static constructor of actor class it is reasonable for

    creating the object. This object is return back using which we access the above three

    methods.

    Singleton class: -

    Creating a class in such way so that we can create exactly one object is called as

    singleton.

    Persistence service using GUID: - In this we need to consider database table which contains

    GUID as the first field. The data element of this field can be GUID/OS-GUID.

    The data type of this field raw data type.

    This field is used for unique identification of the object. The value for this field is generated dynamically by SAP.

    Note: - when a persistence class is mapped with the database table containing GUID also the field

    expects the GUID are added as attribute of class and also getter and setter method are generated for

    the entire field except GUID.

    Transact

  • 14 | P a g e S a n t o s h P

    ion service: - It is use for managing the object oriented transaction involving database operations.

    As part of this we need to use the following class and interfaces.

    1) CL_OS_SYSTEM CLASS 2) IF_OS_TRANSCATION_MANGAER INTERFACE 3) IT_OS_TRANSACTION INTERFACE

    Procedure for interacting with transaction service: -

    1) Start the object oriented transaction by calling the start method of the interface. IT_OS_TRANSACTION

    Start method is an instance method of interface IT_OS_TRANSACTION so we need to instantiate.

    The interface IF_OS_TRANSACTION which cannot be done directly. So we need to access the

    instance method Create_Transaction of the interface IF_OS_TRANSACTION_MANAGER to

    access this method we required the object of transaction manger interface. To get these object of

    transaction manger interface. To get this object we need to access the static method

    GET_TRANSCATION_MANAGER of the class CL_OS_SYSTEM.

    2) Perform the required operation: -

    End the transaction by calling the end method of the interface IF_OS_TRASACTION. When the transaction is successfully completed SAP issues commit work statement

    internally for saving the transaction permanently.

    If the transaction fails SAP raise the exception as part of this exception handling we need to cancel the transaction by calling the undo method of the interface

    IF_OS_TRANSCATION.

    Implementing persistence service using transaction service: -

    Note: -

    Transaction service is always implemented globally.

    Since transaction service is implemented in global classes we need to attach a T-code for a

    transaction class method so that everything will be executed as single process. If we access the

    transaction class method form local program it executed the different process which are assuming

    as two different translations.

    Casting: - It is the process of converting a variable from one data type to another data types they

    are two types.

    1) Wide casting 2) Narrow casting

    Wide casting: - It is the process of converting an object from a less detailed view to more detailed

    view.

    Narrow casting: - It is a process of converting an object from a more details view to be less

    detailed view.

  • 15 | P a g e S a n t o s h P

    Procedure for deleting the persistent object using GUID: -

    Check the existent persistent object using the method gets persistent OID. If the persistent object is

    available it returns the object of object class which needs to be type casted to the corresponding

    persistent class object. Pass the persistent class object as an input to the method.

    DELETE_PERSISTENT.

    Event handling in object oriented: - As part of ABAP objects SAP as provided many events as

    part of standard classes. These events are used in ALV reporting work flow customization CRM

    technical DSP and webdynpro programming.

    As part of custom classes we can declare user defined events. These events are declared are raised

    and handle by the developer itself.

    Procedure for interactive with user defined events in local class: -

    Declare the event in the definition of the class. Declare the event handler method in the definition of the class. Implemented the event handler method in the class implementation. Raise the event in one of method implementation. Register the handlers.

    Step1 Syntax: -

    Events/class-events [exporting parameters list].

    Step2 Syntax: -

    Methods/class-methods for event of [importing

    parameter list].

    Step3 Syntax: -

    Raise event [exporting parameter list].

    Step4 Syntax: -

    Set handler [for ].

    They are two types of events

    1) Instant 2) Static

    Instance events are declared by using keyword events. Static events are declared by using keyword class-events. Instance event is specific to an object. They are static event is not specific to an object. For every event there can be one are more event handler methods within the class are

    across the classes.

    These event handler methods are executed automatically whenever the event is raised are triggered.

  • 16 | P a g e S a n t o s h P

    For executing the event handler methods we need to register the handlers. By using this register handlers SAP will execute all the event handler methods. One after the other.

    Accordingly to sequence of register.

    Events can contains only exporting parameters which are imported by event handler method these parameters are always passed by values. The parameter name in the event

    as well as in event handler method must be same.

    Note: - if the handler is not register events can be triggered but no actions can be performed

    because the event handler methods will not be executed.

    For all instance: - while register the handler for register events as part of set handler statement we

    need to specify the object name after for keyword. This is reasonable for raising the event. This as

    to be done for every object separately which is raising the event instead of this we can use FOR ALL INSTANCE keyword.

    As part of handler such that the event handler method will executed irrespective of object used for raising the event.

    Static event: - While registering the handlers for static even we should not specify the object which

    is reasonable raising the static event. Because static event is not specific to on object i.e. for

    keyword is not allowed for the static event part of set handler this similarly for all instances in case

    of instance event.

    Static event handler method: -

    Instance event can be raised only in instance methods. Static event can be raised either in instance are static method.