ABAP OOPS : Introduction and Concepts of Object Oriented ABAP - Sap 4 All

Latest

Please enter your email and get the updates in your inbox.

Thursday 26 May 2016

ABAP OOPS : Introduction and Concepts of Object Oriented ABAP

What is Object Oriented ABAP and Concepts ?

  • The main feature of Object Oriented programming is representing real-time objects in the form of class objects.
  • Object Oriented ABAP focus on representing real-time objects of classes.
  • SAP ABAP Object Oriented programming is available in two flavors .
  • One is Global Classes and another one is local class.

Global Class

Global Class is an ABAP object which can be accessible via SAP Class Builder, T-code for SAP Class Builder is SE24.

Local Class

Local classes are classes which are available in ABAP programs, we can access them via ABAP editor SE38.
ABAP Classes and It`s components


What is a Class ? A class is a user defined data type with attributes, methods, events, user-defined types, interfaces etc for a particular entity or business application .
What are Objects ? Objects are nothing but instances of classes, each object has a unique identity that is memory and it`s own attributes.

Declaring Classes and Objects

Syntax: DATA <instance> TYPE REF TO <class name> . "Declaring global class in ABAP program using type ref to(type reference to )          
CREATE OBJECT <instance> . "Create object for the declared instance  

Components of a Class

Attributes: Attributes are variables, constants declared within a class .
Methods: Methods are coding blocks which provides some functionality .


  • These methods are similar to Function Modules in ABAP.
  • Methods can access all attributes of it`s own class.
  • Methods are defined in definition part and implemented in implementation part.
  • We can call methods using CALL METHOD statement.

Events: Event is a mechanism through which one method of a class can raise method of other class, without hazard of instantiating that class.
Interfaces: Interfaces are similar to classes which contain methods Without any implementation.
Interfaces are mainly used to extend the scope or functionality of the class.
Instance and Static Components




Instance components : These components exist separately in each instance (object) of the class and are referred using instance component selector using ->
Static components : These components exists globally for a class and are referred to using static component selector => .
Visibility of Components of Class
Each class component has a visibility.
In ABAP Objects, the whole class definition is separated into three visibility sections:
  • PUBLIC .
  • PROTECTED .
  • PRIVATE.
Public section: Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.
Protected section: 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.
Private Section: 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.
Global Class and Local Class
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.
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.


SAP Class Builder introduction :-

Transaction code for SAP class builder is SE24. SAP class builder(SE24) is used to create, display and change SAP classes.
Go to SE24 and provide some class example ex: CL_ABAP_CHAR_UTILITIES, to see what are the components of class.
SAP Classes
attributes of classes in SAP
Properties: The properties tab contains class properties like created user name, last changed user name, package etc.
Interfaces: Contains the list of the interfaces that are implemented in this class( will discuss in later lessons).
Friends: contains friend classes ( we will discuss later lessons).
Attributes: The attributes tab contains the list of attributes declared in that class.
Methods : Contains the methods with importing and exporting parameters and with some business functionality (source code).
Events: Contains events declared and implemented in that class.
Types: The types tab contains user defined type decelerations.
Aliases: Contains alias names for interface methods ( will discuss in later lessons).


Write First SAP class :-

Go to SE24( Class Builder).
Give the class name ZCL_MSAP and create.
creating a class in SAP
Provide short description, save.
Save it in a Local Object or in your test package.
Go to attributes tab and define an attribute as below.
CReate a class in SE24
IM_NAME-INSTANCE-PUBLIC-TYPE-CHAR25.
Save and Activate.
Now class is created, we have to use this in our program.
Create a ABAP program in SE38 and add below code

DATA : LR_CLASS TYPE REF TO ZCL_SAPN1 . "STEP1--WE DECLARE CLASSES USING REF TO BECAUSE THEY ARE OBJECTS  
CREATE OBJECT LR_CLASS. "STEP2--CREATE OBJECT FOR THE CLASS  

*CALL CLASS COMPONENT WITH THE INSTANCE   
LR_CLASS->IM_NAME = 'ATTRIBUTE NAME'. "USE CLASS COMPONENTS 
WRITE:/ LR_CLASS->IM_NAME.  *OUT PUT WILL BE 'ATTRIBUTE NAME'
Using multiple objects of class
DATA : LR_CLASS1 TYPE REF TO ZCL_SAPN1 . "Declare first class object  
DATA : LR_CLASS2 TYPE REF TO ZCL_SAPN1 . "Declare second class object    
CREATE OBJECT LR_CLASS1. "Create a first object  
CREATE OBJECT LR_CLASS2. "Create a second object  

*CALL CLASS COMPONENT WITH THE INSTANCE   
LR_CLASS1->IM_NAME = 'FIRST ATTRIBUTE NAME'. "Assign value to first object    
LR_CLASS2->IM_NAME = 'SECOND ATTRIBUTE NAME'. "Assign value to second object  
WRITE:/ LR_CLASS1->IM_NAME. "OUT PUT WILL BE 'FIRST ATTRIBUTE NAME'    
WRITE:/ LR_CLASS2->IM_NAME. "OUT PUT WILL BE 'SECOND ATTRIBUTE NAME'

6 comments:

  1. Hi

    Thank you for posting wonderful lesson. There is not even one OO ABAP complete tutorial on net. Please upload the lessons starting from basics to expert level. That way you could help people like me a lot.

    Thank you very much.

    ReplyDelete
  2. Very nice blog. Please post more blogs on this covering scenario's of expert level

    ReplyDelete
  3. First cover the basics and then move on to expert level

    ReplyDelete
  4. This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.

    SAP HR Training in Chennai

    SAP SD Training in Chennai

    SAP Basis Training in Chennai

    ReplyDelete
  5. Great Post. Well Explained!! Keep Up the good job!!
    SAP Certification Course in Pune

    ReplyDelete
  6. very good post, i actually love this web site, carry on it
    SAP PP online training
    SAP PP training

    ReplyDelete