Sap Abap Oops: What Is OOP?

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 11

SAP ABAP OOPS

What is OOP?

Object Oriented programming breaks down tasks into objects that have data and function.This method
makes software programming understandable, flexible and reusable.Object-Oriented Programing (OOP)
was created with simplicity .It makes people communicate clearly, exchange information and ideas using
a common language .These objects are made up of their two main characteristics – properties (data) and
behaviour (or functions).

The main characteristics of OOP are –

· Data abstraction

· Encapsulation

· Emphasis on data rather than procedure

· Usage of real-world entities called ‘object’

· Data privacy and protection

POP vs OOP

POP

· POP stands for Procedure-Oriented programming.

· It focuses on procedure (doing tasks) rather than data (tasks)..

· POP follows top-down fashion in problem-solving.

· It divides a program into smaller parts called functions or modules

· Emphasis:Emphasis is on tasks.

· Modularization:Programs can be divided into smaller programs known as functions.

· Data security:Most of the functions share global data.

· Extensibility:This is more time consuming to modify and extend the existing functionality.

· Functions share global data and data cannot be hidden from each other

· There is not much emphasis on data protection.

· Extending functionality and addition of data is tedious and time-consuming.

· Concepts of inheritance, polymorphism, encapsulation are not used.

OOP
· OOP stands for Object-Oriented programming

· It focuses on data/tasks rather than procedure

· OOP follows bottom-up fashion in problem-solving

· Emphasis:Emphasis is on things that does those tasks.

· Modularization:Programs are organized into classes and objects and the functionalities are
embedded into methods of a class.

· Data security:Data can be hidden and can’t be accessed by external sources.

· Extensibility:New data and functions can be added effortlessly as and when required.

· It divides a program into smaller parts called objects

· Functions do not share global data – you can decide who can access your data

· One of the main points of focus in OOP is data protection.

· It is very easy to extend functionality and add new data, by simply adding new objects.

· Concepts of inheritance, polymorphism, encapsulation form the very basis of OOP.

OOP in ABAP

· Initially, SAP developed ABAP as a POP language.

· However, as time progressed, they shifted it and transformed it into an OOP language Hence,
ABAP now follows the tenets of object orientation and paradigms like objects.

· As part of OOP, ABAP uses the concepts of inheritance, overloading, data hiding, etc. for better
functionality.

TERM MEANING

Object a real-world entity that has data and behaviour

Class a complex collection of objects that mainly contain data fields and

characteristics pertaining to the data

Method behaviour of an object i.e. function modules that the object performs

Member either the data member or member function of a class

Instance synonym of object, usually used as ‘instance of a class’

Instantiation creating a new object of a class


Objects in SAP ABAP

· An object is a real-world entity that contains data and behaviour.

· These characteristics and behaviour define the state of the object, and the actions that the
object will perform.

· An object, as mentioned previously, is a blueprint or instance of a class.

· For e.g. an employee, a book, an apple tree – all are objects.

Objects can interact with one another by sending messages. Objects contain data and code to
manipulate the data. An object can also be used as a user-defined data type with the help of a class.
Objects are also called variables of the type class. After defining a class, you can create any number of
objects belonging to that class. Each object is associated with the data of the type class with which it has
been created.

Creating an Object

· Creating a reference variable with reference to the class. The syntax for which is −

DATA: <object_name> TYPE REF TO <class_name>.

· Creating an object from the reference variable. The syntax for which is −

CREATE Object: <object_name>.

Report Name : ZR_SS_DATAFLAIR_SAMPLE_001


Output:

Classes in SAP ABAP


· A class is a complex collection of objects

· It contains data members (characteristics) and member methods (functions) together


wrapped into a single entity called object

· Classes are where objects are created and defined

· We also define and declare function modules in a class

· We can describe the privacy of a class

· What this means is, we can define who all can have access to particular data of a class

Creating a Class in ABAP


Creating a class in ABAP has two steps –

1. Declaration

Syntax –

CLASS <class_name> DEFINITION.

//define class

ENDCLASS.

Example –

CLASS dataflairclass DEFINITION.

//define class

ENDCLASS.

2. Definition

Syntax –

CLASS <class_name> IMPLEMENTATION.

//define class

ENDCLASS.

Example –

CLASS dataflairclass IMPLEMENTATION.

//define class

ENDCLASS.

A class definition starts with the keyword CLASS followed by the class name, DEFINITION and the
class body. The definition of a class can contain various components of the class such as attributes,
methods, and events. When we declare a method in the class declaration, the method
implementation must be included in the class implementation.

Attributes in SAP ABAP


· Attributes are the characteristics of the class

· They can have any pre-defined data type like P, F, I ,C, N etc.

· We must declare attributes in class declaration section

Attributes are of two types:

Instance: this includes the instance-specific state of object, the changes are different for all objects,
and this is declared by DATA statement
Static: this includes common state of the class to be shared by all instances of class, hence when we
change one state then all other objects can see the change, declared by CLASS-DATA statement

Methods in SAP ABAP

· A method is a module or a function

· It is the behaviour of the object of a class

· A method can access any characteristic of a class

· It contains actions to be performed by objects that call upon the class

· Method definition may or may not contain parameters, which are passed when the method
is called

· We can define a method using METHOD and ENDMETHOD statements

Accessing Attributes and Methods in SAP ABAP

1. Methods and attributes can be accessed by objects of the class

2. The access specifiers – public, private and protected – work the same way as they do for class
access specifiers

· If we declare them public, they can be accessed by any class

· When we declare them private, they cannot be accessed by any class

· If we declare them protected, they can only be accessed by inheriting classes

Report name: ZR_SS_DATAFLAIR_SAMPLE_002


Output

Static Attributes

A Static attribute is declared with the statement CLASS-DATA. All the objects or instances can use
the static attribute of the class. Static attributes are accessed directly with the help of class name
like class_name⇒name_1 = 'Some Text'.

Report name : ZR_SS_DATAFLAIR_SAMPLE_003.


Output

Constructors in SAP ABAP

· Constructors are special types of methods


· When we create an object, we need to initialise it and constructors do the job of initialising
data for the objects

· The program calls constructors automatically when an object of the class is created

· The constructor will trigger the operation included whenever an object is newly created in
execution

Report Name:ZR_SS_DATAFLAIR_SAMPLE_004

Output

ME Operator in SAP ABAP

· Whenever we redefine a variable within a method with a different value, that method will
use the redefined value of the variable
· However, if you want to access the originally declared value, then you can use the ME
operator

Report Name:ZR_SS_DATAFLAIR_SAMPLE_005

Output

You might also like