Instantiation and Object-Oriented Language
Instantiation and Object-Oriented Language
ORIENTED LANGUAGE
Specifies status
data(attributes) and Two different objects can have
identical attribute values and
behavior(methods).
still not be identical.
LOCAL/GLOBAL
CLASSES
• LOCAL CLASSES
Local Classes can be defined within any ABAP program and are only visible
there.
• GLOBAL CLASSES
Global Classes are created with the Class Builder tool of the ABAP
workbench in the Class Library and are visible to any ABAP program.
• EVENTS
All methods of the same class can trigger these events. Other methods of the same or the different class
can be declared as special handler methods which react to these events.
ATTRIBUTES, TYPES AND
CONSTANTS
• CLASS <CLASSNAME> DEFINITION.
…
TYPES: <NORMAL TYPE DEFINITION>
CONSTANTS: CONSTANT TYPE <TYPE> VALUE <VALUE>.
DATA: VAR1 TYPE <TYPE>,
VAR2 TYPE <DDIC_TYPE>,
VAR3 TYPE VAR1,
VAR4 TYPE <TYPE> VALUE <VALUE>,
VAR5 TYPE <TYPE> READ ONLY,
VAR6 TYPE REF TO <CLASSNAME>,
VAR7 TYPE REF TO <INTERFACE>,
CLASS-DATA: ONLY_ONCE TYPE …
ENDCLASS
Attributes
Instance Attributes:
The contents of instance attributes define the instance
specific state of an object. You declare them using the DATA
statement.
Static Attributes:
The contents of static attributes define the state of the class
that is valid for all instances of the class.
Static attributes exist once for each class.
You declare them using the CLASS-DATA statement.
They are accessible for the entire runtime of the class.
All of the objects in a class can access its static attributes.
If you change a static attribute in an object, the change is
visible in all other objects in the class.
Methods
Instance Methods
You declare instance methods using the METHODS
statement.
They can access all of the attributes of a class, and can
trigger all of the events of the class.
Static Methods
You declare static methods using the CLASS-METHODS
statement.
They can only access static attributes and trigger static
events.
Special Methods
As well as normal methods, which you call using CALL
METHOD, there are two special methods called
CONSTRUCTOR and CLASS_CONSTRUCTOR, which
are automatically called when you create an object
(CONSTRUCTOR) or when you first access the components
of a class (CLASS_CONSTRUCTOR).
METHODS
• PARAMETER INTERFACE
The parameter interface of a method is defined by the additions to the METHODS
statement in the declaration part of the class.
The implementation part does not require any more details for the parameter interface.
the complete syntax for defining the parameter interface is:
METHODS meth
[ IMPORTING <im_var> TYPE <type>
EXPORTING <ex_var> TYPE <type>
CHANGING <ch_var> TYPE <type>
RETURNING VALUE <re_var> TYPE <type>
RAISING <class_exception> ].
start-of-selection.
create object obj_air_plane. “creating the object.
Generalization
LCL_SUP1 LCL_SUP2
NO MULTIPLE
INHERITANCE
Specialization
Inheritance
You can use an existing class to derive
a new class.
Derived classes inherit the data and
methods of the Super classes.
However, they can redefine existing
methods, and also add new ones.
RELATIONSHIP BETWEEN
SUPERCLASS AND SUBCLASS
• Common Components only exist once in the superclass.
New components in the superclass are automatically
available in subclasses.
Amount of new coding is reduced
PRIVATE SECTION.
DATA : name TYPE string,
planetype TYPE saplane-planetype. " Instance Attributes
METHOD display_n_o_airplanes.
WRITE: /,/ 'Total number of planes'(cal),
AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /.
ENDMETHOD. "display_n_o_airplanes
ENDCLASS. "c_airplane IMPLEMENTATION
Example (4 of 4)
• Creating class reference and accessing the class
attributes & methods
DATA: r_plane TYPE REF TO cl_airplane. " creating the class reference
START-OF-SELECTION.