0% found this document useful (0 votes)
65 views

Abstract Methods Ooabap

1. Abstract methods cannot be implemented within an abstract class. You cannot define the implementation of an abstract method directly in an abstract class. 2. Final classes cannot be inherited from. A class defined with the final keyword cannot be subclassed. 3. Final methods cannot be redefined in subclasses. Methods defined with the final keyword in a parent class cannot be overridden in a subclass. Attempting to do so will result in a compilation error.

Uploaded by

prakash bhairi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Abstract Methods Ooabap

1. Abstract methods cannot be implemented within an abstract class. You cannot define the implementation of an abstract method directly in an abstract class. 2. Final classes cannot be inherited from. A class defined with the final keyword cannot be subclassed. 3. Final methods cannot be redefined in subclasses. Methods defined with the final keyword in a parent class cannot be overridden in a subclass. Attempting to do so will result in a compilation error.

Uploaded by

prakash bhairi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

abstract method can not be implemented in

abstract class.
You can not give implementation of abstract method with in the abstract
class , you can
see the example in above program , if u do it will give compilation error.
Final classes can not be inherited by other classes. Class defined
with final k/w
can not be inherited from the other classes means subclass , if you do it
will give
compilation error. Like this.
report zs_class_final_class. * CLASS fin_class
DEFINITION class fin_class definition final. "defining final
class using final k/w
Page 18 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
public
section.
methods
show.
endclass. "fin_class DEFINITION *
CLASS fin_class IMPLEMENTATION
class fin_class implementation.
method
show.
write 'hi this final method.' . endmethod.
"show endclass. "fin_class
IMPLEMENTATION
*inheriting the final class , but as we check using ctrl+f2 it will give error that final
class not be
subclassed. class inherit_final definition inheriting from fin_class.
endclas
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 29/78
s.
class inherit_final
implementation.
endclas
s.
so you must remember that final class never be
instantiated.
Final method can not be redefined in subclass. Final method
never be
reimplemented with in the subclass if you do this it will give you
compilation error as you
show in below code.
REPORT
ZS_CLASS_FINAL_METHOD.
class first
definition.
public section. methods showme final.
"defining the final method. endclass.
class first
implementation.
method showme. "implementing the final
method.
write 'hello this is
method' . endmethod.
endclass.
class second definition inheriting from
first.
public
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 30/78
section.
methods showme redefinition. "redefining the final method will give you compilation
error. endclass.
class second
implementation.
method
showme.
write 'hello
method' .
Page 19 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
endm
ethod.
endclass.
Encapsulation : The wrapping up of data and methods into a single
unit (called class)
is known as Encapsulation. The data is not accessible to the outside
world only those
methods, which are wrapped in the class, can access it.
Polymorphism: Methods of same name behave differently in different
classes. Identical
(identically-named) methods behave differently in different classes.
Object-oriented
programming contains constructions called interfaces. They enable you
to address
methods with the same name in different objects. Although the form of
address is always
the same, the implementation of the method is specific to a particular
class.
WORKING WITH
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 31/78

CONSTRUCTOR
Each class has only one instance constructor, which cannot be
overloaded. But,
using optional and default parameters allows seeming overloading.
Description of
Constructor:
❖ Constructor is automatically called when an object
created. ❖ Constructor is the same name of the class. ❖
No return value. ❖ With in static method we can only
access class attributes. ❖ Class-constructor does not
have any parameters. ❖ Constructor has only import
parameters .
There are two types of
constructor.
1. Instance
constructor 2. Class
constructor
Page 20 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Constructor can not have export
parameters.
When we call the constructor means during creation of object this import
parameter pass
by using export parameter like this.
when we create object then it will automatically call the constructor of
that class
then if that con structor have import parameter then we must pass that
value using
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 32/78
the export parameter.
Like
this.
REPORT
ZS_CLASS_CONSTRUCTOR1.
CLASS FIRST
DEFINITION.
PUBLIC
SECTION.
* CLASS CONSTRUCTOR WILL BE
EXECUTE FIRST . * DEFINING CLASS
CONSTRUCTOR.
CLASS-METHODS
CLASS_CONSTRUCTOR.
* DEFINING INSTANCE
CONSTRUCTOR.
METHODS CONSTRUCTOR importing today_date
type d. ENDCLASS.
* CLASS
IMPLEMENTATION.
CLASS FIRST
IMPLEMENTATION.
*INSTANCE CONSTRUCTOR
IMPLEMENTATION.
METHOD
CONSTRUCTOR.
WRITE / 'INSTANCE CONSTRUCTOR' . write : /
'TODAY DATE IS ' , today_date dd/mm/yyyy.
ENDMETHOD. * CLASS CONSTRUCTOR
IMPLEMENTATION.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 33/78
METHOD
CLASS_CONSTRUCTOR.
WRITE / 'CLASS
CONSTRUCTOR' .
ENDMETHOD. ENDCLASS.
*EVENT OF REPORT WHERE EXECUTION
OCCCUR.
START-OF-SELECTIO
N. * CREATING REFERNCE OF
CLASS FIRS.
DATA OBJ TYPE REF TO FIRST. * CREATING OBJECT OF CLASS FIRST THEN
IT
WILL CALL FIRST THE CLASS CONSTRUCTOR THEN IT WILL CALL THE *
OBJECT
CONSTRUCTOR.
CREATE OBJECT OBJ exporting today_date = sy-datum. "when we create object
then it will
automatically call the * constructor of that class then if that constructor have import
parameter then we
must pass that value using the * export parameter.
Page 21 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Objects must be created at runtime (CREATE OBJECT – instruction in
ABAP Objects).
With their creation they also get their own identity. However, there are
no fixed attribute
values linked to the identity. You are probably already wondering how
objects get to their
initial state. How do objects recognize their i nitial attribute values? How
does an object
know during its creation that it is a Miller and not a Meyer ?
The Constructor concept exists specifically to answer this question. The
constructor is a
method, which runs automatically during the creation of an object. The
constructor allows
you to define IMPORTING-parameters.
In ABAP Objects you differentiate between instance-dependent and
class-dependent
constructors via the language elements METHODSand CLASS-
METHODS to be used in
the definition part and via their names constructor and The class
constructor is called by
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 34/78
the first access to a class element (method, attribute, event, object), the
(instance)
constructor by the creation of an object (CREATE OBJECT).
You can now create small programs with the help of the ABAP Objects
language
instructions you have learned. You know the basic elements of classes
(attributes and
methods), the differences between classes and their objects and how to
express these in
ABAP Objects.
HERE IS SAMPLE PROGRAM WHO USE CLASS CONSTRUCTOR
AND
INSTANCE CONSTRUCTOR.
REPORT
ZS_CLASS_CONSTRUCTOR1.
CLASS FIRST
DEFINITION.
PUBLIC
SECTION.
* CLASS CONSTRUCTOR WILL BE
EXECUTE FIRST . * DEFINING CLASS
CONSTRUCTOR.
CLASS-METHODS
CLASS_CONSTRUCTOR.
* DEFINING INSTANCE
CONSTRUCTOR.
METHODS
CONSTRUCTOR.
ENDCLASS.
* CLASS
IMPLEMENTATION.
CLASS FIRST
IMPLEMENTATION.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 35/78
*INSTANCE CONSTRUCTOR
IMPLEMENTATION.
METHOD
CONSTRUCTOR.
WRITE / 'INSTANCE
CONSTRUCTOR' . ENDMETHOD. *
CLASS CONSTRUCTOR
IMPLEMENTATION.
METHOD
CLASS_CONSTRUCTOR.
WRITE / 'CLASS
CONSTRUCTOR' .
Page 22 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
END
METHOD.
ENDCLASS.
*EVENT OF REPORT WHERE EXECUTION
OCCCUR.
START-OF-SELECTIO
N. * CREATING REFERNCE OF
CLASS FIRS.
DATA OBJ TYPE REF TO FIRST. * CREATING OBJECT OF CLASS FIRST THEN
IT
WILL CALL FIRST THE CLASS CONSTRUCTOR THEN IT WILL CALL THE *
OBJECT
CONSTRUCTOR.
CREATE OBJECT
OBJ.
SAMPLE PROGRAM FOR UTILIZING THE CONSTRUCOTR WITH IN
ABAP OOPS.
REPORT
ZS_CLASS_CONSTRUCTOR.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 36/78
*CLASS DEFINITION. CLASS
FIRST DEFINITION.
PUBLIC SECTION. DATA ONE(12). METHODS CONSTRUCTOR
IMPORTING A TYPE C. "CONSTRUCTOR DEFINITION. ENDCLASS.
*CLASS FIRST IMPLEMENTATION. CLASS FIRST IMPLEMENTATION.
METHOD CONSTRUCTOR. "CONSTRUCTOR
IMPLEMENTATION.
ONE = A. "ASSIGNING THE VALUE TO ONE WRITE: / 'THIS IS
CONSTRUCTRO WHICH CALL WHEN OBJECT CREATED' , ONE.
END
METHOD.
ENDCLASS.
START-OF-SELEC
TION.
DATA OBJ TYPE REF TO FIRST . "OBJECT REFERENCE TYPE WRITE / 'BEFOR
OBJECT CREATION' . CREATE OBJECT OBJ EXPORTING A = 'SDK' .
"CREATING
OBJECT WILL CALL THE CONSTRUCTOR.
Page 23 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
METHOD WITH IMPORT , EXPORT , CHANGING PROPERY
AND
CALLING IT.
export and import parameter of the method definition change during
import and export
parameter respectively and with in export parameter we use to pass
value , while with in
import parameter we pass the data object where we get the value of
import parameter
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 37/78
same in changing parameter.
Here is the example
below.
REPORT
ZS_CLASS_CONSTRUCTOR.
DATA SECOND(12).
*CLASS DEFINITION.
CLASS FIRST
DEFINITION.
PUBLIC SECTION. DATA ONE(12). METHODS CONSTRUCTOR
IMPORTING A TYPE C. "CONSTRUCTOR DEFINITION. * method
definition with all type of parameter.
METHODS SHOWME IMPORTING A TYPE C EXPORTING B
TYPE C CHANGING D TYPE C. ENDCLASS. "FIRST DEFINITION *CLASS
FIRST IMPLEMENTATION. CLASS FIRST IMPLEMENTATION.
METHOD CONSTRUCTOR. "CONSTRUCTOR
IMPLEMENTATION.
ONE = A. "ASSIGNING THE VALUE TO ONE WRITE: / 'THIS IS
CONSTRUCTRO WHICH CALL WHEN OBJECT CREATED' , ONE.
ENDMETHOD. "CONSTRUCTOR * METHOD IMPLEMENTATION
METHOD
SHOWME.
D = 'HELLO CHANGE' . B =
'HELLO IMPORT
SECODND' .
WRITE : / 'THE VALUE OF A IS ' , A.
WRITE : / 'THE VALUE OF B IS ' , B.
WRITE : / 'THE VALUE OF D IS ' , D.
ENDMETHOD. "SHOWME ENDCLASS.
"FIRST IMPLEMENTATION
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 38/78
START-OF-SELEC
TION.
DATA OBJ TYPE REF TO FIRST . "OBJECT REFERENCE TYPE WRITE / 'BEFOR
OBJECT CREATION' . CREATE OBJECT OBJ EXPORTING A = 'SDK' .
"CREATING
OBJECT WILL CALL THE CONSTRUCTOR. * CALLING THE METHOD SHOWME.
CALL METHOD
OBJ->SHOWME
Page 24 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
EXPORTI
NG
A=
'SHOWME'
IMPORTING
B=
SECOND
CHANGIN
G
D = SECOND. WRITE : / 'AFTER CHANGE
PARAMETER THE SECOND VALUE IS : ' , SECOND.
Static constructor can be triggered at the beginning of a
processing block(form /event/block/procedure)
Static constructor will only call when either we create object or access
the class type of
data or call class type method, other wise it will not call or it call atfirst.
REPORT
ZS_CLASS_STATIC_CONSTRUCTOR.
* CLASS first DEFINITION CLASS
FIRST DEFINITION.
PUBLIC
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 39/78
SECTION.
CLASS-DATA NUM TYPE I VALUE 23. "defining class or static data. CLASS-
METHODS
CLASS_CONSTRUCTOR. "for defining class constructor always define with
class_constructor.
ENDCLASS. "first DEFINITION
* CLASS first
IMPLEMENTATION
CLASS FIRST
IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR. "constructor
implementation.
WRITE / 'hi i am class
constructor' . ENDMETHOD.
"class_constructor ENDCLASS. "first
IMPLEMENTATION
START-OF-SELEC
TION.
WRITE : / 'hello selection' . WRITE : / 'hello class data' ,
FIRST=>NUM. "as we access the value of class data the
class constructor will call.
*if we comment the second line write code then static consturctor will not , it will only
call when either we
create o bject, call static method or accessing the static attributes.
Page 25 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Working on
Polymorphism
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 40/78
Polymorphism occurs, where classes implement the same functionality
with different
methods (one functionality, several methods but the same name). This
can occur via an
inheritance relationship, in that the methods belonging to the superclass
are redefined i n
the subclasses and implemented differently. ABAP Objects requires the
method names to
be the same and the signature to be the same (signature = method
interface).
Polymorphism can be achieved in 2
ways:
• Two independent classes implement methods with the same names
and the same
signature with the intention, that the methods should be called
dynamically from a third
location.
• A superclass implements a method, and in a subclass you want to re-
implement the
same method, as the superclass implementation is not suitable for the
subclass.
The first scenario will not occur very often in ABAP Objects, as the
interface concept was
created precisely for such cases (see Interfaces).
POLYMORPHISM:- Polymorphism 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. This allows each subclass to
differ, without the
parent class being explicitly aware that a difference exists.
CLAUSES REGARDING
POLYMORPHISM:-
✓ Allows one interface to be used for a general class of actions. ✓ When
objects from
different classes react differently to the same procedural call. ✓ User can
work with
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 41/78
different classes in a similar way, regardless of their implementation.
4.Allows improved
code organization and readability as well as creation of “extensible”
programs.
5.Although the form of address is always the same, the implementation
of the method is
specific to a particular class.
Here in below code First class definition we used to define ADD method
, with in the same
class we use to define another class second which inherit the property
from FIRST class in
which we redefine the method ADD again ,.
REPORT
ZS_SINGLE_INHERITANCE.
CLASS FIRST DEFINITION. *
DEFINING ATTRIBUTES.
Page 26 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
PUBLIC SECTION. DATA
FIRST(12). "DEFINING
ATTRIBUTES METHODS ADD.
"DEFINING METHOD.
ENDCLASS.
* IMPLEMENTING
CLASS.
CLASS FIRST
IMPLEMENTATION.
METHOD ADD. "METHOD
IMPLEMEMNTATION.
WRITE / 'HELLO METHOD FROM THE
FIRST' . ENDMETHOD. ENDCLASS.
* DEFINING OTHER CLASS WHICH INHERIT PROPERTY FROM FIRST AND
REDEFINIG THE
FIRST CLASS METHOD.
CLASS SECOND DEFINITION
INHERITING FROM FIRST. PUBLIC SECTION.
DATA SECOND(13). METHODS HELLO. *
REDEFINING THE ADD METHOD OF CLASS
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 42/78
FIRST.
METHODS ADD REDEFINITION. "REDEFINING THE
ADD METHOD. ENDCLASS.
CLASS SECOND
IMPLEMENTATION.
METHOD
HELLO.
WRITE / 'HELLO FROM
SECOND' . ENDMETHOD.
METHOD ADD.
WRITE / 'FROM THE SUBCLASS
REDEFINITION' . ENDMETHOD.
ENDCLASS.
*EVENT WHERE DATA EXECUTION
OCCUR.
START-OF-SELEC
TION. * CREATE REFERNCE
OF OBJECT. DATA OBJ
TYPE REF TO FIRST. *
CREATING OBJECT.
CREATE
OBJECT OBJ. * CALLING
MEHTOD OF CLASS.
CALL METHOD
OBJ->ADD.
* CREATE REF OF SECOND
CLASS
DATA OBJ1 TYPE
REF TO SECOND. *
CREATING OBJECT
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 43/78
Page 27 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CREATE OBJECT
OBJ1. * CALLING
THE METHOD.
CALL METHOD OBJ1->HELLO. * CALLING
THE SUPER CLASS METHOD USING SUBCLASS
OBJECT.
CALL METHOD
OBJ1->ADD.
Working with
Interfaces
In ABAP interfaces are implemented in addition to, and independently of
classes. An
interface only has a declaration part, and do not have visibility sections.
Components
(Attributes, methods, constants, types) can be defined the same way as
in classes.
✓ Interfaces are listed in the definition part of the class, and must always
be in the
PUBLIC
SECTION. ✓ Operations defined in the interface are implemented as
methods
of the class. All methods
of the interface must be present in the implementation part of the class.

Attributes, events, constants and types defined in the interface are
automatically
available
to the class carrying out the implementation. ✓ Interface components are
addressed in the class by <interface name>~<component name>
REPORT
ZS_INTERFACE.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 44/78
*DEFINING THE INTERFACE AND ITS METHOD OR ATTRIBUTES INTERFACE
FIRST. *METHOD
DEFININTION ALWAYS STARTS WITH METHODS NOT METHOD AND IN
INTERFACE ATTRIBUTES
AND MET HODS ARE PUBLIC. DATA NAME(12). METHODS SHOW.
ENDINTERFA
CE.
* IMPLEMENTING THE
INTERFACE .
CLASS IMPLEMENT_INTERFACE DEFINITION. "CLASS
DEFINITION.
PUBLIC SECTION. * INTERFACE WHICH YOU WANT TO IMPLEMENT WITH IN
A CLASS MUST PASS WITH IN PUBLIC SECTION
INTERFACES FIRST. "INTERFACE WHICH I WANT TO IMPLEMENT WITH
IN THIS CLASS.
METHODS
METHOD1.
ENDCLASS.
* IMPLEMENTING
CLASS.
Page 28 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CLASS IMPLEMENT_INTERFACE
IMPLEMENTATION. * IMPLEMENTIONG
INTERFACE METHOD.
METHOD
FIRST~SHOW.
WRITE / 'THIS IS INTERFACE
METHOD.' . ENDMETHOD.
* IMPLEMENTING CLASS
METHOD.
METHOD
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 45/78
METHOD1.
WRITE / 'THIS IS CLASS
METHOD' . ENDMETHOD.
ENDCLA
SS.
* EVENT
START-OF-SELECTION.
START-OF-SELECTION. DATA OBJ
TYPE REF TO IMPLEMENT_INTERFACE.
CREATE OBJECT OBJ. * CALLING THE
INTERFACE METHOD. CALL METHOD
OBJ->FIRST~SHOW. * CALLING THE CLASS
METHOD.
CALL METHOD
OBJ->METHOD1.
Interfaces can only be implemented in the public section of a class if you
implement it
either in private or protected section it will give you compilation error.
A class with an interface should implement all the methods of that
interface other wise it
will give compiler error.
ABAP
Objects :
• ABAP Objects is an upwards-compatible extension of the existing
ABAP
language.
• You can use existing ABAP statements within ABAP
Objects.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 46/78
• You can use ABAP Objects within existing
programs.
• ABAP Objects is full integrated in the ABAP
debugger.
• Instances of function groups as
objects
• Example: Function group as
counter.
Page 29 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Elements of ABAP
Objects :
• Classes
• Interfaces
• Objects
• Object
References

Inheritance
Classes :
• Classes are the central element of
Object-orientation
• A class describes a general element or a general concept, for example
the abstract
concepts Business Partner, Material, Transaction, Equipment or list.
Classes realize an
abstract data types.
• You can define classes globally using the class builder (Transaction
SE24) or locally
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 47/78
in an ABAP program.
• Local classes and interfaces can only be used in the program in which
they are
defined.
Components of
classes.
• Attributes
• Methods
• Constructor
• Events
• Types and
Constants.
In ABAP Objects classes are made up of a definition and an
implementation part.
*Declaring class definition attribute and
property.
CLASS FIRST DEFINITION. * BODY OF THIS CLASS OR DEFINE METHODS
CONSTRUCTOR TYPES AND EVETNT
ENDCLA
SS.
*Implementation of that class and its
method.
CLASS FIRST IMPLEMENTATION. *IMPLEMENT METHODS AND CLASS WITH
IN THE
CLASS.
ENDCLA
SS.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 48/78
Page 30 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Attribute
s.
Attributes can take on values within an object at runtime. The sum of all
attributes and
values describes the state of an object.
In ABAP Objects attributes are defined in the definition part via the
ABAP keyword DATA.
In this example we have defined that the name of a business partner
may be 50
characters long and is of the data type Character:
CLASS cl_party DEFINITION. PUBLIC SECTION. "I will return later to
the meaning of
the language element Public Section see Visibility DATA: name(50) type
C.
ENDCLASS.
Here name is a attribute of class
cl_party .
Attributes can be defined as instance dependent as well as Class
dependent .
Class attributes (Class attributes are also called static attributes) are not
tied to a single
instance, rather they "belong" to all instances of the Class. These
attributes exist only
once in main memory. Instance-dependent attributes exist once per
instance and are tied
to a single instance. For example, the attribute name is an instance-
dependent attribute,
while the attribute instance_count could be a Class-dependent attribute,
in which the
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 49/78
number of instances of the Class cl_party could be noted.
In ABAP Objects you differentiate between instance-dependent and
class-dependent
attributes by means of the ABAP keywords DATA or CLASS-DATA to be
used in the
definition part:
CLASS cl_party DEFINITION. PUBLIC SECTION.
CLASS-DATA: instance_count type i. DATA: name(50) type c.
ENDCLASS. START-OF-SELECTION. DATA: instance TYPE
REF TO cl_party. * Access to the different attribute types is
also defined in the syntax: * Class attribute:
cl_class => instance_count * Instance attribute: instance -> name
Page 31 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
IF cl_class=>instance_count
< 50. CREATE OBJECT
instance. ... ENDIF. IF
instance->name <> 'Miller'. ...
ENDIF.
*Declaring class definition attribute and
property.
CLASS
FIRST DEFINITION. *
public section of class.
PUBLIC SECTION. data
instance_data(12).
class-data class_data(12).
METHODS: SHOWME
,SAWME.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 50/78
ENDCLASS. *Implementation
of that class and its method.
CLASS FIRST
IMPLEMENTATION. * Method
implementation.
METHOD
SHOWME.
WRITE 'SHOW ME
METHOD' .
ENDMETHOD.
METHOD
SAWME.
WRITE / 'SAW WITH IN THE
SUPERCLASS' . ENDMETHOD.
ENDCLASS. * *always create refernce and object
in START-OF- SELECTION Event because the data extraction
come under this event.
START-OF-SELEC
TION.
*creating reference of the
object. DATA FIRSTOBJ TYPE
REF TO FIRST. *creating object
of the reference. CREATE
OBJECT FIRSTOBJ.
*using the class and instance
object.
FIRSTOBJ->instance_data = 'INSTNCE DATA' . "GIVING THE VALUE TO
INSTANCE ATTRIBUTE. FIRSTOBJ->CLASS_DATA = 'CLASS DATA' . "GIVING
THE VALUE TO CLASS ATTRIBUTE.
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 51/78
Page 32 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
WRITE : / 'ACCESSING CLASS DATA : ' , FIRSTOBJ->CLASS_DATA.
WRITE : / 'FIRST OBJECT INSTANCE METHOD DATA : ' ,
FIRSTOBJ->INSTANCE_DATA.
*CREATING ANTOTHER REFERENCE AND OBJ OF
FIRST CLASS. DATA FIRSTOBJ1 TYPE REF TO
FIRST. CREATE OBJECT FIRSTOBJ1.
*ACCESSING THE CLASS DATA OF FIRSTOBJ1. WRITE : / 'ACCESSING
THE CLASS DATA USING ANOTHER OBJECT :' , FIRSTOBJ1-
>CLASS_DATA. "HERE IT WILL GIVE OUTPUT *BECUASE THIS IS CLASS
DATA. WRITE : / 'ACCESSING THE INSTANCE DATA USING ANOTHER
OBJECT : ' , FIRSTOBJ1- >INSTANCE_DATA. "HERE IS GIVE BLNAK VALUE
*BECUASE HERE NOT SPECIFY THE VALUE OF INSTANCE_DATA FOR
OBJECT FIRSTOBJ1 ,
Here in above program we can see that the class attribute value remain
same for all object
same type class , where as the instance data of class have different
value for each class.
Method
s
As well as attributes, Classes have so-called Methods (you could say
functions instead of
methods but, as mentioned in the introduction: New technologies require
new names).
While attributes describe the static structure of a class and its objects,
Methods describe
the behavior of objects within a class. With the help of methods, the
system provides
operations, services and functions. Via methods, a user can manipulate
the objects in a
class or also the class itself. As for attributes, there are instance-
dependent as well as
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 52/78
class-dependent (static) methods.
In order to carry out instance-dependent (or instance-dependent)
methods, the calling
program needs a specific instance of the class. That is, the calling
program must have
a defined reference variable, that points to a specific instance. Class
methods are not
instance- dependent. They can be called at any time by a user. To see
how the syntax
calls the various method types, see the following example.
REPORT
ZS_CLASS.
*Declaring class definition attribute and
property. CLASS FIRST DEFINITION. *
public section of class.
PUBLIC
SECTION.
data instance_data(12). "INSTANCE DATA
DELCARATION.
Page 33 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
class-data class_data(12). "CLASS DATA DELCARATION. METHODS:
SHOWME ,SAWME. "INSTANCE METHODS DECLARATION.
CLASS-METHODS:CLASSMETHOD_SHOWME. "CLASS METHOD
DECALRATION.
ENDCLASS. "FIRST DEFINITION
*Implementation of that class and its
method. CLASS FIRST
IMPLEMENTATION. * INSTNACE Method
implementation.
METHOD
SHOWME.
WRITE 'SHOW ME
METHOD' . ENDMETHOD.
"SHOWME *INSTANSE METHOD
IMPLEMENTATION.
METHOD
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 53/78
SAWME.
WRITE / 'SAW WITH IN THE
SUPERCLASS' . ENDMETHOD.
"SAWME *CLASS METHOD
IMPLEMENTATION.
METHOD CLASSMETHOD_SHOWME. WRITE / 'THIS IS CLASS METHOD.' .
ENDMETHOD.
ENDCLASS. "END OF FIRST CLASS IMPLEMENTATION
* *always create refernce and object in START-OFSELECTION
Event because the data extraction come under
this event.
START-OF-SELEC
TION.
*creating reference of the
object.
DATA FIRSTOBJ TYPE REF TO
FIRST. DATA SECONDOBJ TYPE
REF TO SECOND.
*creating object of the
reference.
CREATE OBJECT
FIRSTOBJ. *calling the
method of that class.
CALL METHOD
FIRSTOBJ->SHOWME. *Creating
object of second class CREATE
OBJECT SECONDOBJ. *calling
method of subclass and superclass.
CALL METHOD
SECONDOBJ->SHOWME. CALL
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 54/78
METHOD SECONDOBJ->SAWME.
*CALLING CLASS
METHOD.
Page 34 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CALL METHOD
FIRSTOBJ->CLASSMETHOD_SHOWME.
*using the class and instance
object.
FIRSTOBJ->instance_data = 'INSTNCE DATA' . "GIVING THE VALUE TO
INSTANCE ATTRIBUTE. FIRSTOBJ->CLASS_DATA = 'CLASS DATA' . "GIVING
THE VALUE TO CLASS ATTRIBUTE. WRITE : / 'ACCESSING CLASS DATA : ' ,
FIRSTOBJ->CLASS_DATA. "ACCESSING CLASS DATA . WRITE : / 'FIRST
OBJECT INSTANCE METHOD DATA : ' , FIRSTOBJ- >INSTANCE_DATA.
"ACCESSING THE INSTACNE DATA.
*CREATING ANTOTHER REFERENCE AND OBJ OF
FIRST CLASS.
DATA FIRSTOBJ1 TYPE REF
TO FIRST. CREATE OBJECT
FIRSTOBJ1.
*ACCESSING THE CLASS DATA OF
FIRSTOBJ1.
WRITE : / 'ACCESSING THE CLASS DATA USING ANOTHER
OBJECT :' , FIRSTOBJ1- >CLASS_DATA. "HERE IT WILL GIVE
OUTPUT *BECUASE THIS IS CLASS DATA.
WRITE : / 'ACCESSING THE INSTANCE DATA USING ANOTHER
OBJECT : ' , FIRSTOBJ1- >INSTANCE_DATA. "HERE IS GIVE BLNAK VALUE
*BECUASE HERE NOT SPECIFY THE VALUE OF INSTANCE_DATA FOR
OBJECT FIRSTOBJ1 ,
CONSTRUCTOR IN ABAP
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 55/78

CLASS
Objects must be created at runtime (CREATE OBJECT – instruction in
ABAP Objects).
With their creation they also get their own identity. However, there are
no fixed attribute
values linked to the identity. You are probably already wondering how
objects get to their
initial state. How do objects recognize their initial attribute values? How
does an object
know during its creation that it is a Miller and not a Meyer ? The
Constructor concept
exists specifically to answer this question. The constructor is a method
which runs
automatically during the creation of an object. The constructor allows
you to define
IMPORTING-parameters.
In ABAP Objects you differentiate between instance-dependent and
class-dependent
constructors via the language elements METHODSand CLASS-
METHODS to be used in
the definition part and via their names constructor and class_constructor:
Page 35 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
There are two types of constructor in ABAP
CLASS.
1. Instance
construcror 2. Class
constructor
Instance constructor will call when you create object using that
constructor.
EVENTS IN ABAP
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 56/78
CLASS.
The steps to be followed are as
follows:-
❖ Create an event in a class ❖ Create a triggering method in the
same class which will raise the event. ❖ Create an event handler
method for the event in same/other class. ❖ Register the event
handler method in the program.
Now, your settings are complete. Create an object from the class
containing the event and
call the triggering method to raise the event.
* CLASS c1
DEFINITION class c1
definition.
public
section. *(1)Creating
event : E1
events: e1. *(2) Creating an event handling
method. This method can belong to * same or different class
methods: m1 for
event e1 of c1. * Method to
raise the event
methods : t1. endclass. "c1
DEFINITION * CLASS c1 IMPLEMENTATION
class c1 implementation. *Method : M1 will be
called when the event is raised
method :
m1.
write:/5 'I am the event
handler method' . endmethod. ": *
Method : T1 will raise the event

You might also like