Oobject Oriented Abap
Oobject Oriented Abap
Ramesh A
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.
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.
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++.
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.
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.
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.
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.
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.
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.
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.
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.
SPECIAL
NORMAL METHOD
METHOD(CONSTRUCTOR)
It can be declared in any of the sections Only in public section
A method have any type of parameters Instance constructor contain importing and
exception parameters and static constructor
cannot have any parameters
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 :
Types :
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
Events are Similar to actions. The main use of Events is to trigger the methods of the same
class or the other class
Types :
DEFINE A EVENT
DEFINE A METHOD
LINK EVENT AND METHOD
RAISE EVENT
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.
In Local Class, to define abstract class and abstract method, ABSTRACT keyword is mandatory
to incudes while defining it.
Submitted By
Ramesh A
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
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 :
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 :
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
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
Type of Casting:
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".
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".
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.
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.
CL_SALV_TABLE
Other class which can also be used to built OOALV reports in ABAP is,
CL_GUI_ALV_GRID
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
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.
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.