Oops Abap Notes - L
Oops Abap Notes - L
*&---------------------------------------------------------------------*
*& Report ZOOPS_CLASS1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOPS_CLASS1.
**********************************Creation of class
PUBLIC SECTION.
data a type i. "Instance attribute
class-data b type i. "Static attribute
ENDCLASS.
ob->a = 10.
* lcl_abc=>a = 10. we cannot use this
* ob->b = 20.
lcl_abc=>b = 20.
* WRITE a. We cannot use
WRITE ob->a.
WRITE ob->b.
uline.
data ob1 type REF TO lcl_abc.
create OBJECT ob1.
ob1->a = 10.
WRITE : ob1->a,
ob1->b.
1. Instance constructor.
2. Static constructor.
Instance constructor
Static constructor
Note:
If a class contains static and instance Constructor and if we
instantiate first object before accessing any static components,
than SAP first executes Static Constructor and then the Instance
Constructor will be executed on behalf of that first object, from
the second object onwards only instance constructor will be
executed.
If an instance constructor contains any mandatory importing
parameters, we must pass the values to those parameters while
creating objects itself.
INHERITANCE
It is a process of using the properties (components) of one class
inside other class.
The main aim of Inheritance is Reusability of the components i.e.
a component declared in one class is accessible inside other class.
In Inheritance, two classes are involved (super class/base class
and sub class/derived class).
The class which gives properties is called as super class / base
class.
The class which takes the properties is called as sub-class/derived
class.
Only Public and Protected components can be inherited i.e.
Private Components cannot be inherited.
Types of inheritance:
1. SINGLE INHERITANCE: A Class acquiring properties from only one
super class.
2. MULTIPLE INHERITANCE: A class acquiring properties from more
than one entity (interface).
Note: In ABAP, Multiple inheritance can be implemented using the
combination of class and interfaces.
3. MULTILEVEL INHERITANCE: A class acquiring the properties from
another sub-class.
Inheriting from – is a keyword used as part of local class definition
for inheriting another class.
A class declared as Final cannot be inherited i.e. it cannot have
subclass. In case of local classes, we use the keyword ‘final’ as
part of class definition to declare a final class.
Polymorphism
Poly -> many,
Morph-> forms,
Ism-> behaviour
It is a process of making an entity behaving in multiple forms.
In this case, the entity is a method
Example:
Method overloading, Method Overriding.
Method Overloading:
It is similar to function overloading in C++.
It is a process of overloading the same method by passing
different Number and different types of parameters.
Eg: methods m1.
Methods m1 importing x type i.
Methods m1 importing x type c.
Methods m1 importing x type I y type i.
ABAP does not support method overloading.
Method Overriding:
- If a subclass redefines a super class method it is called as Method
Overriding.
Public classes:
- The default visibility at class level is public.
- The sub class inheriting the super public class can be created as
explicit protected/private class.
Protected classes:
- Private classes:
- Create private is the keyword used for creating the private classes.
Friend keyword:
ABSTRACT CLASS:
Abstract is a class which contains one or more Abstract methods.
- An abstract method is a method which is just declared but not
implemented.
- We cannot create the objects for abstract classes because they are
not implemented completely. But once abstract methods are
implemented inside subclass, we can instantiate the subclass and
assign subclass object to abstract super class reference which is
called as narrow casting.
INTERFACES
- An interface is a pure abstract class i.e. by default all the methods
of interface are abstract.
Aliases
Aliases are the alternative names provided for interface
components .i.e. whenever we refer to the interface components
outside the interface, it must be prefixed with the name of the
interface. This lengthy naming representation can be avoided by
declaring “aliases” for interface components.
- Aliases can be declared in any of the visibility sections inside
implementation class
- Aliases declared inside interfaces are always public
- Aliases can be declared either in interfaces / in the
implementation class.
- If the aliases as to be declared in the interface it can be declared
only for included interfaces components and that alias will be
created as component of interface.
Note: The included interfaces can be referred directly in the
implementation class .i.e. the interface which is included in other
interface can be directly referred in the implementation class.
Note: As Part of global classes, If a global class Includes any
interface in the interfaces tab and the checkbox ‘final’ is selected,
it indicates that these interface methods cannot be further
implemented (re-defined) in other classes.
INTERRFACE:
Example: Local Interfaces
REPORT ZCLASS8_INTERFACE.
interface rectangle.
constants : length type i value 10,
breadth type i value 5.
methods : area,
perimeter.
endinterface.
interface square.
constants side type i value 10.
methods : area,
perimeter.
endinterface.
class lcl_impl DEFINITIOn. "implementation class
PUBLIC SECTION.
interfaces : rectangle,
square.
PROTECTED SECTION.
data res type i.
endclass.
method rectangle~area.
res = rectangle~length * rectangle~breadth.
write :/ 'Area of rectangle is ',res.
endmethod.
method rectangle~perimeter.
res = 2 * ( rectangle~length * rectangle~breadth ).
write :/ 'Perimeter of rectangle is ',res.
endmethod.
method square~area.
res = square~side * square~side.
write :/ 'Area of square is ',res.
endmethod.
method square~perimeter.
res = 4 * square~side.
write :/ 'Perimeter of squareis ',res.
endmethod.
endclass.
START-OF-SELECTION.
data r type ref to rectangle.
*create object r. "syntax error
uline.
data s type ref to square.
*create object s. "syntax error
uline.
write :/ 'Implementation class object ob --> assigned to interface square
reference s.'.
s = ob. "narrow casting
call method : s->area,
s->perimeter.
Example: Implementing interfaces partially
REPORT Z1030OOPS36.
interface intf1.
methods : m1,
m2,
m3.
endinterface.
method intf1~m2.
write :/ 'Inside method m2...'.
endmethod.
method intf1~m3.
write :/ 'Inside method m3...'.
endmethod.
endclass.
START-OF-SELECTION.
data ob type ref to lcl_impl2.
create object ob.
INTERFACE intf1.
methods : m1,
m2.
endinterface.
endclass.
START-OF-SELECTION.
data ob type ref to lcl_impl.
create object ob.
interface intf1.
methods : m1,
m2.
endinterface.
interface intf2.
methods m3.
interfaces intf1.
endinterface.
method intf1~m1.
write :/ 'inside method m1 of intf1...'.
endmethod.
method intf1~m2.
write :/ 'inside method m2 of intf1...'.
endmethod.
endclass.
START-OF-SELECTION.
data ob type ref to lcl_impl.
create object ob.
Splitter control
It is used for splitting the container in to “N” no of partitions.
Each partition is called as a “PANE”.
Each “PANE” should be associated with a container to hold an object.
The object can be ALV Grid, Image and Tree.
Procedure for displaying images in ALV Reporting using classes
Step 1:
Upload the picture by using t-code ‘SMW0’.
In SMW0, choose the radio button ‘binary data for webrfc applications’,
click on ‘FIND’ (appl.toolbar) à provide package name(any package),
object name (any name, unique id) description(…), click on execute à
click on ‘create’ button (appl.toolbar),provide object name (ZBT),
description(…), click on ‘import button’ (status bar of dialog box)
Step 2:
Call the function module “dp_publish_www_url”.
This function module takes an image object id as input and returns the
“URL” of picture which is of type “cndp_url”.
Note:
“cndp_url” is a types declaration declared in the type group “CNDP”.
Step 3: Create the object of picture class ‘CL_GUI_PICTURE’ linking with
a container.
Step 4: call the instance method “load_picture_from_url” of the class
“cl_gui_picture”. This method takes URL of the picture as input and
displays the picture in the container.
Procedure for tree control:
A tree control is collection of nodes.
A node can be a folder / it can be an action item.
We can add the nodes to the tree control by using the instance method
“add_nodes” of the class
“cl_gui_simple_tree”.
This method contains two mandatory parameters.
1. Structure representing the nodes.
2. Internal table containing the nodes information.
Note: “ABDEMONODE” is one of the standard structure given by SAP
which represents simple tree (or) we can create custom dictionary
structure by including std.structure ‘TREEV_NODE’ and a character field
‘text’ of any length
Displaying ALV columns as Dropdown:
Generally we display ALV column as drop down when we have fixed set
of values to be shown to the user as a drop down so that the user can
select the value in the runtime.
Procedure:
Step 1:
Take an additional column in the final internal table, this additional
column needs to be displayed as dropdown.
Step 2:
Generate the field catalog for the additional column.
As part of the field catalog, set the field “DRDN_HNDL” to some
Numeric value and also set the edit field to ‘X’ as the column should be
editable, so that user can choose value from the drop down in the
runtime.
Step 3:
Prepare an internal table of type ‘LVC_T_DROP’ with drop down handle
and dropdown values and pass this internal table as a parameter to the
method ‘SET_DROP_DOWN_TABLE’ of the class ‘CL_GUI_ALV_GRID’.
Custom event handling process:
1. Needs to be declared in custom class definition
Persistence service:
It is used for storing the state of object (CURRENT values of the object)
permanently in the database.
By default, the life time of the object is local to the program i.e. once a
program execution is completed, memory allocated for the object will
be destroyed by the garbage collector.
Persistence object means -> permanent object
Any database operation (CRUD operations) can be achieved by using
“persistent service”.
Persistence service is implemented by creating persistence classes.
Persistence class is always created globally (class builder).
The naming standard of persistent class is ‘ZCL_<classname> ‘ /
’YCL_<classname>’.
Persistence class is always created as protected class.
Whenever a persistence class is created, SAP creates two more classes
internally.
a. Base agent class: (abstract class) i.e. ‘ZCB_<class name>’.
b. Actor /agent class: (Private class) i.e. “ZCA_<class name>”.
Base agent class: is a friend of persistence class and it is super class of
ACTOR / AGENT class.
Actor class: is a subclass of Base agent class
By Default, Persistence class implements a standard interface
‘IF_OS_STATE’ which is responsible for storing the state of the object
permanently in the database.