Object Oriented ABAP (OOABAP) Instance, Static Constructors Tutorial with Examples. Welcome to SAP ABAP Interview Questions.
OOABAP Constructors Tutorials with Examples
Here sapabapiq.com presenting article on how to work with instance and static constructors tutorial with examples. If you are an novice OOABAP programmer this post will helps you to learn the constructor concepts in object oriented abap programming. Refer this post for instance and static constructors with tutorials.
Before going to read this article we suggest to read previous article in the series on OOABAP tutorials with example.
How to work with Constructors: Instance & Static Constructors
Instance Constructors get fired at the time of class instantiation |
|
Theme
|
This simple program will show you that instance constructor methods of a class get
triggered when an object is created from the class.
|
Program Description
|
This program contains a class C1 with a constructor
method which writes out something to indicate that it is triggered.
In the
START-OF-SELECTION block, the class C1 is instantiated, which triggers
the instance constructor method( as is evident by the output as report).
This establishes the theme.
|
Dump
|
REPORT YSUBOOPS1.
CLASS
C1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
WRITE:/5 'I am constructor'.
skip
2.
ENDMETHOD.
ENDCLASS.
***************
main program **************
START-OF-SELECTION.
DATA:
obj1 TYPE REF TO c1.
CREATE
OBJECT: obj1.
|
Output
|
I am constructor
|
Instance Constructors can have import parameters |
|
Theme
|
Instance constructors can have import parameters. Values
to them are passed at the time of CREATE OBJECT statement to create object
from the class containing the constructor.
|
Program Description
|
The program contains a class C1 which has one instance constructor with one
import parameter.
The constructor gets
fired at the time of CREATE OBJECT statement.
|
Dump
|
REPORT YSUBOOPS2.
CLASS
c1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR importing today type
d.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
Write:/5 'Today is : ' , today
dd/mm/yyyy.
ENDMETHOD.
ENDCLASS.
***************
main program **************
START-OF-SELECTION.
DATA:
obj1 TYPE REF TO c1.
CREATE
OBJECT: obj1 exporting today = sy-datum.
|
Output
|
Today is 08.04.2004
|
Constructors cannot have any export parameters |
|
Theme
|
This program will demonstrate that constructor methods
cannot have any export parameters.
|
Program Description
|
This program attempts to create a constructor with export
parameter, which is trapped and resisted at the time of compilation.
This establishes the theme.
|
Dump
|
REPORT YSUBOOPS2.
CLASS
c1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR exporting name
type c.
ENDCLASS.
|
Output
|
Compilation errror
is reported.
|
Instance Constructors can raise exceptions |
|
Theme
|
Instance
Constructor methods can raise exceptions
|
Program Descriptions
|
This program contains a class C1 which contains an
instance constructor method. It accepts an import parameter, NUM.
If
it is lesser than 7, an exception is raised, which is properly handled by
specifying some sy-subrc value at the time of CREATE OBJECT statement and
later handled properly.
|
Dump
|
REPORT YSUBOOPS2.
CLASS
c1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR importing num type i
exceptions e1 .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
if num lt 7.
raise e1.
endif.
ENDMETHOD.
ENDCLASS.
***************
main program **************
START-OF-SELECTION.
DATA:
obj1 TYPE REF TO c1.
CREATE
OBJECT: obj1 exporting num = 5
exceptions e1 = 2.
if sy-subrc = 2.
write:/5 'Exceptions raised'.
endif.
|
Output
|
Exceptions Raised
|
Use of static constructor. |
|
Theme
|
There are two programs over here which will show you
simple example on a static constructor. You will learn how to declare a
static constructor. Static/class constructors get triggered before any of the
following events:-
v Generating
an instance of a class using CREATE
OBJECT obj, where obj has the data type REF TO class.
v Calling
a static method using [CALL
METHOD] class=>meth.
v Registering
a static event handler method using SET HANDLER
class=>meth for obj.
v Registering
an event handler method for a static
event of the class class.
Addressing
a static attribute with
class=>a.
These two programs will show you that a class constructor
gets fired before any of its static components are accessed, or an object is
created from the class.
|
Dump
|
REPORT YSUBOOPS2.
CLASS c1 DEFINITION .
PUBLIC SECTION.
CLASS-DATA : NUM TYPE I VALUE 5.
CLASS-METHODS :CLASS_CONSTRUCTOR.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE:/5 'I am class constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF TYPE REF TO C1.
CREATE OBJECT OREF.
REPORT YSUBOOPS2.
CLASS c1 DEFINITION .
PUBLIC SECTION.
CLASS-DATA : NUM TYPE I VALUE 5.
CLASS-METHODS :CLASS_CONSTRUCTOR.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE:/5 'I am class constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
WRITE:/5 C1=>NUM.
Constructor
is triggered when a static Constructor is fired when an object is attribute
is accessed created
from the class
|
Output
|
For the first program(on the LHS):-
I am class constructor
5
For the second program(on the RHS):-
I am class constructor
|
Static constructor can be triggered at the beginning of a processing block(form /event/block/procedure) |
|
Theme
|
In the START-OF-SELECTION block of this program, static
attribute of a class containing class constructor will be accessed.
This will
demonstrate that the the first thing which will get executed in the
START-OF-SELECTION block is the class constructor method, irrespective of the
point at which the static attribute is accessed.
|
Program Description
|
The program contains a class C1 with a static constructor,
which prints the statement:- “ I am class constructor”.
This class also contains a static attribute , num of value
= 5.
The START-OF-SELECTION block in this program contains the
following statements:-
A write statement which will print :-“Hello”
A call to access the static attribute(NUM) of class C1.
On execution of this program, we will get the following
output in the list:-
I am class constructor
Hello
5
instead of the expected output:-
Hello
I am class constructor
5
This
demonstrates the theme.
|
Dump
|
REPORT YSUBOOPS2.
CLASS c1 DEFINITION .
PUBLIC SECTION.
CLASS-DATA : NUM TYPE I VALUE 5.
CLASS-METHODS :CLASS_CONSTRUCTOR.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE:/5 'I am class constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
write:/5 'Hello'.
write:/5 c1=>num.
|
Output
|
I am class constructor
Hello
5
|
Static/Class constructors cannot have any interface |
|
Theme
|
This program will show you that static constructors of a
class cannot have any interface parameters and exceptions
|
Program Description
|
In this program, the class C1 contains a class
constructor which is having an import parameter , NUM.
The program
could not be successfully compiled due to such attempt.
|
Dump
|
REPORT YSUBOOPS2.
CLASS c1 DEFINITION .
PUBLIC SECTION.
CLASS-METHODS :CLASS_CONSTRUCTOR IMPORTING
NUM TYPE C.
ENDCLASS.
|
Output
|
Compilation of the progran will fail with an error
message:-
“The method CLASS_CONSTRUCTOR may not have parameters
or EXCEPTIONS”.
|
"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.