0% found this document useful (0 votes)
19 views17 pages

Oobject Oriented Abap

Uploaded by

rraj10922
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)
19 views17 pages

Oobject Oriented Abap

Uploaded by

rraj10922
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/ 17

Submitted By

Ramesh A

OOBJECT ORIENTED ABAP


What is OOPS ?

Procedural Programming can be defined as a programming model which is derived


from structured programming, based upon the concept of calling procedure. Procedures, also
known as routines, subroutines or functions, simply consist of a series of computational steps to
be carried out. During a program’s execution, any given procedure might be called at any point,
including by other procedures or itself.

Object-oriented programming (OOP) is a computer programming model that organizes


software design around data, or objects, rather than functions and logic. An object can be defined
as a data field that has unique attributes and behavior.

Object orientation simplifies software design to make it easier to understand, maintain, and
reuse. This approach to programming is well-suited for programs that are large, complex and
actively updated or maintained. Due to this advantage, everyone is opting for Object oriented
programming.

Let’s compare the procedural and object oriented programming –

Features Procedure Oriented approach Object Oriented approach

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


tasks.

Modularization Programs can be divided into smaller Programs are organized into classes and
programs known as functions. objects and the functionalities are
embedded into methods of a class.

Data security Most of the functions share global Data can be hidden and can’t be accessed
data. by external sources.

Extensibility This is more time consuming to New data and functions can be added
modify and extend the existing effortlessly as and when required.
functionality.

Approach top-down approach. bottom-up approach.

Access There is no access specifier in There is no access specifier in procedural


Specifiers procedural programming. programming.
Submitted By
Ramesh A

OOPS Concept in ABAP :

ABAP was initially developed as a procedural language (just similar to earlier procedural
programming language like COBOL). But ABAP has now adapted the principles of object oriented
paradigms with the introduction of ABAP Objects. The object-oriented concepts in ABAP such as
class, object, inheritance, and polymorphism, are essentially the same as those of other modern
object-oriented languages such as Java or C++.

Advantages of OOPS Concept in ABAP:

 In OOABAP, programs are divided into objects leading to better and powerful data
management.
 Provide properties like data hiding (encapsulation) & code reusability (inheritance) with
more data security.
 Better performance with less consumption of time.
 Helps in future orientation.
 Simple and it much easier to maintain as compare to procedural ABAP programming.
 Relatively flexible & adaptable to changing business needs.

CLASS and OBJECTS

The concept of classes and objects is the foundation of object-oriented programming. The
real world is made up of objects, for example building, car, and human and so on. Placing all the
characteristics and behaviors of an object type into a class is knows a defining the class. Thus when
defining a car class, we place all the common characteristics of a car into the car class, for example
a car will have wheels, a car will have an engine, a car will have seats, etc., etc. Using this car
class, individual car objects can be derived.

OBJECTS :

Objects are runtime instances of a class. Several runtime instances can be created based on
a single class. Each instance (object) of a class has a unique identity and its own set of values for
its attributes.
Submitted By
Ramesh A

CLASS :
Class forms the blueprint or template for objects. A class encapsulates data (also known
as attributes) and services (also known as methods) into a single unit.

Class contains Attributes and Methods.

Types

 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.
Class will have two sections :
 Class Definition : This section is used to declare the components of the classes
such as attributes, methods, events.
CLASS <Class Name> DEFINITION.
... ( Attributes & Methods )
ENDCLASS.
 Class Implementation : This section of a class contains the implementation of
all methods of the class.
CLASS <Class Name> IMPLEMENTATION.
METHOD <Method Name>.

Statement...……….
ENDMETHOD.
ENDCLASS.

 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.
Submitted By
Ramesh A

ACCESS SPECIFIER :
Each class components has a visibility which is known as access specifier. In ABAP
Objects the whole class definition is separated into three visibility sections:

PUBLIC : Data declared in public section can be accessed by the class itself, by its
subclasses as well as by other users outside the class.

PROTECTED : 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.

PRIVATE : 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.

Arrangement of access specifiers should be as follow,

CLASS <Class Name> DEFINITION.


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

ATTRIBUTES :

These are nothing but a Variables declared in class as like in functions. There are three
types of attributes available in class.

Types :

 Instance Attributes :

These are object specific, declared by using DATA keyword and it can be only be
accessed by object name.

Instance attributes are accessed/ called by using the Below syntax

<Instance/Object name>->Instance component>


Submitted By
Ramesh A

 Static Attributes

These are not instance specific, declared by using keyword CLASS-DATA. Static
attributes can be accessed by class name as well as by instances of the class. It is not
mandatory to have an object to access static attributes.

Static objects are accessed by the below syntax.


<Class Name>=><Static component>

 Constant Attributes

Constants are similar to attributes and holds a constant value throughout the
program runtime. Constants are declared using CONSTANTS statement. The value set
to a constant cannot be changed.

METHODS :

It is a Block of code, providing some functionality offered by the class. Can be compared
to function modules. They can access all of the attributes of a class. Methods are defined in the
definition part of a class and implement it in the implementation part.

Every method defined in class definition should be implemented, at least it should be


implemented Null.

Types :
 Instance Method :
It can be called only by object of class, and declared by using METHODS keyword.
Instance method can access instance attributes and as well as static attributes.

Class Definition part declaration of Instance Methods is


METHODS: <Method name> [parameter].
Class Implementation part declaration of Instance Methods is
METHOD <Method Name>.
Statement...……….
ENDMETHOD.
To Call Method
CALL METHOD <obj1>->Method name.
Submitted By
Ramesh A

 Static Method :
It can be called by both object of class as well as class name. You don’t need to
have an instance of the class to call a static method. It is declared using CLASS-
METHOD keyword. Static methods cannot access Instance attributes but can only
access static attributes of class.

Class Definition part declaration of Instance Methods is


CLASS-METHODS: <Method name> [parameter].
Class Implementation part declaration of Instance Methods is
METHOD <Method Name>.
Statement...……….
ENDMETHOD.
To Call Method
CALL METHOD <Class Name>->Method name.

SPECIAL
NORMAL METHOD
METHOD(CONSTRUCTOR)
It can be declared in any of the sections Only in public section

It has to called explicitly It called implicitly

A method have any type of parameters Instance constructor contain importing and
exception parameters and static constructor
cannot have any parameters

Methods can return values It cannot return values.

It can be called any no. of times in the Instance constructor will be called only once
lifetime of an object. in the lifetime of object whereas static
constructor will be called only once in
lifetime of class.
Submitted By
Ramesh A

 Special methods :

A CONSTRUCTOR is considered as a special method as it cannot be called


explicitly by using the CALL METHOD statement. These methods are mainly used to
set default values in a class. I.e. used to default values to attributes. We have two kind
of special methods. These are declared only in public section. These methods cannot
be called explicitly

Types :

 Instance constructor - defined using name CONSTRUCTOR.


 Constructor method is executed significantly whenever an object is
created.
 These methods are mainly used to set default values in a class.
 These methods have only importing parameters.
 There are no exporting parameters.

 Static constructor - defined using name CLASS_CONSTRUCTOR.

 This method is executed naturally whenever a first call to the class is


made.
 The first call can be through instance or class name.
 Static constructor are used to set the default values globally .i.e.,
Irrespective of instances.
 The static constructors will not have any importing / exporting
parameters.
 These are executed only once.
Submitted By
Ramesh A

INTERFACES :

Similar to classes in ABAP, interfaces act as data types for objects. The components of
interfaces are same as the components of classes. Unlike the declaration of classes, the declaration
of an interface does not include the visibility sections. This is because the components defined in
the declaration of an interface are always integrated in the public visibility section of the classes.

Interfaces are used when two similar classes have a method with the same name, but the
functionalities are different from each other. Interfaces might appear similar to classes, but the
functions defined in an interface are implemented in a class to extend the scope of that class.
Interfaces along with the inheritance feature provide a base for polymorphism. This is because a
method defined in an interface can behave differently in different classes.

INTERFACE <Interface_Name>.

DATA.....

CLASS-DATA.....

METHODS.....

CLASS-METHODS.....

ENDINTERFACE.

All the methods of an interface are abstract. They are fully declared including their
parameter interface, but not implemented in the interface. All the classes that want to use an
interface must implement all the methods of the interface. Otherwise, the class becomes an abstract
class.

The following syntax is used to implement the methods of an interface inside the
implementation of a class −

METHOD <Interface_Name~Method_Name>.

<statements>.

ENDMETHOD.
Submitted By
Ramesh A

ALIASES :
An alias is an alternate name for interface methods that are implemented in the class.
Whenever we implement the interface in a class, the interface method is copied with naming
convention <Interface-Name>~<Method-Name>. Instead of using the default interface method
name in the class, we can provide an alternate name to it (called alias) and can use alias name in
the program.

Syntax :
ALIASES <alias-name> FOR <interface-method>.

EVENTS

Event is an action or occurrence of something that is recognized by the application.


Handling events means response by the application to the action or that occurrence. As an example,
when ALV is displayed and user clicks on something, event is triggered by the user’s action and
the event handler simply responds to the user action i.e. handles the event of the user action.

Events are Similar to actions. The main use of Events is to trigger the methods of the same
class or the other class

Types :

Instance Event : EVENTS key word is used

Static Event : CLASS-EVENTS key word is used

Syntax to Raise the Event

We have to follow 5 steps when working with Events.

 DEFINE A EVENT

EVENTS <Event Name> EXPORTING VALUE (Parameters) TYPE string.


Submitted By
Ramesh A

 DEFINE A METHOD
 LINK EVENT AND METHOD

METHODS <Method Name> FOR EVENT <Event Name> OF <Class Name>


IMPORTING Parameters.

 RAISE EVENT

RAISE EVENT <Event Name> EXPORTING Parameters = 'value'.

 USE SETHANDLER

SET HANDLER <New Object Name > -> <Method Name> FOR <Existing
Object Name >.

ABSTRACT CLASS

Class with at least one abstract method (which does have implementation) is known as
‘Abstract class’.

A class can be called as an abstract class, if it would contain at least one abstract method.
An abstract method is a method which does not have an implementation. We can implement that
abstract method, but not like any other simple class. There is a particular way to implement it. To
implement an abstract method, a subclass of an abstract class is required. There is no need to
instantiate an object of an abstract class. Instantiation is possible only for the subclass. An abstract
class can have non-abstract methods as well and it is not necessary to redefine non-abstract
methods in each and every inherited class.

Following points, we need to remember while creating an abstract method:

 Abstract methods can never be private.


 Only instance methods are allowed to be an abstract method.

In Local Class, to define abstract class and abstract method, ABSTRACT keyword is mandatory
to incudes while defining it.
Submitted By
Ramesh A

ABSTRACT CLASSES INTERFACES

Can contain both abstract and non-abstract Can contain only abstract methods.

methods

Explicitly we need to use abstract keyword By default all methods are abstract

Abstract methods can be declared in public or By default all components of interfaces are

protected section. public.

A class can inherit only one abstract class A class can implement any know of interfaces

Abstract class components are directly Interface components must prefix with the
referred in subclass. name of interface.

FRIEND CLASS :

In any Object Oriented programming language, the access to private or protected


components that is both methods and attributes, would be prohibited. If someone try to access
them, compiler would generate syntax error. Sometimes, it would be advantageous to give the
access to these protected and private attributes to other classes. This can be achieved using the
FRIENDS Class.

By using FRIEND Class we have able to access the private and protected components
mentioned in class definition.

FEATURES OF OOABAP :

 Encapsulation

 Abstraction

 Inheritance

 Polymorphism
Submitted By
Ramesh A

ENCAPSULATION
Encapsulation is a key concept of object programming that ensures the autonomy and
integrity of the objects. Restricts the visibility of object resources (attributes & data) and methods
(services)) to other users. In object-oriented, both data and services are encapsulated and not visible
outside the object itself. The methods or services work like an object interface through which other
objects can interact with it.

ABSTRACTION
Through the process of abstraction, a programmer hides all but the relevant data about an
object in order to reduce complexity and increase efficiency. In the same way that abstraction
sometimes works in art, the object that remains is a representation of the original, with unwanted
detail omitted. The resulting object itself can be referred to as an abstraction, meaning a named
entity made up of selected attributes and behavior specific to a particular usage of the originating
entity.

INHERITENCE
Inheritance is the concept of deriving methods and attributes from Super class (also known
as Parent class) to sub class (also known as child or derived class). Derived class not only inherit
the data and methods but also it can add own data and methods. . However, they can overwrite
existing methods, and also add new ones.
The keyword to use for inheritance is ‘INHERITING FROM’, just beside the class definition
Syntax :
CLASS <subclass_name> DEFINITION INHERITING FROM <superclass_name>

Types :
 Single Inheritance : sub class is derived from only one super class.
 Multiple Inheritance : sub class is derived from more than one super class.
This inheritance is not applicable in ABAP. This can be
achieved by INTERFACES technique.
 Multilevel Inheritance : sub class is acting as a super class for another sub class.
Submitted By
Ramesh A

Redefinition:

The methods in the super class can redefine in subclass. The redefined methods can have
different implementation is subclass from base class.

Super Keyword :

It is a keyword which is used to access the super class method

Syntax: Super-><Parent class method>

ME Variable :

It just like a self-reference, by this we can call methods that are within same class without
creating object.

FINAL keyword :
It is a keyword which will indicate whether we create inheritance or not for the Parent
class.

POLYMORPHISM

In object-oriented programming, polymorphism (from the Greek meaning "having multiple


forms") is the characteristic of being able to assign a different meaning to a particular symbol or
"operator" in different contexts. The simple example is two classes that inherit from a common
parent and implement the same virtual method.
Polymorphism is a concept by which the same method names will behave differently in
different classes i.e each method will have it's own implementation in different classes but with
the same name.
Polymorphism is achieved by redefining methods during inheritance or by implementing
interface methods. Polymorphism supports generic programming.

 Method Overriding :
Method overriding allows a subclass to override a specific implementation of a
method that is already provided by one of its super classes.
Submitted By
Ramesh A

A subclass can give its own definition of methods but need to have the same
signature as the method in its super class. This means that when overriding a method the
subclass's method has to have the same name and parameter list as the super class's
overridden method.

 Method Overloading :
Method overloading is in a class have many methods having same name but
different parameter called overloading or static polymorphism

CASTING

Customer specific changes can be easily implemented using casting. Suppose


there are two customers, customer1 and customer2. They have some common properties and
some individual customer specific properties. So we can maintain their common properties in
class general(parent) and individual properties in class customer1(child) and customer2(child).
Casting help as us to access these child properties using parent. ie, we don't need to make
changes in our programs whenever the individuals customer properties changes.

Type of Casting:

Narrowing Cast( Upcasting):

When we assign the instance of the Sub class back to the instance of the Super
class, than it is called the "Narrow Casting", because we are switching from a "More
Specific view of an object" to "less specific view".

super class = sub class

Widening Cast(Downcasting):

When we assign the instance of the Super class to the Subclass, than it is called
the Widening Cast, because we are moving to the "More Specific View" from the "Less
specific view".

sub class ?= super class


Submitted By
Ramesh A

SINGLETON CLASS

A class is said to be a singleton class if it can have the utmost one instance only.

Sometimes there is a need to instantiate an object at a time, that means only one instance
is required at one point of time. This is a very common requirement while designing an
application. To achieve such a scenario, there is one concept of ‘Singleton Pattern’ or ‘Singleton
Class’ in OOABAP.

Following is a procedure to create a singleton class :

 Visibility of class should be private.


 Declare a private attribute with reference to the same class, in which it is being declared.
 Create a public static method with returning parameter, having a reference of the same
class.
 Create an implementation and an object inside the implementation of a public static
method.
 To create an instance for the singleton class, call this public static method in any program.
Submitted By
Ramesh A

OOALV

The OO ALV is ALV using object-oriented ABAP. ALV stands for ABAP List Viewer
and provides the standard list format and user interface for all the ABAP programs. The advantage
of using OOPS ALV in SAP ABAP is that is flexible and used to build interactive and modern-
designing list. SAP has provided a class (class name: CL_GUI_ALV_GRID) which acts as the
wrapper class encapsulating ALV GRID functionality.

Object Oriented ALV’s can be built using two standard classes.

 FACTORY CLASS is easy to use to built ALV reports in ABAP.


This ALV can be generated by using the class,

CL_SALV_TABLE

 Other class which can also be used to built OOALV reports in ABAP is,

CL_GUI_ALV_GRID

The basic components for ALV Grid are:

 List data: List data are the internal table data’s to be listed.
 Field Catalog: The field catalog is the internal table that defines the specification on the
display of fields. To generate field catalog there are three methods: Automatic generation,
semi-automatic generation and manual generation. The internal table must be referred to
LVC_T_FCAT.
 Layout Structure: The layout structure must be of type “LVC_S_LAYO”. This can be used
to modify the layout including colour adjustments, grid customization etc.
 Event Handler: To handle events triggered by ALV, event handler class should be defined
and implemented.
 Additional Data: To stimulate additional data in ALV Grid, additional data should be
passed as a parameter.
Submitted By
Ramesh A

Types of Containers used in OOALV are,

 Custom Container
In this container, we can create a fixed container where ALV report will be
generated.
Accessed by using the class : CL_GUI_CUSTOM_CNTAINER

 Docking Container

In this container, we can create a adjust the container where ALV report will be
generated after generating report on output screen in any direction which we configure
while generating.

Accessed by using the class : CL_GUI_DOCKING_CNTAINER

 Splitter Container

In this container, we can create a multiple container where ALV report can be
displayed according to the configuration done while creating the container. We can fix or
can adjust the after screen a generating report on output screen in any direction according
to the configuration done while creating the container.

Accessed by using the class : CL_GUI_SPLITTER_CNTAINER

 Dialog box Container

By using this container, we can ale to generate ALV report in dialogbox.

You might also like