Ads

Object Oriented Programming in SAP ABAP

Object Oriented Programming in SAP ABAP

As SAP ABAP Developer, you should have advanced skills in Object Oriented Programming which is used in mainly in Webdynpro ABAP and BADI also developed with object oriented Method .in the advanced versions of SAP System ,there is more requirements on OO Programming in ABAP.

The main ABAP objects components and functions are :

1) Classes
2) Object handling
3) Declaring and calling methods
4) Inheritance 
4) Interfaces
5) Triggering and handling events
6)Class and interface-fools

Now ,we will discuss all above functionalities one by one

What are ABAP Objects and Object oriented programming in sap


The object-oriented concept mainly depends on the objects that represent abstract  things of the real world. These objects are first defined by their attributes and their properties which are defined by their internal structure and their attributes . 
The behavior of these objects is described by methods (functionality).
The real world objects are vehicle,animal and planet.

Advantages of Object Orientation


Complex software systems become easier to understand, since object-oriented structuring provides a closer representation of reality than other programming techniques.

Class level implementation should be possible to implement changes without having to make alterations at other points in the system. This reduces the overall amount of maintenance required.

Through polymorphism and inheritance, object-oriented programming allows us to reuse individual components.

The amount of work involved in revising and maintaining the system is decreased becuase many problems can be detected and corrected in the design phase. 


Objects


Objects are instances of classes. They contain data and provides services. The data forms the attributes of the object. The services are known as methods .

Classes

Classes describe objects. From a technical point of view, objects are run time 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.

Object References

In a program, you can declare and call objects using unique object references. Object references allow you to access the attributes and methods of an object.

In object-oriented programming, objects  have the following properties:

Encapsulation

Objects restrict the visibility of their resources (attributes and methods) to outside users. Every object has an interface, which determines how other objects can interact with it. The implementation of the object is encapsulated, that is, invisible outside the object itself.

Inheritance

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.

Polymorphism

Identical (same name) methods behave differently in different classes. In ABAP Objects, polymorphism is implemented by redefining methods during inheritance and by using  interfaces.



Object Handling 


Data Types for References

... TYPE REF TO class
cref->comp

Creating Objects

CREATE OBJECT cref [TYPE class].

Addressing the Components of Objects

To access an attribute attr: ref->attr
·  To call a method meth: CALL METHOD ref->meth
  To access a static attribute attr: class=>attr
·  To call a static method meth: CALL METHOD class=>meth
  To access an attribute attr of your class: me->attr
·  To call a method meth of your class: CALL METHOD me->meth

Creating More Than One Instance of a Class
CREATE OBJECT

Assigning Object References

cref1 = cref2
iref1 = iref2.
iref1 = cref1.
cref1 = iref1.
MOVE ... ?TO ...
cref1 ?= iref1.

Object Lifetime
An object exists for as long as it is being used in the program.

Declaring Methods:

METHODS meth IMPORTING [VALUE(]i1  i2 ... [)] TYPE type [OPTIONAL]...
 EXPORTING [VALUE(]e1  e2 ... [)] TYPE type ...
 CHANGING  [VALUE(]c1  c2 ... [)] TYPE type [OPTIONAL]...
 RETURNING VALUE(r) 
 EXCEPTIONS exc1  exc2 ... .
and the appropriate additions.
To declare static methods, we use the following statement:

CLASS-METHODS meth...name

Implementing Methods

METHOD meth.
 ...
ENDMETHOD.


Static Method Call
To call a method, we use the following statement:

CALL METHOD meth EXPORTING  i1 = f1  i2 =f2  ... 
  IMPORTING  e1 = g1  e2 =g2  ...
 CHANGING   c1 = f1  c2 =f2  ...
 RECEIVING   r = h 
 EXCEPTIONS e1 = rc1 e2 =rc2  ...

directly using their name meth.
CALL METHOD meth...

Ou
tside the class, the visibility of the method depends on whether you can call it at all. Visible instance methods can be called from outside the class using
CALL METHOD ref->meth...
where ref is a reference variable whose value points to an instance of the class. Visible instance 

methods can be called from outside the class using
CALL METHOD =>meth...

Declaring Events
we declare events in the declaration part of a class or in an interface. To declare instance events, use the following statement:

EVENTS evt EXPORTING... VALUE(e1 e2 ...) TYPE type [OPTIONAL]..
To declare static events, use the following statement:
CLASS-EVENTS evt...

Triggering Events

RAISE EVENT evt EXPORTING e1 = f1  e2 = f2 ..

Handling Events
METHODS meth FOR EVENT evt OF cif IMPORTING e1 e2 ...

Registering Event Handler Methods

SET HANDLER h1 h2 ... [FOR]...
SET HANDLER h1 h2 ... FOR ref.
SET HANDLER h1 h2 ... FOR ALL INSTANCES.
SET HANDLER h1 h2 ...


Inheritance 

Inheritance is taking functionality of parent class into child class that is exactly redefining the super class methods in the child class.  

CLASS subclass DEFINITION INHERITING FROM super class.

Interfaces 

Interfaces are like classes but which contains empty methods which we implement in accessed classes.

INTERFACE intf.
...
ENDINTERFACE.

METHOD intf~imeth.
...

Comments

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced