Sunday, January 19, 2014

OOABAP Instance Static Constructors Tutorial with Examples6

OOABAP Instance Static Constructors Tutorials with Examples
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:-
Generating an instance of a class using CREATE OBJECT obj, where obj has the data type REF TO class.

Calling a static method using [CALL METHOD] class=>meth.

Registering a static event handler method using SET HANDLER class=>meth for obj.

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.

Categories

ABAP (1) ABAP Interview Questions (112) ABAP Open SQL Statements (1) ABAP Syntax Rules (6) ABAP WORKBENCH (2) ABAP-Interview-Questions (52) ALE IDOC (6) ALE IDOC Interview Questions (6) ale-idoc (6) ALE-IDOC-Interview-Questions (19) ALV Interview Questions (5) ALV-Interview-Questions (22) BADI (2) BAPI (1) BAPI Interview Questions (1) BAPI-Interview-Questions (14) BDC (6) BDC Interview Questions (6) BDC-Interview-Questions (9) big data (2) big data interview questions (1) Classical Reports Interview Question (3) Classical-Reports-Interview-Questions (22) Conditional Statements (1) Cross Applications (3) Cross-Applications (14) Data Dictionary (22) Data Type Questins (1) Data types (1) Data-Dictionary (48) Data-Type-Questins (6) Dialog programming (5) Dialog Programming Interview Questions (4) Dialog-Programming (30) DOMAIN Interview Questions (1) Domain-Interview-Questions (8) Function Module (2) hadoop (2) hadoop interview questions (2) hdfs (1) IDoc Tutorials (6) Interactive Report Interview Questions (4) Interactive-Reports-Interview-Questions (22) Internal Tables (1) interview questions (1) Lock Object Interview Questions (1) Lock-Objects-Interview-Questions (10) Logical Database (1) Modularization Interview Questions (4) Modularization-Interview-Questions (25) Module Pool Programming (5) Module-Pool-Programming (39) modules in sap (1) Object Oriented ABAP (19) Object Oriented ABAP Interview Questions (15) object-oriented-abap (2) Object-Oriented-ABAP-Interview-Questions (34) OOABAP (9) Reports (14) Reports Interview Questions (9) Reports-Interview-Questions (19) RFC (1) RFC Interview Questions (1) RFC-Interview-Questions (14) RICEF (1) RICEF Objects (1) SAP (4) SAP ABAP (4) SAP ABAP Interview Questions (42) SAP ABAP Introduction (46) SAP ABAP Message Types (2) SAP BADI Interview Questions (2) SAP Basics (71) SAP Books (2) SAP Certification (1) SAP CONSULTANTS (5) SAP CRM (1) SAP ENHANCEMENTS (3) SAP EXITS (2) SAP EXITS ( SAP ENHANCEMENTS ) Interview Questions (1) SAP Free Books (1) SAP HR (2) SAP Lock Object (1) sap modules (2) SAP Open SQL Statements (1) SAP R/3 Architecture (4) SAP Search help (1) SAP Smartforms (1) SAP Smartforms Interview Questions (2) SAP Tables (5) SAP Tcodes (10) SAP Views (1) SAP Webdynpro ABAP (12) SAP Work Processors (2) SAP Workflow (3) SAP-BADI-Interview-Questions (11) SAP-Enhancements (39) SAP-Exits (39) SAP-Exits-Enhancements-Interview Questions (3) SAP-HANA (1) SAP-HANA-Interview-Questions (1) SAP-Smartforms-Interview-Questions (2) SAP-Workflow (3) Scripts (3) Scripts Interview Questions (2) Scripts-Interview-Questions (32) Search Help Interview Questions (1) Search-Help-Interview-Questions (9) Smartforms (1) Table Maintenance Generator (1) Table-Maintenance-Generator (4) Tables in SAP (2) Tables Interview Questions (3) Tables-Interview-Questions (3) Type Group Interview Questions (1) Type-Group-Interview-Questions (7) Variable Declaration (1) Views Interview Questions (1) Views-Interview-Questions (5) Webdynpro (12) what is big data (1)

Protected Blog

 
This blog is not affiliated to SAP AG |SAP is trademark of SAP AG |The information collected from various sources use information with your own risk.