Sei sulla pagina 1di 68

INTERNAL

OO ABAP

Ravi Andela
Nov 22, 2014
Content Prepared By
Uma Maheshwari
Shainaj Ningnur
Ravi Andela

Reviewed By
Suresh Babu Suresetti

2
Introduction

OO ABAP Ravi Andela


History Of OO ABAP

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.

 Objects restrict visibility of their resources(attributes and


methods) to other users.
 Visibility sections are:
 Private
 Public
 Protected
 Encapsulation is supported through classes.

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.

 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, So it
can be achieved by using “Interfaces”.

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.

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

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

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

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

 The static constructor is called automatically before the class is first


accessed
 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

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.

In rare cases, classes have to work together so closely that they


need access to each others' protected or private components.
The concept of friendship between classes prevents these
components from being made available to all users at the
same time.

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

Potrebbero piacerti anche