ABAP OOPS
1
Introduction to OOPS
▪ OOPS stands for ‘Object Oriented Programming System’.
▪ Object oriented programming is based upon bottom-up approach.
▪ One of the main point of focus in object-oriented programming is data protection.
2
Various Concepts of OOPS
▪ The various concepts of object-oriented programming are as follows:
Class
Object
Inheritance
Polymorphism
Encapsulation
3
Various Concepts of OOPS(Contd.)
Method Overloading
Method Overriding
4
Class
▪ Class is a collection of objects like - methods, attributes, events, interfaces etc.
5
Object
▪ Object is an instance of a class.
6
Inheritance
▪ The concept of inheritance allows us to derive new classes from existing classes.
▪ The existing class is called as base class or parent class or super class .
▪ The new class is called as derived class or child class or sub class.
Imp point : Multiple inheritance is not possible through classes, but we can achieve
multiple inheritance with the help of interfaces.
7
Polymorphism
▪ Polymorphism means multiple forms.
▪ It is a characteristic of being able to assign a different behavior or value in a subclass, to
something that was declared in a parent class.
▪ For example - a method can be declared in a parent class, but each subclass can have a
different implementation of that method.
8
Encapsulation
▪ Binding of data and functions in to a single unit(capsule form) is called as encapsulation.
9
Method Overloading
▪ Method overloading occurs within the class.
▪ In method overloading, methods must have the same name and different signatures.
Imp point : Method overloading is not possible in ABAP OOPS.
10
Method Overriding
▪ It is performed in two classes with inheritance relationship( superclass method and
subclass method).
▪ In method overriding, methods must have the same name and same signature.
11
Types of Class
▪ There are 2 types of class in ABAP OOPS.
1. Global class
2. Local class
12
Global Class
▪ Global classes are the reusable classes.
▪ The transaction code to create global class is SE24(class builder).
13
Types of Global Class
▪ There are 4 types of global class.
1. Usual ABAP class
2. Exception class
3. Persistence class
4. Unit test class
14
Usual ABAP Class
▪ The purpose of usual ABAP class is to write the logic.
▪ It is equal to function module of core ABAP.
15
Exception Class
▪ Exception is a problem that arises during execution of the program.
▪ The purpose of exception class is to raise and handle the exceptions.
▪ We use TRY block to raise a exception and CATCH block to handle a exception.
▪ The exception class name must start with CX.
▪ CX_ROOT is the global class(parent class) for all the exceptions.
16
Exception Class - Subclasses of CX_ROOT
▪ There are 3 subclasses of parent class CX_ROOT.
1. CX_STATIC_CHECK
2. CX_DYNAMIC_CHECK
3. CX_NO_CHECK
17
Exception Class - Subclasses of CX_ROOT
1. CX_STATIC_CHECK - It is checked by both the compiler and runtime system.
2. CX_DYNAMIC_CHECK - It is checked only at runtime system.
3. CX_NO_CHECK - It is not checked either by compiler or runtime system.
▪ Imp point : While creating the exception class, system by default provides
CX_STATIC_CHECK as the super class.
18
Creation of Exception Class
▪ There are 2 ways to create the exception class.
1. Exception class with messages of message class.
2. Exception class without messages of message class.
19
Persistence Class
▪ The purpose of persistence class is to perform database operations like insert, update,
delete.
▪ The persistence class name must start with CL.
▪ While creating the persistence class, 2 helper class creates automatically. Those helper
classes are CA and CB.
▪ CA is called as actor class or agent class.
▪ CB is called as base class.
20
Persistence Class - AGENT Attribute of the Agent Class
▪ The static attribute AGENT is a reference variable to agent class(CA).
▪ The first time this attribute is accessed in an ABAP program, the static constructor of
the agent class creates exactly one instance of this class, to which the attribute AGENT
then points.
▪ There is only one object of the agent class throughout a particular session.
21
Unit Test Class
▪ The purpose of unit test class is to write unit test cases.
▪ To identify a unit test class , We use the keyword FOR TESTING with the class definition.
22
Local Class
▪ Local classes are not reusable classes.
▪ Local classes are dedicated to one program only.
▪ The transaction code to create local class is SE38.
23
Syntax to Declare and Create an Object
▪ Declaration of object : DATA lo_object TYPE REF TO zcl_test.
▪ Creation of object : CREATE OBJECT lo_object.
▪ In the above syntax : DATA = keyword, lo_object = name of the object , TYPE REF TO =
keyword , zcl_test = name of the class , CREATE OBJECT = keyword.
24
Levels of Class Methods
▪ There are 2 levels of class methods.
1. Instance method
2. Static method
25
Instance Method
▪ We need to create an object to call the instance method.
▪ The keyword to declare instance method is : METHODS.
▪ The operator for instance method is : -> .
26
Static Method
▪ There is no need to create an object to call the static method.
▪ The keyword to declare static method is : CLASS-METHODS.
▪ The operator for static method is : => .
27
Visibility Control of Class Methods
▪ There are 3 types of visibility control for class methods.
1. Public
2. Private
3. Protected
28
Visibility Control of Class Methods
1. Public - Public method can be accessed with in the class, with in subclasses and outside
the class.
2. Private - Private method can only be accessed with in the class. It can not be accessed
with in subclasses and outside the classes.
3. Protected - Protected method can be accessed with in the class, with in subclasses, but
not outside the class.
29
Visibility Control of Class Methods(Contd.)
▪ The following table shows the visibility control of class methods.
30
Parameters of Class Methods
▪ There are 4 types of parameters for class methods.
1. Importing
2. Exporting
3. Changing
4. Returning
31
Parameters of Class Methods
1. Importing - Importing is the input parameters.
2. Exporting - Exporting is the output parameters.
3. Changing - Changing acts as input and output parameters.
4. Returning - A class method can have any number of exporting parameters but returning
is always 1.
32
Class Attributes
▪ Class attributes are data objects of any data type within a class.
▪ There are 2 types of class attributes.
1. Instance attribute
2. Static attribute
33
Instance Attribute
▪ Instance attribute exists separately for every instance of the class.
▪ Instance attribute are declared with the keyword DATA.
34
Static Attribute
▪ Static attribute exists only once for each class.
▪ Static attribute are defined with the keyword CLASS-DATA.
Imp point : Instance method can access both the instance and static attributes, whereas
static method can only access the static attributes.
35
Final Class
▪ A final class is that class which cannot be inherited.
▪ A final class cannot have sub-classes.
36
Abstract Class
▪ It is a special type of class which has at least one abstract method.
▪ Abstract method is that method which has only definition, there is no Implementation.
▪ We cannot create object of abstract class.
▪ We can create objects of sub classes of abstract class.
37
Interface
▪ In Interface all methods are public by default.
▪ In Interface all methods are abstract methods - there is only definition, no
implementation.
38
Abstract Class and Interface Comparison
▪ In abstract class, we have at least one abstract method, others can be non-abstract
methods whereas in interface all methods are abstract methods.
▪ In abstract class , method visibility are public, private and protected whereas in
interface all methods are public.
39
Abstract Class and Interface Comparison(Contd.)
▪ In abstract class, we need to redefine the abstract method in to sub classes so that we
can write the logic into that method whereas in interface there is no need to click on
the redefine button( redefine button is disabled),we can write the logic by double
clicking on the class method.
▪ In abstract class, multiple inheritance is not possible whereas multiple inheritance is
possible with interfaces.
Imp point : We can not redefine a static method.
40
Abstract Class and Interface Comparison(Contd.)
▪ The following table shows the difference between abstract class and interface.
41
Events in Object Oriented Programming
▪ With the help of events, method of one class can call method of another class.
▪ Triggering method - This method is used to raise the event.
▪ Event handler method - This method is used to handle the event.
▪ Register the event handler method using SET HANDLER statement.
42
Syntax to Register Event Handler Method
▪ Syntax : SET HANDLER lo_object1->event_handler FOR lo_object2.
▪ In the above syntax : SET HANDLER = keyword , lo_object1 = Class object in which event
handler method is defined, event_handler = event handler method name , FOR =
keyword, lo_object2 = Class object in which triggering method is defined.
43
Significance of ME
▪ A method can have a variable defined within it which has the same name as one of the
attributes of the class.
▪ To clearly identify the class level attribute, the selector ME is used.
44
Constructors
▪ Constructor is a special type of method which is executed automatically when an object
is created.
▪ They cannot be called using call method statement.
▪ Constructors are methods with a predefined name.
▪ Multiple instance and static constructor are not allowed with in a class.
45
Types of Constructors
▪ There are 2 types of constructors.
1. Instance constructor
2. Static constructor
46
Instance Constructor
▪ Name of the method: CONSTRUCTOR.
▪ It has only importing parameters.
▪ It can access both static and instance attributes.
▪ It is called every time when an object is created.
▪ Used to set default value for a particular instance.
47
Static Constructor
▪ Name of the method: CLASS_CONSTRUCTOR.
▪ It does not have any parameters.
▪ It can access only static attributes.
▪ It is called only first time when an object is created.
48
ALV by CL_GUI_ALV_GRID Class
▪ The various steps to create a ALV by CL_GUI_ALV_GRID class are as follows:
Create a field catalog.
Create a custom control (layout element) on a screen.
Create an object of the container class (CL_GUI_CUSTOM_CONTAINER).
Create an object of the alv grid class (CL_GUI_ALV_GRID) and pass the object of
container class in the parent parameter.
Use alv grid class method (SET_TABLE_FOR_FIRST_DISPLAY) to display the data.
49
ALV Object Model - CL_SALV_TABLE Class
▪ ALV object model is an encapsulation of the pre-existing ALV tools.
▪ CL_SALV_TABLE class is a part of ALV object model.
▪ CL_SALV_TABLE combines both the CL_GUI_ALV_GRID class for container
implementation, as well as the REUSE_ALV_GRID_DISPLAY and
REUSE_ALV_LIST_DISPLAY function modules for full screen display.
Advantage of ALV object model - It provides a collective interface to ALV tools.
Disadvantage of ALV object model - It does not support editable ALV.
50
Steps of ALV Creation By CL_SALV_TABLE Class
▪ The various steps for creating a ALV by CL_SALV_TABLE class are as follows:
1. Call the FACTORY Method of CL_SALV_TABLE class to get the instance of ALV table
object.
2. Call the DISPLAY Method of CL_SALV_TABLE class to display the ALV.
51
Non-Event Based Functionality in ALV OOPS
▪ Sorting
▪ Filtering
▪ Coloring etc.
52
Event Based Functionality in ALV OOPS
▪ Double clicking
▪ Hotspot clicking etc.
53
Concept of Alias
▪ Alias is a shortcut or alternative name which is used to increases the readability of the
code.
▪ In ABAP OOPS , Alias is a component of the class and the interface.
▪ Use : Alias is widely used when we implement interface methods in the class. At that
time, we define an Alias for those interface methods and call those methods using that
defined Alias.
▪ Syntax : ALIASES <alias name> FOR <interface name>~<component name> .
54
Concept of Types
▪ Types is a component of the class and the interface.
▪ Types allows us to define and manage the custom data types like structure type, table
type etc.
▪ It enhances the readability and reusability of the code.
55
Singleton Class - Introduction
▪ Singleton is basically a single object/instance creation.
▪ Whenever we want to create an object/instance of the class, we create the object
through CREATE OBJECT or NEW statement and we can create any number of
instances/objects.
▪ In many cases, we need to have only one instance of the class. In that case, we use
singleton class.
▪ Singleton class is a class which ensures that class has only one instance/object.
56
Singleton Class - Introduction(Contd.)
▪ We can not create the instance/object outside the class.
▪ We write the logic for instance/object creation in the class itself.
▪ It means singleton class itself controls the creation of instance/object.
57
Steps to implement a Singleton Class
▪ The various steps to implement a singleton class are as follows:
Instance generation : We need to specify the instance generation of the class as
PRIVATE. It prevents the external instance/object generation of the class.
Declare static variable : A static variable is declared with in the class to hold the single
instance. It is usually TYPE REF TO class itself.
58
Steps to Implement a Singleton Class(Contd.)
Static method to return the single instance : A static method is created and
implemented which is used to return the single instance. For best understanding, It is
good to provide the method name as GET_INSTANCE.
59
Agent Class as Singleton Class
Agent class is one of the best example of singleton class as all database operations go
through a single instance.
60
Singleton Class - CL_SALV_TABLE
CL_SALV_TABLE class is not a singleton class as we can go for multiple
instances/objects.
61
Casting
▪ In ABAP OOPS, assigning the instance of parent class to child class or assigning the
instance of child class to parent class is called as casting.
▪ There are 2 types of casting.
1. Narrow casting( Up casting)
2. Wide casting( Down casting)
62
Narrow Casting/Up Casting
▪ In narrow casting, the child class instance is
assigned to the instance of parent class.
▪ As we are moving from more specific view
to less specific view so it is called as narrow casting.
▪ As the path goes upwards from child class to parent class
so, it is also called as up casting.
63
Narrow Casting/Up Casting(Contd.)
▪ The assignment operator = is used for narrow casting.
▪ Example - lo_parent = lo_child.
lo_parent = parent class instance, lo_child = child class instance
64
Wide Casting/Down Casting
▪ In wide casting, the parent class instance is
assigned to the instance of child class.
▪ As we are moving from less specific view
to more specific view, so it is called as wide casting.
▪ As the path goes downwards from parent class to child class,
so, it is also called as down casting.
65
Wide Casting/Down Casting(Contd.)
▪ The conversion operator ?= is used for wide casting.
▪ Example - lo_child ?= lo_parent.
lo_child = child class instance , lo_parent = parent class instance.
66
Thank You
67