Monday, June 9, 2008

Starting With ABAP Objects-I

Object Orientation:

It is a problem solving method where the software solution reflects objects in the real world.
Objects:

It is a section of source code which contains data and provides services.
-Data is the attribute
-Services are methods
The attributes can be changed only by the method of the class, hence thus proves the internal consistency.
Classes:

-It describes Objects
-Objects are runtime instances on classes
-we can have any number of objects for a class
-Each Object has unique identity and its own set of values
Object References:

-It helps to identify the address of the objects
-It allows to access attributes and methods of the objects.
Properties of Object Oriented Programming:

Encapsulation:

-Restricts the visibility of attributes and methods to other users.
-Every object has an interface which shows how the objects interact with each other.
-Implementation part is hided so that the process of interaction between the objects is
made invisible to others.
-It is often known as information hiding.
Polymorphism:
-Identical methods behave differently in different classes.
-With the help of an interface it enables to address methods with same name to
different objects.
-The signature or the definition of the method is always the same but the
implementation is different for different classes.
Inheritance:
-Deriving a new class from the existing class
-Can inherit the data and methods of the super class
-Can overwrite the existing methods and can also add new ones.

Simple class Example1:

CLASS LCL_SIMPLE DEFINITION.
PUBLIC SECTION.
DATA: ENAME (15) TYPE C,
EID (5) TYPE N. METHODS DISPLAY_DATA IMPORTING
IM_ENAME TYPE C
IM_NO TYPE N.
ENDCLASS.
CLASS LCL_SIMPLE IMPLEMENTATION.
METHOD DISPLAY_DATA.
ENAME = IM_ENAME.
EID = IM_NO.

WRITE: / ENAME,
EID.
ENDMETHOD. "display data
ENDCLASS. " lcl_simple IMPLEMENTATION

START-OF-SELECTION.

DATA EMP TYPE REF TO LCL_SIMPLE.
CREATE OBJECT EMP.

CALL METHOD EMP->DISPLAY_DATA
EXPORTING im_ename = 'priya'
im_no = '1060'.

Internal process of TYPE REF TO and CREATE OBJECT statements:

DATA EMP TYPE REF TO LCL_SIMPLE.

-When the statement is executed it creates a class reference variable EMP of type
LCL_SIMPLE.
-It can contain all instances of the class
-The class must be declared either globally or locally
-The contents of the variable EMP will be empty.
-It doesn’t point anything now.

EMP - ->

CREATE OBJECT EMP.

-Creates an instance of class.
-Now this reference variable EMP points the object.
-The instance on the class LCL_SIMPLE is LCL_SIMPLE<1> (Internal representation)


EMP - -> ------------> LCL_SIMPLE<1>

DATA: EMP TYPE REF TO LCL_SIMPLE,
EMP1 TYPE REF TO LCL_SIMPLE,
EMP2 TYPE REF TO LCL_SIMPLE,


EMP - ->
EMP1 - ->
EMP2 - ->

All these references will be initial.

CREATE OBJECT EMP, EMP1.

Move EMP to EMP2.

When this statement is executed both reference EMP, EMP2 points to the same object,
We can use either of these references.

EMP - -> ------------> LCL_SIMPLE<1> <------------ <--- EMP2

EMP1 - -> ------------> LCL_SIMPLE<2>