0% found this document useful (0 votes)
2 views13 pages

OOABAP Theory

The document provides an overview of object-oriented programming (OOP) concepts, emphasizing the distinction between procedural and object-oriented approaches. It details the structure and components of classes in ABAP Objects, including attributes, methods, and visibility sections, as well as the benefits of OOP such as encapsulation, inheritance, and polymorphism. Additionally, it explains how to create and manage objects, along with their lifecycle and the use of object references.

Uploaded by

lukaswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

OOABAP Theory

The document provides an overview of object-oriented programming (OOP) concepts, emphasizing the distinction between procedural and object-oriented approaches. It details the structure and components of classes in ABAP Objects, including attributes, methods, and visibility sections, as well as the benefits of OOP such as encapsulation, inheritance, and polymorphism. Additionally, it explains how to create and manage objects, along with their lifecycle and the use of object references.

Uploaded by

lukaswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

History of programming languages:

What is Object Orientation?


Object orientation (OO), or to be more precise, object-oriented programming, is a
problem-solving method in which the software solution reflects objects in the real world.

In the past, information systems used to be defined primarily by their functionality: Data
and functions were kept separate and linked together by means of input and output
relations.

The object-oriented approach, however, focuses on objects that represent abstract or


concrete things of the real world. These objects are first defined by their character and
their properties, which are represented by their internal structure and their attributes
(data). The behavior of these objects is described by methods (functionality).

The Object-Oriented Language Extension

ABAP Objects is a complete set of object-oriented statements that has been introduced
into the ABAP language. This object-oriented extension of ABAP builds on the existing
language, and is fully compatible with it. You can use ABAP Objects in existing
programs, and can also use "conventional" ABAP in new ABAP Objects programs.

Kalyan trainerkalyan@gmail.com
Comparison between Procedural and Object Oriented Programming

Features Procedure Oriented Object Oriented approach


approach
Emphasis Emphasis on tasks Emphasis on things that does
those tasks.

Modularization Programs are divided into Programs are organized into


smaller programs known classes and objects and the
as functions functionalities are embedded
into methods of a class.
Data security Most of the functions Data can be hidden and
share global data cannot be accessed by
external sources.
Extensibility Relatively more time New data and functions can
consuming to modify for be easily added whenever
extending existing necessary
functionality.

Structure of a Procedural Programming:

Object Oriented Programming:

Kalyan trainerkalyan@gmail.com
The main difference between real object orientation and function groups is that although
a program can work with the instances of several function groups at the same time, it
cannot work with several instances of a single function group. Suppose a program wanted
to use several independent counters, or process several orders at the same time. In this
case, you would have to adapt the function group to include instance administration, for
example, by using numbers to differentiate between the instances.

Object Oriented Approach - key features and Benefits.

* Complex software systems become easier to understand, since object-oriented


structuring provides a closer representation of reality than other programming techniques.
* In a well-designed object-oriented system, it should be possible to implement
changes at class level, without having to make alterations at other points in the system.
This reduces the overall amount of maintenance required.
* Through polymorphism and inheritance, object-oriented programming allows you to
reuse individual components.
* In an object-oriented system, the amount of work involved in revising and
maintaining the system is reduced, since many problems can be detected and corrected in
the design phase.

Classes: Class is a prototype that defines a real life entity which has both data and the
behavior common to all the objects of certain kind. Classes describe the real time objects.

Classes in ABAP Objects can be declared either globally or locally. You define global
classes and interfaces in the Class Builder (Transaction SE24) in the ABAP
Workbench.There are, however, a significant difference in the way that local and global
classes are designed. If you are defining a local class that is only used in a single
program, it is usually sufficient to define the outwardly visible components so that it fits
into that program. Global classes, on the other hand, must be able to be used anywhere.

Objects: objects are runtime instances of a class. In theory, you can create any number of
objects based on a single class. Each instance (object) of a class has a unique identity and
its own set of values for its attributes. They form a capsule which combines the character
to the respective behavior. Objects should enable programmers to map a real problem and
its proposed software solution on a one-to-one basis.

Object References: In a program, you identify and address objects using unique object
references. Object references allow you to access the attributes and methods of an object.

Kalyan trainerkalyan@gmail.com
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.

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

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

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.

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.

Kalyan trainerkalyan@gmail.com
Structure of a Class

The following statements define the structure of a class:

1. A class contains components


2. Each component is assigned to a visibility section
3. Classes implement methods

Components of a Class are as follows:

• Attributes: Attributes are internal data fields within a class that can have any ABAP
data type.
Instance Attributes: The contents of instance attributes define the instance-specific state
of an object. You declare them using the DATA statement.

Static Attributes: The contents of static attributes define the state of the class that is valid
for all instances of the class. Static attributes exist once for each class. You declare them
using the CLASS-DATA statement. They are accessible for the entire runtime of the
class.

• Methods:- Methods are internal procedures in a class that define the behavior of an
object. They can access all of the attributes of a class. This allows them to change the
data content of an object. They also have a parameter interface, with which users can
supply them with values when calling them, and receive values back from them.
METHOD <meth>.
...
ENDMETHOD.

You can declare local data types and objects in methods in the same way as in other
ABAP procedures (subroutines and function modules). You call methods using the
CALL METHOD statement.

Instance Methods

You declare instance methods using the METHODS statement. They can access all of the
attributes of a class, and can trigger all of the events of the class.

Static Methods

You declare static methods using the CLASS-METHODS statement. They can only
access static attributes and trigger static events.

Special Methods

Kalyan trainerkalyan@gmail.com
As well as normal methods, which you call using CALL METHOD, there are two special
methods called CONSTRUCTOR and CLASS_CONSTRUCTOR, which are
automatically called when you create an object (CONSTRUCTOR) or when you first
access the components of a class (CLASS_CONSTRUCTOR).

Declaring Methods

You can declare methods in the declaration part of a class or in an interface. To declare
instance methods, use the following statement:

METHODS <meth> IMPORTING.. <i1> TYPE type [OPTIONAL]..


EXPORTING.. <e1> TYPE type [OPTIONAL]..
CHANGING.. <c1> TYPE type [OPTIONAL]..
RETURNING VALUE (<r>)
EXCEPTIONS.. <ei>..

and the appropriate additions.

To declare static methods, use the following statement:

CLASS-METHODS <meth>...

Both statements have the same syntax.

When you declare a method, you also define its parameter interface using the additions
IMPORTING, EXPORTING, CHANGING, and RETURNING. The additions define the
input, output, and input/output parameters, and the return code. They also define the
attributes of the interface parameters, namely whether a parameter is to be passed by
reference or value (VALUE), its type (TYPE), and whether it is optional (OPTIONAL,
DEFAULT). Unlike in function modules, the default way of passing a parameter in a
method is by reference. To pass a parameter by value, you must do so explicitly using the
VALUE addition. The return value (RETURNING parameter) must always be passed
explicitly as a value. This is suitable for methods that return a single output value. If you
use it, you cannot use EXPORTING or CHANGING parameters.

• Events:- A mechanism set within a class which can help a class to trigger methods
of other class.

• Interfaces:- Interfaces are independent structures that you can implement in a class to
extend the scope of that class.

Instance and Static Components:

 Instance components exist separately in each instance (object) of the class and
are referred using instance component selector using ‘’.

Kalyan trainerkalyan@gmail.com
 Static components only exist once per class and are valid for all instances of the
class. They are declared with the CLASS- keywords

 Static components can be used without even creating an instance of the class and
are referred to using static component selector ‘ =>’ .

Visibility of Components

Each class component has a visibility. In ABAP Objects the whole class definition is
separated into three visibility sections: PUBLIC, PROTECTED, and PRIVATE.

• Data declared in public section can be accessed by the class itself, by its
subclasses 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 subclasses 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 subclasses and by external users outside the class.

CLASS <class> DEFINITION.


PUBLIC SECTION.
...
PROTECTED SECTION.
...
PRIVATE SECTION.
...
ENDCLASS.

Object References

To access an object from an ABAP program, you use object references. Object references
are pointers to objects. In ABAP, they are always contained in reference variables.

You define class references using the

... TYPE REF TO <class>

addition in the TYPES or DATA statement, where <class> refers to a class. A reference
variable with the type class reference is called a class reference variable, or class
reference for short.

A class reference <cref> allows a user to create an instance (object) of the corresponding
class, and to address a visible component <comp> within it using the form.

Kalyan trainerkalyan@gmail.com
Creating Objects

Before you can create an object for a class, you need to declare a reference variable with
reference to that class. Once you have declared a class reference variable <obj> for a
class <class>, you can create an object using the statement

CREATE OBJECT <cref>.


This statement creates an instance of the class <class>, and the reference variable <cref>
contains a reference to the object.

Addressing the Components of Objects

Programs can only access the instance components of an object using references in
reference variables. The syntax is as follows (where <ref> is a reference variable):

* To access an attribute <attr>: <ref>-><attr>


* To call a method <meth>: CALL METHOD <ref>-><meth>

You can access static components using the class name as well as the reference variable.
It is also possible to address the static components of a class before an object has been
created.

* Addressing a static attribute <attr>: <class>=><attr>


* Calling a static method <meth>: CALL METHOD <class>=><meth>

Within a class, you can use the self-reference ME to access the individual components:

* To access an attribute <attr> in the same class: ME-><attr>


* To call a method <meth> in the same class: CALL METHOD ME-><meth>

Self references allow an object to give other objects a reference to it. You can also access
attributes in methods from within an object even if they are obscured by local attributes
of the method.

Object Lifetime

An object exists for as long as it is being used in the program. An object is in use by a
program for as long as at least one reference points to it, or at least one method of the
object is registered as an event handler.

As soon as there are no more references to an object, and so long as none of its methods
are registered as event handlers, it is deleted by the automatic memory management
(garbage collection). The ID of the object then becomes free, and can be used by a new
object.

Kalyan trainerkalyan@gmail.com
Example on Visibility of Components

We shall see an example on Visibility of Components once we become familiar with


attributes of ABAP Objects.

Class Definition

Class Implementation

Object Creation

Step1 is Create a reference variable with reference to the class.

Syntax: DATA : <object name> TYPE REF TO <class name>.

Step 2 : Create an object from the reference variable:-

Syntax: CREATE OBJECT <object name> .

Output for the above code is

Kalyan trainerkalyan@gmail.com
Attributes of Object Oriented Programming:

• Inheritance.
• Abstraction.
• Encapsulation.
• Polymorphism

Inheritance is the concept of adopting the features from the parent and reusing them . It
involves passing the behavior of a class to another class. You can use an existing class to
derive a new class. Derived classes inherit the data and methods of the super class.
However, they can overwrite existing methods, and also add new ones.

Inheritance is of two types: Single Inheritance and Multiple Inheritance

Single Inheriting: Acquiring the properties from a single parent. (Children can be more).

Example for Single Inheritance

Multiple inheritance: Acquiring the properties from more than one parent.

Example

Tomato4 (Best Color, Size, Taste)

Tomato1

(Best color)

Tomato2

(Best Size)

Kalyan trainerkalyan@gmail.com
Tomato3

(Best Taste)

Syntax : CLASS <subclass> DEFINITION INHERITING FROM <superclass>.

Let us see a very simple example for creating subclass(child) from a superclass(parent)

Kalyan trainerkalyan@gmail.com
Multiple Inheritance is not supported by ABAP.

Output is as follows :

Kalyan trainerkalyan@gmail.com
Abstraction: Everything is visualized in terms of classes and objects.

Encapsulation The wrapping up of data and methods into a single unit (called class) is
known as Encapsulation. The data is not accessible to the outside world only those
methods, which are wrapped in the class, can access it.

Polymorphism: Methods of same name behave differently in different classes. Identical


(identically-named) methods behave differently in different classes. Object-oriented
programming contains constructions called interfaces. They enable you to address
methods with the same name in different objects. Although the form of address is always
the same, the implementation of the method is specific to a particular class.

Kalyan trainerkalyan@gmail.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy