OOPS ABAP-COMPONENTS OF CLASS



1. CLASS

A class is a template  because it gives the preview of want we obtain using that.
Class Creation is done in 2 steps.

CLASS CL_MYCLASS DEFINITION.

ENDCLASS.

CLASS CL_MYCLASS IMPLEMENTATION.

ENDCLASS.

2. OBJECT

1. An object is an instance /image of class.
2. Each Object has its own memory and attributes.
3. Objects are created with the command CREATE OBJECT.

SYNTAX

DATA OBJ TYPE REF TO  CL_MYCLASS.
CREATE OBJECT OBJ.
                                                  



                            COMPONENTS OF CLASS

        ATTRIBUTES

            Any constants, data variables  declared within a class are called as attributes of the class.

        METHODS

        Methods contain a block of code, which provides some functionality for a class.
        These are like function modules in SAP ABAP.
        It  can access all of the attributes of a class.
        Methods are defined in the definition part of a class and
Implemented in the implementation part.
        Methods are called using the CALL METHOD statement.

        EVENTS

          1. Events are like actions performed by the user.
          2. Each event must be linked to a method for writing  the corresponding logic for the         
              Action/event raised by the user.
  
        INTERFACES
        1. Interfaces are similar to classes which contain methods without any implementation.
        2. Interfaces area mainly used to extend the scope or functionality of the class.

                     INSTANCE AND STATIC COMPONENTS

        1. INSTANCE COMPONENTS

         1.  The components which exist separately for each instance (object) of the class are called as instance components, which mean for each object, instance components will be available separately.
        2. These are referred using the symbol ‘->’.

2. STATIC COMPONENTS

             1. The components which exist globally or fixed for all instances (objects) of the class are called as STATIC components, that means all objects with be referring to same/fixed memory.
        2. These are referred to using the symbol ‘ =>’ .

                                       VISIBILITY OF COMPONENTS

        PUBLIC SECTION

Anything declared under public section can be accessed by same class/outside of the class, i.e. (se38).

        PRIVATE SECTION

Data declared in the private section can be accessed by the class only, but not by its sub classes and by external users outside the class.

        PROTECTED SECT

Data declared in the protected section can be accessed by the class itself, and also by its sub classes and not by external users outside the class.

                    GLOBAL AND LOCAL CLASS

         Classes in ABAP Objects can be maintained either globally or locally.

GLOBAL CLASS

Global classes and interfaces are specified in the (Transaction SE24) in the ABAP Workbench.
        All of the ABAP programs in an R/3 System can access the global classes.

LOCAL CLASS

Local classes are specified in an ABAP program (TCODE SE38) and can only be used in the program in which they are defined.

EXAMPLE ON CLASS

STEPS TO CREATE CLASS

1. Go to t code se24.
2. Provide the Class Name: zcl_myclass.
3. Click on Create and Provide Short Description.
4. Click on save button.
        5.Go to attributes tab, define an attribute as below
        V_city instancepublictypechar25.
        6. Save and Activate
                                                    CREATING OBJECTS

        Creating instance or objects for the class

         #Creation of objects is of 2 steps

      1. Declare a variable for an object or instance.

                Syntax :-< object name> Type ref to <class name>.
                        Eg1:- OBJ1 type ref to ZCL_MYCLASS.
                        Eg2:- OBJ2 type ref to ZCL_MYCLASS.

        2. Next, instantiate/create copy for the object.
             Syntax: create object <object name>
                     Eg1:- create object obj1.
                     Eg2:- Create object obj2.










                             Accessing class components 

        Accessing or calling class components

        To call any class component with the instance, use the below Syntax.

        Syntax:    <Object name>  <class-component name>
              E.g.:- OBJ1 v_city
                       OBJ2v_city

        EXAMPLE PROGRAM

        DATA: OBJECT1 TYPE REF TO ZCL_MYCLASS,
                    CREATE OBJECT OBJECT1.

        *Setting a value using object1

        OBJECT1->V_CITY = ‘MUMBAI’.

        *Printing a value using object1

        Write:/ Object1->v_city.

       DATA: OBJECT2 TYPE REF TO ZCL_MYCLASS,
                  CREATE OBJECT OBJECT2.

         *Setting a value using object1
           OBJECT2->V_CITY = ‘BANGALORE’.

       *Printing a value using object2
       Write:/ Object2->v_city .

USING PUBLIC, PROTECTED, PRIVATE VARIABLES  

        Go TCODE se24.
        Provide Class Name as ZCL_MYCLASS.
        Give Short Description.
        Declare variables and attributes as below.




Write the logic as:

CREATE OBJECT OBJ1.

OBJ1->PUB_N AME = ‘KARTHIK’.    No error  because pub_name is a variable.
OBJ1->POR_N AME = ‘KARTHI’.      “Error because Por_name is a protected
OBJ1->PRI_N AME = ‘KART’.            “Error because  Pri_name is a private Variable