Friday, January 17, 2014

ABAP Object Oriented Programming Tutorials Examples 1

object-oriented-abap-tutorials-with-examples
ABAP Object Oriented Programming Tutorials Examples 1, OOABAP Programming Tutorials with Examples, Welcome to SAP ABAP Interview Questions.



SAP ABAP Object Oriented Programming Tutorials & Examples

Hello ABAP programmers, we hope you are doing good today!! We sapabapiq.com started a new series of posts on SAP OOABAP tutorials with examples to learn and understand the object oriented programming concepts in ABAP. This is the first post in this series of ABAP Object Oriented Programming Tutorials with examples. This is basically for novice ABAP programmers, if you are an experienced consultant just refresh your knowledge. 



What is a Class in OOABAP ?



Classes are the central element of object-orientation.

A Class is an abstract description of an object.

Classes are templates for objects.

The attributes of objects are defined by the components of the class, which describe the state and behavior of objects.

You define global classes and interfaces in the Class Builder (Transaction SE24) in the ABAP Workbench.

They are stored centrally in class pools in the class library in the R/3 Repository.

All of the ABAP programs in an R/3 System can access the global classes.




OOABAP Programming Tutorials with Examples





1)    Accessibility of different sections of a class








Theme of the Program

From this program, one will learn:-

  1. How to define, implement and instantiate a class.
  2. What are the different sections of visibility in a class.
  3. How to define instance attributes and get them accessed by external users.

The following program will also show that :-

*  Data declared in public section can be accessed by the class itself, by its sub classes as well as by other users outside the class.
*  Data declared in the protected section can be accessed by the class itself,  and also by its sub classes but not by external users outside the class.
*  Data declared in the private section can be accessed by the class only, but not by its sub classes and by external users outside the class.





Brief Description of the Program

This program contains a class : parent class with following attributes in different sections:-

  Commondata     in public section

  Protectdata       in   private section

  Privatedata       in   private section

The method showval in class : parentclass displays values of all the attributes.

This demonstrates that class can access all its attributes.

Class childclass is a subclass of  class parentclass, which has a method : subval.

It displays the value for the data : commondata and protectdata .

Then, it changes the values for both and displays them again.

This demonstrates that subclass can access/change public/ protected attributes of superclass.

In the START-OF-SELECTION event, object : parent is instantiated from class : parentclass and object : child is instantiated from class : childclass.

Then , the method showval of parent(object of parentclass) and method subval of child(object of childclass) is called , which displays the values of  different attributes.


Then, the public attribute of object parent is changed and the changed value is displayed.


This demonstrates that external users can change/ display public attributes of a class.



OOABAP Program Code

REPORT  YSUBDEL LINE-SIZE 120.

 CLASS parentclass DEFINITION .
 PUBLIC SECTION.
  DATA : commondata(30) type c value 'Accessible to all'.
  METHODS : SHOWVAL.
 PROTECTED SECTION.
  DATA : protectdata(40) type c value 'Protected data'.
 private section.
 data : privatedata(30) type c value 'Private data'.
 ENDCLASS.

 CLASS parentclass IMPLEMENTATION.
 METHOD : SHOWVAL.
  write:/5 'All data from parentclass shown:-'.
  write:/ sy-uline.
  WRITE:/5 COMMONDATA,
        /5 PROTECTDATA,
        /5 PRIVATEDATA.
 endmethod.
endclass.

CLASS childclass DEFINITION INHERITING FROM parentclass.
 PUBLIC SECTION .
 METHODS : subval.
 ENDCLASS.

 CLASS childclass IMPLEMENTATION.
 method : subval.
  skip 1.
  write:/5 'Data of parent shown from child-'.
  write:/5 sy-uline.
  WRITE:/5 COMMONDATA,
        /5 PROTECTDATA.
  Commondata = 'Public data changed in subclass'.
  Protectdata = 'Protected data changed in subclass'.
  write:/5 sy-uline.
  WRITE:/5 COMMONDATA,
        /5 PROTECTDATA.
 endmethod.
endclass.

START-OF-SELECTION.
   DATA : parent type ref to parentclass ,
          child  type ref to childclass  .
   create object : parent ,
                   child  .
   call method : parent->showval ,
                 child->subval.
      skip 2.
  parent->commondata = ‘User changing public data’.
  write:/5 parent->commondata.



 
  







Output of the ABAP Program
                                 
 All data from parentclass shown:-  
                                    
 Accessible to all                  
 Protected data                     
 Private data                       
                                     
 Data of parent shown from child-   
                                    
 Accessible to all                  
 Protected data                     
                                    
 Public data changed in subclas     
 Protected data changed in subclass 
                                    
                                    
 User changing public data     
                                    
                                    





2)    Subclass cannot access the private component of superclass



Theme of the Program


The program demonstrates that subclasses cannot access the private components of superclass.



OOABAP Program description

The program used here is similar to above with change in the method : subval of class : childclass . This method is now attempting to access the attribute : privatedata , which is a private attribute of  its superclass : parentclass.

On compilation, the program will give a compilation error.


This demonstrates that private components of superclass cannot be accessed by subclasses.




OOABAP Programming Example
Take the first program. Only change the method : subval of class : childclass as follows:-

method : subval.
   skip 1.
    write:/5 'All data from parent class shown by subclass'.
   write:/5 sy-uline.
  WRITE:/5 COMMONDATA,
        /5 PROTECTDATA,
        /5 privatedata.
endmethod.




Output of the OOABAP Program
The program will not compile. It will show an error message:-

ooabap-error-message







3)    External users cannot access protected/private components of a class



Theme of the Program


This program will demonstrate that external users cannot access the protected and private components of a class




OOABAP Program Description

In this program , class C1 has three attributes declared in different sections as follows:-

* Commondata      in   public section

*  Protectdata       in   private section

*  Privatedata        in   private section


In the main program, an object , OBJ1 is created from   class C1 and an incorrect attempt is   made   to display the protected and private attribute of class C1 using its object OBJ1.


Compilation of this program produces an error.


This demonstrates :  protected and private components of a class cannot be accessed by external users.






Example code of OOABAP Program

REPORT  YSUBDEL LINE-SIZE 120.

 CLASS c1 DEFINITION .
 PUBLIC SECTION.
  DATA : commondata(30) type c value 'Accessible to all'.
 PROTECTED SECTION.
  DATA : protectdata(40) type c value 'Protected data'.
 private section.
 data : privatedata(30) type c value 'Private data'.
 ENDCLASS.

 CLASS c1 IMPLEMENTATION.
 endclass.


START-OF-SELECTION.
   DATA : obj1 type ref to c1.
   create object : obj1.
   write:/5 obj1->protectdata ,
            obj1->privatedata.
  



Output of the Program

On compilation, an error will be generated which will prove that protected and private components of a class cannot be accessed by external users.





Read continuation of SAP Object Oriented Programming Tutorials with Examples in next posts.

"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.