SAP ABAP OOP Basics For Bigners 1698198003
SAP ABAP OOP Basics For Bigners 1698198003
SAP ABAP OOP Basics For Bigners 1698198003
1. Encapsulation --> binding data and member functions into single unit (class)
4. Execute select queries to read data from db into work area / internal table
5. Process the work area/ internal table to show the content (data) to the user
field 2, ename
----
ty_emp-empno = 1. (invalid)
wa_emp1-empno = 1.
wa_emp1-ename = 'Raju'.
wa_emp2-empno = 4.
wa_emp2-ename = 'Ramesh'.
2. Global class --> global to all objects ----> class builder tool (se24)
1. Definition of class
Syntax:
declaration of components.
endclass.
2. Implementation of class
Syntax:
implementation of methods.
endclass.
Note: Class definition and Class implementation doesn't allocate any memory,
it only provides template of the class
Instantiate the class ---> creating the object for the class--> memory will be
allocated
Syntax:
Syntax:
They are specific to object i.e. for every object, separate memory will be allocated
for each instance attribute.
Static Attributes:
The memory for static attributes will be allocated whenever the class is loaded in
to the memory i.e. memory will be allocated only once which will be shared by all
the objects of the class.
They can be accessed either by using the object or by using the class name.
Constant Attributes:
In Local classes, they are declared by using the keyword ‘constants’. They must be
initialized at the time of declaration itself. The value of the constant attribute
remains same throughout the program.
They are not specific to any object.
The memory for them will be allocated whenever the class is loaded in to the
memory i.e. memory will be allocated only once which will be shared by all the
objects of the class.
They can be accessed either by using the object or by using the class name.
Methods:
- A method is a set of statements which is implemented only once and can
be called any no. of times.
- It is similar to subroutine / function module.
Subroutines: - 2 sections ---> definition ---> form...endform
Method Parameters:
Types of parameters: importing, exporting, changing and returning.
1. Declaration
syntax:
2. Implementation
syntax:
statements.
endmethod.
3. Calling
syntax 1:
syntax 2:
Returning Parameters:
- In general, a method can return any no. of parameters.
- To restrict a method to return exactly one value, we must use returning
parameter.
- If a method contains returning parameter it cannot contain exporting
/changing parameters at the same time or vice versa (prior EHP7)
- A method can contain only one returning parameter
- Returning parameter is always passed by value.
- Returning parameters are always optional.
Instance methods:
In local classes they are declared by using the keyword “Methods”.
They can access any kind of components of the class (instance / static /
constant / types).
Static methods:
In local classes they are declared by using the keyword “class-Methods”.
They can access only static components, constant attributes and types
attributes. i.e they cannot access instance components.
Note: In Local classes, the sequence of visibility sections (access specifiers)
for the class components should be in the order of public, protected,
private sections.
Me keyword:
“Me” keyword refers to current object in execution i.e. as part of every
instance method execution (runtime), an implicitly created object (created
by SAP) will be available and it refers to the object which is currently
executing the method.
Friend keyword:
In General outside the class, A Object can access only public components of
the class directly. i.e protected and private components of the class cannot
be accessed outside the class using an object of a class.
In order to enable the objects of the class to access all components of the
class outside the class irrespective of the visibility, then we can go for
Friend classes.
Load keyword:
It is used to load the global classes or global interfaces explicitly in the
executable program. This is mandatory before the release 6.20 for
accessing static components of the global class or global interface before
instantiating them.
From 6.20, it is not required, but in case, if we get any compilation error
while accessing the static components / any other components of global
class or global interface, then we can load the global class or global
interface explicitly from the class library by using load keyword.
Constructor:
- It is special method used for initializing the attributes of the class i.e.
whenever an object is created, the attributes should be initialized with
some initial values.
1. Instance constructor.
2. Static constructor.
Instance constructor
- They are declared by using the keyword “Constructor”.
- Instance constructor is executed only once in the life time of every object.
Static constructor
- It is declared by using keyword “class_constructor”.
- It is executed only once in the life time of every class. I.e. it is executed in
either of the following cases.
- Case 1: When we access any static components of the class before creating
any objects for the class. (or)
- Case 2: when we create first object of the class before accessing any static
components of the class.
Note:
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.
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.
Morph-> forms,
Ism-> behaviour
Example:
Method Overloading:
It is similar to function overloading in C++.
Method Overriding:
- If a subclass redefines a super class method it is called as Method
Overriding.
- Whenever a local subclass wants to redefine the super class method, the
sub class as to re-declare super class method by using “redefinition”
keyword.
- In this case, if we instantiate first object of sub class before accessing any
static components of super/sub class and before creating any objects for
super class, then SAP first executes static constructors from super class to
sub class (top to bottom) and then instance constructors from sub class to
super class (bottom to top) and from second object of sub class onwards,
only instance constructors will be executed from sub class to super class.