Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OO ABAP
Ravi Andela
Nov 22, 2014
Content Prepared By
Uma Maheshwari
Shainaj Ningnur
Ravi Andela
Reviewed By
Suresh Babu Suresetti
2
Introduction
4
Procedural Programming
Data and functions are kept separately.
Usually non-encapsulated access to data.
Possibility of encapsulating functions using modularization.
The main program cannot access the function group’s Data
Object directly
5
Object Oriented Programming
ABAP objects is a systematic extension of
ABAP.
ABAP Objects statements can be used in
procedural ABAP programs.
Objects(Classes) contain procedural ABAP
statements.
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.
6
Aims of OO Programming
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 Modeler, Customer and Developer in analysis
and Design Phase for better “implementation” of project.
7
Concepts and Principles of OOPS
Encapsulation
Abstraction
Polymorphism
Inheritance
8
Encapsulation
Encapsulation means the implementation of an object is
hidden from other components in the system.
9
Abstraction
Abstraction is to show only necessary details to the client of
the object without disclosing all the details.
Example 1:
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.
Example 2:
we have a existing method “Calculate Salary” in Employee class, which
takes Employee ID as a parameter and returns the “Salary” of the
employee. So here we know the necessary details that we should pass
employee ID to that method and we will get the result “ Calculated salary”
but other details like how the salary is getting calculated in that method is
not disclosed.
10
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).
11
Inheritance
Inheritance is the process by which the objects of one class
acquire the properties of objects of another class.
12
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.
Syntax
13
Attributes
Attributes contain the data that can be stored in the objects of
a class.
Syntax:
14
Attributes – Contd.
There are two kinds of attributes
Instance Attributes
Static 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.
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”.
Example:
15
Methods
There are two types of methods.
Instance Methods
Static methods
16
Objects
Object is an instance of a class. Each object has unique identity
and its own attributes.
17
Events
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.
The events can be
Instance Events: Instance events are declared using the “EVENTS”
statement and only can be triggered in an instance method.
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.
18
Visibility
Accessibility of attributes and methods is restricted using
Visibility modifiers.
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.
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.
19
Visibility – Contd..
20
Accessing Attributes and Methods
21
Instantiation And Constructor
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).
22
Constructor Example
23
Static Constructor
The static constructor is a special static method in a class and is always named
CLASS_CONSTRUCTOR. It is executed no more than once per program (and
class).
24
Static Constructor Example
25
Instance And Static Methods
Instance Methods: Instance methods are declared using the “METHODS”
statement. They can access all of the attributes of a class and can be
called using an instance.
Static methods: Static methods are declared using the “CLASS-
METHODS” statement. They can only access static components and can
be called using class.
26
Events
Events
28
Features
Looser linkage than for a method call
Triggering object makes status change known
Handler objects can show interest and react
Different communication model
The trigger does not know the user;
user's reaction is of no interest to trigger
Areas of use
GUI implementations
Conformity with other object models: COM, ActiveX
controls, OpenDoc
29
Triggering and Handling Events: Overview
30
Defining and Triggering Events: Syntax
31
Event Handler Methods
32
Registering Events
33
Registering for an Event: Syntax
34
Registration/Deregistration: Handler Tables
35
Event Handling: Features
Event handling is sequential & Sequence in which event
handler methods are called is not defined
With regard to the Garbage Collector, registration has the
same effect as a reference to the registered object and
Registered objects are never deleted
The visibility of an event defines authorization for event
handling
The visibility in an event handler method defines
authorization for using SET HANDLER statements and Event
handler methods, however, can only have the same visibility
or more restricted visibility than the events they refer to
36
Self Reference (me)
37
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)
38
Superclass And Subclass
The mechanism of deriving a new class from an old class is
called as Inheritance.
The Old class is known as Base class or Super class. It is a
Generalization of its subclass.
New class is called as a Derived class or Subclass. It is a
specialization of its super class.
Common components only exists once in the superclass, so
they can be maintained centrally.
Components in the superclass are available in all
subclasses: avoids redundant implementations.
Subclasses contains extensions/changes (Programming by
difference).
Subclasses are dependent on super classes.
39
Superclass, Subclass Example
40
Redefinition Of Methods
Providing a new implementation to the inherited method in a
subclass is known as Redefinition.
•Redefined methods must be Re-implemented in subclass.
41
Interfaces
Interface
43
Interface Example
44
Defining and Implementing an Interface
45
Interfaces: Features
46
Polymorphism and Interfaces
47
Interface Hierarchy
48
Compound Interfaces
49
Compound Interfaces: Example
50
Global & Local
Classes
Review: Local Classes and Interfaces
52
Class Builder
And the Test Environment
Global Classes and Interfaces
ABAP Workbench development tool: Class Builder
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
54
Class Builder
55
Class Builder: Test Environment
56
Abstract & Final
Classes
Abstract Classes
Abstract classes themselves cannot be instantiated (although their
subclasses can)
References to abstract classes can refer to instances of
subclasses
Abstract (instance) methods are defined in the class, but not
implemented
58
Final Classes
59
Visibility Of
Constructors
Who Can Instantiate Classes?
The usability of classes can be restricted:
CLASS CL_CLASS DEFINITION ...
CREATE PUBLIC
Every user (client) can create instances of a class (default setting)
CREATE PROTECTED
Only the class itself and all its subclasses can create instances of this
class
CREATE PRIVATE
Only the class can create instances of itself
This implicitly controls the visibility of the class
Possible use:
Singleton concept
61
Friends Concept
Friendship Between Classes
In classes, there is normally a strict division between outside
(PUBLIC) and inside (PROTECTED or PRIVATE). A user can only
access the public components of a class. This permits changes
to the implementation inside a class without invalidating its
users.
63
Friends
A class can grant other classes and interfaces (and hence all
classes that implement this interface) its friendship. This is the
purpose of the FRIENDS additions to the CLASS_DEFINITION
statement, which lists all the classes and interfaces that are
granted friendship. These friends gain access to the protected
and private components of the class granting the friendship
and can always create instances of this class, independently of
the CLASS statement's CREATE addition.
64
Friendship is Unilateral
In principle, granting of friendship is one-sided: A class
granting a friendship is not automatically a friend of its friends.
If the class granting the friendship wants to access the private
components of a friend, then the latter has to explicitly grant
friendship to the former.
65
Questions?
66
Thank You!
RAVI ANDELA
www.altimetrik.com