Object Orientation


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
Procedure Oriented approach
Object Oriented approach
Emphasis
Emphasis on tasks.
Emphasis on things that does those tasks.
Modularization
Programs are divided into smaller programs known as functions.
Programs are organized into classes and objects and the functionalities are embedded into methods of a class.
Data security
Most of the functions share global data.
Data can be hidden and cannot be accessed by external sources.
Extensibility
Relatively more time consuming to modify for extending existing functionality.
New data and functions can be easily added whenever necessary.
Object Oriented Approach - key features
1.     Better Programming Structure.
2.     Real world entity can be modeled very well.
3.     Stress on data security and access.
4.     Reduction in code redundancy.
5.     Data encapsulation and abstraction.
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.
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
Local Class
Accessed By
Any program
Only the program where it is defined.
Stored In
In the Class Repository
Only in the program where it is defined.
Created By
Created using transaction SE24
Created using SE38
Namespace
Must begin with Y or Z
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.     A class contains components.
2.     Each component is assigned to a visibility section.
3.     Classes implement methods.
1. Components of a Class are as follow:
·         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.
·         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.

            Instance and Static Components:
·         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
·         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.
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> .
Attributes of Object Oriented Programming
·         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
 Syntax: CLASS <subclass> DEFINITION INHERITING FROM <superclass>.

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 (identically-named) 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,
t           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.

Difference between static and instance variables
Static attributes:
1.     The attributes are created only once.
2.     That means memory allocation for the attribute is only once.
3.     We can call next time the same attribute by using object then the same memory space is referred.
4.     We can change the value in the memory.
Instance attributes:
1.     These attributes are created for every time.
2.     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  ZTESTCLASS_VARIABLES                                       *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  ZTESTCLASS_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.

No comments:

Post a Comment