You are on page 1of 22

OO ABAP CONCEPTS

Prepared By:
Shahir A. Mirza

04-Mar-2012
Table of Contents
Topics Page No.

1. Introduction to OO Programming 4

1.1 Procedural programming 4


1.2 OO Programming 4
1.3 Why OO Programming? 4

2. Basic Concepts of and Principals of OOPS 5

2.1 Encapsulation 5

2.1.1 Visibility sections 5


2.1.2 Example 5

2.2 Abstraction 5
2.3 Polymorphism 6

2.3.1 Example 6

2.4 Inheritance 6

2.4.1 Example 6

3. Classes 7

3.1 Local and Global Classes 7


3.2 Global Class 7
3.3 Local Class 7

3.3.1 Local Classes 7


3.3.2 Structure of a Class 8

4. Attributes 8

4.1 Instance Attributes 8

4.1.1 Example 8

4.2 Static Attributes 9

5. Methods 9

5.1 Types of Methods 9

5.1.1 Instance Methods 9


5.1.2 Static methods 9
5.1.3 Examples 10

5.2 Functional Methods 10


5.3 Redefinition of Methods 10

2
5.3.1 Example 11

6. Objects 11

6.1 Example 11

7. Events 12

7.1 Instance Events 12


7.2 Static Events 12
7.3 Example 12
7.4 Concepts 12

8. Instantiation And Constructor 13

8.1 Instantiation 13
8.2 Constructor 14

8.2.1 Static Constructor 14

8.3 Example 14

9. Self Reference 15

9.1 ‘Me’ reference variable 15


9.2 Example 15

10. Casting 15

10.1 Widening Cast 15


10.2 Narrowing Cast 16
10.3 Example 16

11. Interfaces 16

11.1 Compound Interfaces 17

12. Global Class and Interfaces 17

12.1 Examples 17

13. Abstract Classes 18

13.1 Examples 18

14. Final Classes 19

15. Singleton Concept 19

15.1 Examples 20

16. Friend Concept 20

17. Exceptions 20

3
17.1 Defining Class based Exceptions 21
17.2 Raising Class based Exceptions 21

4
OO ABAP CONCEPTS
1. Introduction to OO Programming:

Object-oriented programming (OOP) is a computer science term used to characterize a


programming language that began development in the 1960’s. The term ‘object-oriented
programming’ was originally coined by Xerox PARC to designate a computer application that
describes the methodology of using objects as the foundation for computation. OOPs use three basic
concepts as the fundamentals for the programming language: classes, objects and methods.
Additionally, Inheritance, Abstraction, Polymorphism, Event Handling and Encapsulation are also
significant concepts within object-oriented programming languages. There are many terms in Object
Oriented programming; together these terms are called as OOP s concepts.

1.1 Procedural programming

The procedural programming has following features:

1.1.1 Data and functions are kept separately.

1.1.2 Usually non-encapsulated access to data.

1.1.3 Possibility of encapsulating functions using modularization.

1.1.4 The main program cannot access the function group’s Data Object directly.

1.2 OO Programming

1.2.1 An ABAP object is a systematic extension of ABAP.

1.2.2 ABAP Objects statements can be used in procedural ABAP programs.

1.2.3 Objects (Classes) contain procedural ABAP statements.

1.2.4 In the Object Oriented Context:

 Data is treated as a critical element.


 It ties data more closely to the functions.
 Decompose problems into number of entities called Objects.
 Objects = Data + Methods.
 Type check is very strict.
 Obsolete statements are prohibited.

1.3 Why OO Programming?

The aims of OO programming are following:

 Building of Objects around the Real world.


 Implement the Processes Realistically.
 Improving software Structure and consistency in the development process.
 Reduce the maintenance requirements.
 Involve the Modeller, Customer and Developer in analysis and Design Phase for
better “implementation” of project.

5
2. Basic Concepts of and Principals of OOPS:

2.1 Encapsulation

Encapsulation means the implementation of an object is hidden from other components in the
system. Objects restrict visibility of their resources (attributes and methods) to other users.

2.1.1 Visibility sections:

 Private
Private section provides the highest degree of protection. All the
variables/methods declared in the class are only visible to that class itself. Hence
cannot be accessed by subclass.

 Public
When variables/methods are declared in “Public section” it is not only visible
in the defined class but also visible to all the classes which are defined outside the
class. This is possible simply by declaring all the variables and methods in “Public
Section”. The Variables and methods can be accessed everywhere, this is what we
want to prevent in many programs.

 Protected
When variables and methods are declared in “Protected section” it is not only
visible in the defined class but also visible in the subclass.

Encapsulation is supported through classes.

2.1.2 Example

Figure.1. Usage of Visibility Sections

2.2 Abstraction

Abstraction is to show only necessary details to the client of the object without disclosing
all the details.

Example: We don’t know the inner details of the Monitor of our PC. What happens when we
switch on the Monitor? It doesn’t matter for us what is happening inside the monitor,
important thing for us is, whether monitor is ON or NOT.

6
2.3 Polymorphism

Poly means many, morph means forms. Polymorphism means ability to take more
than one form. Polymorphism can be achieved by using:

 Interfaces.

 Inheritance (By Redefinition).

2.3.1 Example

Figure.2. Usage of Interfaces

2.4 Inheritance

Inheritance is the process by which the objects of one class acquire the properties of
objects of another class.
 Mainly used for “Reusability”.
 Super Class / Base Class / Parent Class.
 Sub Class / Derived Class / Child Class.
 Single Inheritance (Single, Hierarchical, Multilevel).
 “Multiple Inheritance” is not supported in OOABAP, but it can be achieved by using
“Interfaces”.

2.4.1 Example

Figure.3. Usage of Inheritance.

7
3. Classes

A class is a set of objects that have the same structure and same behavior.

 All components of the class are defined in the “Definition part”. The components are
attributes, methods, events, constants, types and implemented interfaces.
 Only methods are implemented in “Implementation Part”.
The “Class” statement cannot be nested.

3.1 Local and Global Classes

As mentioned earlier a class is an abstract description of an object. Classes in ABAP


Objects can be declared either globally or locally.

3.2 Global Class:

Global classes and interfaces are defined 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.

3.3 Local Class:

Local classes are define in an ABAP program (Transaction SE38) and can only be used in
the program in which they are defined.

Global Class Local Class

Accessed By Any program Only the program where it is defined.

Stored In In the Class Repository Only in the program where it is defined.

Created By Created using transaction SE24 Created using SE38

Namespace Must begin with Y or Z Can begin with any character

3.3.1 Local Classes

Every class will have two sections.


(1) Definition. (2) Implementation
Definition: This section is used to declare the components of the classes such as
attributes, methods, events .They are enclosed in the ABAP statements CLASS ...
ENDCLASS.
CLASS <class> DEFINITION.
...
ENDCLASS.

8
Implementation: This section of a class contains the implementation of all methods of
the class. The implementation part of a local class is a processing block.
CLASS <class> IMPLEMENTATION.
...
ENDCLASS.

3.3.2 Structure of a Class

The following statements define the structure of a class:


 A class contains components
 Each component is assigned to a visibility section
 Classes implement methods

4. Attributes

Attributes contain the data that can be stored in the objects of a class. Class attributes can be
one of the below types
 Elementary
 Structured
 Table-type
 Object References
 Interfaces References

 They can consist of local or global data types or reference types.


 “Type” is used to refer to data types.
 “Like” is used to refer to “Local Data Objects”.
 “Read-Only” used with Public variables. Attribute can be read from outside but can
be only changed by methods of class.
 “TYPE REF TO” is used to refer to “Reference” Variables.

There are two types of attributes. They are as follows:

4.1 Instance Attributes

Instance attributes are attributes that exist once per object, that is, once per runtime instance
of the class. They are defined with the syntax element DATA.

4.1.1 Example

9
4.2 Static Attributes

Static attributes exist once for each class and are visible for all runtime instances in that class.
They are defined with the syntax element CLASS-DATA. Static attributes are also referred to as
“Class Attributes”.

5. Methods

Methods are internal procedures in classes that determine the behavior of the objects. They
can access all attributes in their class and can therefore change the state of other elements.

 If we have only data fields then there is no life to it. We should use “methods” which
operate on this data.
 For manipulating data we require methods. Methods determine the behavior of the object.
 Methods have a signature that enables them to receive values when they are called and
pass values back to the calling program.
 Methods can have any number of IMPORTING, EXPORTING, AND CHANGING
parameters. All parameters can be passed by value or reference.
 One method return value can be defined using the “RETURN” parameter. It must always
be transferred as a value. In this case EXPORTING, CHANGING Parameters cannot be
defined.
 All input parameters (IMPORTING & CHANGING) can be defined as OPTIONAL or
DEFAULT additions. These parameters then don't necessarily have to be transferred
when the object is called.
 If OPTIONAL addition is used, the parameter remains initialized according to TYPE,
whereas DEFAULT addition allows entering a start value.
 Methods also support the return value SY-SUBRC, but only if the signature exceptions
were defined using the exceptions.
 The RAISING addition can be used in its place to propagate class-based exceptions. The
caller then handles these class-based exceptions without evaluating the SY-SUBRC return
value.

5.1 Types of Methods:

5.1.1 Instance Methods

 Both Static and Instance components can be accessed.

 Definition using METHODS.

5.1.2 Static methods

 Only static components can be accessed.

 Definition using CLASS-METHODS.

10
5.1.3 Examples

Figure.4. Usage of Instance and Static Methods.

5.2 Functional Methods

Methods that have a RETURNING parameter are described as functional methods.

 These methods cannot have EXPORTING or CHANGING parameters.

 It can have IMPORTING parameters and EXCEPTIONS.

 Functional methods can be used directly in various expressions:

 MOVE, CASE, LOOP Statement


 Logical Expressions (IF, WHILE, CHECK,WIT UNTIL)
 Arithmetic and bit expressions (COMPUTE)

5.3 Redefinition of Methods

The methods of the super class can be re-implemented at the sub class. Providing a new
implementation to the inherited method in a subclass is known as Redefinition.

Purpose to redefine methods: If the method implementation at the super class does not satisfy
the requirement of the object which is created with reference to the sub class. Redefined methods
must be Re-implemented in subclass. Within the redefined method, we can access components of
the direct super class using the SUPER reference.

 Inherited methods can be redefined in subclasses.


 Redefined methods must be re-implemented in subclasses.
 The signature of redefined methods cannot be changed.
 Static methods cannot be redefined.
 In inheritance, static components are "shared".
 A class shares its non-private static attributes with all its subclasses.

11
5.3.1 Example

Figure.5. Usage of Redefinition of Methods

6. Objects

Object is an instance of a class. Each object has unique identity and its own attributes.

 Objects are created using the statement “Create Object”.


 Objects can only be created and addressed using the “Reference Variable”.
 A class can have any number of objects.
 When an object is created a memory will be allocated for the attributes.

6.1 Example

Figure.6. Creation and Usage of Objects.

12
7. Events

Objects or classes can use events to trigger event handler methods in other objects or classes.
With events, the handler determines the events to which it wants to react. There are two types of
Events:

7.1 Instance Events:

 Instance events are declared using the “EVENTS” statement and only can be triggered in
an instance method.

7.2 Static Events:

 Static events are declared using “CLASS_EVENTS” statement. All methods, i.e.
instance/static method can trigger static events. Static events can be triggered in a static
method.

7.3 Examples

Figure.7. Usage of Events

7.4 Concepts:

At the moment of implementation, a class defines its:

 Instance events (using the EVENTS statement)


 Static events (using the CLASS-EVENTS statement)

Classes or their instances that receive a message when an event is triggered at runtime and
want to react to this event define event handler methods.

Statement:

[CLASS-]METHODS <handler_method> FOR EVENT <event> OF <classname>.

These classes or their instances are registered to one or more events at runtime.

13
Statement:

SET HANDLER <handler_method> FOR <reference>. (for instance events).

SET HANDLER <handler_method>. (for static events).

A class or instance can trigger an event at runtime using the RAISE EVENT statement. Both
instance and static events can be triggered in instance methods. Only static events can be triggered
in static methods. Events can only have EXPORTING parameters which must be passed by value.
Triggering an event using the statement RAISE EVENT has the following effect:

 The program flow is interrupted at that point.


 The event handler methods registered to this event are called and processed.
 Once all event handler methods have been executed, the program flow continues.

If an event handler method in turn triggers an event, then the program flow is again
interrupted and all event handler methods are executed (nesting). Event handler methods are triggered
by events (RAISE EVENT), although they can also be called like normal methods (CALL
METHOD).

The interface of the event handler method consists solely of IMPORTING parameters. You
can only use parameters from the definition of the corresponding events (event interface). An event
interface, which only has EXPORTING parameters, is defined using the EVENTS statement in the
declaration of the event. The parameters are typed in the event definition and the typing is passed to
the event handler method, that is, the interface parameters of the event handler method cannot be
typed in the definition of the event handler method. In addition to the explicitly defined event
interface parameters, the implicit parameter sender can also be listed as an IMPORTING parameter
for instance events. This passes on a reference to the object that triggered the event.

 When an event is triggered, only those event handler methods are executed that have, by
this point, registered them using SET HANDLER.
 You can register an event using ACTIVATION 'X', and deregister it using
ACTIVATION space. If you do not specify ACTIVATION, then the event registers
(default behavior).
 We can register several methods in one SET HANDLER statement:

SET HANDLER <ref_handle1>-><handler_method1>

...

<ref_handleN>-><handler_methodN>

FOR <ref_sender> | FOR ALL INSTANCES.

8. Instantiation And Constructor

8.1 Instantiation:

A class is a set of Objects. And Object consists of attributes and methods. During the
program runtime the class used to create objects (instances) in the memory. This process is
known as “Instantiation”. (Creating an Object is known as Instantiation).

14
8.2 Constructor:

The constructor is a special instance method in a class and is always named


“CONSTRUCTOR”.
 The constructor initializes the object itself. Constructor is automatically called at the
runtime with “CREATE OBJECT” statement.
 Constructors are called implicitly.
 Each class can have only one “Instance constructor”.
 The “constructor “must be defined in “Public area”.
 The constructor’s signature can only have Importing parameters and exceptions.

8.2.1 Static Constructor

The static constructor is a special static method in a class and is always named
CLASS_CONSTRUCTOR. It is executed once for the program (and class).

 The static constructor is executed automatically whenever a class is accessed first time.

 Following points to be considered while defining the static constructor.

 Each class has only one static constructor

 The static constructor must be defined in the public area

 The static constructor's signature cannot have importing parameters or exceptions

 The static constructor cannot be called explicitly

8.3 Example

Figure.8. Usage of Constructors

15
9. Self Reference

9.1 ‘Me’ reference variable:

Whenever it is required to show the distinction between local data objects and instance
attributes with the same name then me reference variable is used.

9.2 Example

Figure.9. Usage of “me” Reference Variable.


10. Casting

There are two types of casting viz. Widening Cast (? =) and Narrowing Cast (=).

10.1 Widening Cast:

When we assign the instance of the super class to the subclass, then it is called as Widening-
Cast because we are moving to “More specific view” from the “Less specific view”.

 Every time it is not possible to move the Superclass reference to the Subclass, because
subclass will (or might) have always more functionality compare to the Superclass. So,
we need to be more careful when we use the down casting.

 When wide casting is used?

16
Widening cast can be used when we need to access the Specific functionality of the
subclass from the Superclass.

10.2 Narrowing-Cast:

When we assign the instance of the Subclass back to the instance of the Superclass then it is
called as "Narrow Casting" because we are switching from a "More Specific view of an object" to
"less specific view".

10.3 Example

Figure.10. Usage of Casting in OOPS


11. Interfaces

Interfaces are like super classes which cannot be instantiated and all the components of
Interfaces are Public only.

17
 Interfaces are declared using the keyword INTERFACE and may only contain method
signature and constant declarations.
 Interfaces will not have Implementation part. It is the class responsibility to implement
the interface method.
 Various classes can implement the services in different ways, but keep the same
semantics. Therefore interfaces don’t have implementations.
 One benefit of interface is that they simulate multiple inheritances. As interface may
extend any number of interfaces as they do not contain implementations.

11.1 Compound Interfaces

A Compound Interface contains another Interface as a component and its own components.

 An elementary interface does not itself contain other interfaces.

 One interface can be used as a component Interfaces in “multiple compound interfaces”.

12. Global Class and Interfaces

 ABAP Workbench development tool: Class Builder(SE24)


 Tool for creating, testing, and managing global classes and interfaces
 Stored in Repository
 Generates the framework coding,
 for example CLASS <name> DEFINITION
 Manages the include programs in which the coding is stored
 Access from all programs using TYPE REF TO
 Customer namespace for global classes and interfaces: Y* or Z*
 Where-used list available

12.1 Example

Figure.11. Global Class Builder

18
Figure.12. Global Interface in Class Builder

13. Abstract Classes

Abstract classes are classes that contain one or more abstract methods. An abstract method is
a method that is declared, but contains no implementation. Abstract classes may not be
instantiated, and require subclasses to provide implementations for the abstract methods.
 Usually super classes are defined as “Abstract” where we do not want Superclass to
be “instantiated” but its subclass can.
 Abstract Methods: When a method is defined as abstract that means its
implementation is open i.e. abstract methods can be defined in the abstract classes but
it cannot have the implementation part.
The class which is a “subclass” of an “abstract class” and if it is not an abstract class then the
“abstract method” should be “redefined” in the subclass. Static methods cannot be abstract
because they cannot be redefined.

13.1 Examples

19
14. Final Classes

 Final class cannot be extended.


 To prevent a class from being “inherited” the “Final” statement is used with the Class
statement.
 All the methods of a Final class are implicitly final. Therefore methods in final class
should not be defined with the “Final” Keyword.
 Classes that are “Final” and “Abstract” should contain only “static components”.
 A final variable can only be assigned once. If the variable is a field of a class, it must
be assigned in the constructor of its class. Any attempt to reassign variable will lead
to compile error.
 This is done for reasons of security and efficiency.
 Final Method: To prevent a method from being “Redefined” “Final” addition is used with the
“METHODS” statement.

15. Singleton Concept

Classes without Multiple Instantiation: When a class needs to be instantiated only once then
it can be achieved through “Singleton Concept”. -> A Singleton class is a class that will only have
one instance of the class. In certain cases, we want to make sure that we cannot instantiate
multiple copies of the object, so we limit it to just one copy. Instead of having a public constructor
for our class, we use a private constructor. Then we use a public method. Often, there is a
requirement that you would want to create only one instance of the class. To put it in other words,
you do not want anyone to create instance of the class if already created. This is a common
requirement in many application designs. This can be achieved using the singleton pattern. In this
design pattern, we will declare the constructor as a 'Private' one. Go to the properties tab of class
builder (SE24) and change the instantiation type to private. Then you can create a private
constructor. The advantage of having a private constructor is that now no one can create an object
of your class, not even you.
So how do we create the instance of this class? We create a static method GET_INSTANCE
that will be used to create the instance. A static method because this method can be called directly
with the class reference (without an object). Inside this method, we can create the object (because
the constructor is a private method, we can call it within class).
Now, how to make sure that only one instance is created? Store the own reference as an
attribute of the class. In the method GET_INSTANCE check if this attribute is populated. If it
populated then return the same instance. If not, create a new one, store it in the own attribute and
return the instance.

20
15.1 Example

Figure.13. Concept of Singleton.

16. Friend Concept

Whenever a situation arises where one class wants to access the private or protected
components of the other class regardless of the visibility of constructors, then it can be achieved
through “Friendship class”.
 Granting friendship is one-sided: A class granting a friendship is not automatically a
friend of its friends.
 The friend attribute can be inherited.
 Granting friendship is not inherited. i.e. A friend of a super class is not automatically a
friend of subclass.
If a class granting a friendship wants to access the non-public components of a friend, this
friend must also explicitly grant the friendship to it.

17. Exceptions

Exception is used to refer to a situation that arises while executing the program, where there is no
point in continuing to run the program in a normal way.

21
17.1 Defining Class based Exceptions

 We can define our own Exception classes, but there are already a range of predefined
exception classes available in the system. Particularly for exceptions in the runtime
environment.
 Exception classes usually created globally in the Class Builder, but local exception
classes can also be defined within a program or global classes.
 The use of class-based exceptions is not limited to object-oriented contexts. Class-based
exceptions can be raised and handled in all processing blocks.
 In particular, all previously catchable runtime errors can now be handled as class-
based exceptions.
 In the SAP standard system, the names of all exception classes start with CX_ or
ZCX_.

17.2 Raising Class based Exceptions

 Class-based exceptions are raised either by the RAISE EXCEPTION statement or by the
runtime environment.

 Example: Division by zero is an example for runtime error.

 In an exception situation, an exception is represented by an exception object – that is,


an instance of an exception class.

 The attributes of each exception object contain information about the error situation.

 If a class-based exception is raised, the system interrupts the normal program flow
and tries to navigate to a suitable handler.

 If it cannot find a handler, a runtime error occurs.

22

You might also like