2. ObjectivesObjectives
• Understand basic concepts of Object Oriented
Programming
• Components of a Class
• Identify the characteristics of OOP
• Implement the characteristics of OOP in ABAP Objects
3. Basic concept of Object Oriented ProgrammingBasic concept of Object Oriented Programming
The fundamental idea behind Object Oriented Programming
(OOP) is to combine both data and the functions or
methods (which operates on that data) into a single unit.
Such units, in general terminology, are called Object.
OBJECT
VEHICLE
Plate No
Speed
Accelerate( )
Show_Speed( )
UNIT
4. Need of Object Oriented ProgrammingNeed of Object Oriented Programming
Higher level of abstraction for solving real-life problems
Reusability
Allows to think in the problem space rather than thinking about
data declaration, decisions, loops etc. like in procedural
languages.
Better control over process visibility
5. Components of a ClassComponents of a Class
There are three components
• AttributesAttributes
• MethodsMethods
• EventsEvents
CLASS vehicle
DATA : speed TYPE i.
METHODS : accelerate,
show_speed.
EVENTS : seatBeltSign
6. Creating class instanceCreating class instance
Creating a class instance is somewhat similar to creating a data object by
taking reference of a data type.
DATA : my_var TYPE i.
For creating class instance :
1) Data : obj_vehicle TYPE REF TO vehicle.
a reference variable of type “pointer to an object of the vehicle class” is created
2) CREATE OBJECT obj_vehicle.
an instance of the vehicle class is created
a reference to this instance is assigned to “obj_vehicle”
7. Type of componentsType of components
• Static Component
• Instance Component
Instance components exist separately in each instance (object)
of the class.
•DATA
•METHODS (Can access all attributes and events of their own class)
•EVENTS
Static components only exist one per class and are valid for all
instances of the class.
•CLASS-DATA
•CLASS-METHODS (Can access only static attributes and static events )
•CLASS-EVENTS
•CONSTANTS
See Program YCLASS_STATIC (TRZ300)
8. Type of components Contd..Type of components Contd..
Instance Attribute
Static Attribute
Definition
Implementation
Instantiation
Output
9. CLASS (Global + Local)CLASS (Global + Local)
Classes can be of two types
• Global class
• Local class
Global vs Local Global Local
Accessibility Any program Only within the
program where it is
defined
Stored in Class repository In the program where it
is defined
Tools to create SE24 Class builder SE38 ABAP Editor
Namespace Must begin with ‘Y’ or
‘Z’
Any
10. Class declaration (Local)Class declaration (Local)
CLASS vehicle DEFINITION.
DATA speed TYPE I VALUE 0.
METHODS: accelerate,
show_speed.
ENDCLASS.
CLASS vehicle IMPLEMENTATION.
METHOD accelerate.
speed = speed + 1.
ENDMETHOD.
METHOD show_speed.
WRITE speed.
ENDMETHOD.
ENDCLASS.
DEFINITION IMPLEMENTATION
11. • The purpose of abstract class is to act as a ‘Model’ for subclasses.
• They cannot be instantiated.
• To use an abstract class, it has to be inherited in the subclass and its
abstract method should redefined.
• Abstract methods does not need to implemented in super class
Why use abstract class ?
An abstract class is used when we want to model an object, the final
state of which is not known at the time of modeling and will be known
later
Abstract ClassAbstract Class
12. Abstract Class Contd..Abstract Class Contd..
If a method is declared as abstract, then
there is not need to implement it in super
class but it needs to be ‘redefined’ in
subclass.
13. • A class that is defined as final class can not be inherited further.
• All Methods of a final class are inherently final and must not be
declared as final in the class definition.
• Also, a final method can not be redefined further.
• If only a method of a class is final then that class can be inherited but
that method cannot be redefined.
Why use final class ?
If you don't want anyone else to change or override the functionality
of your class then you can define it as final. Thus no one can inherit
and modify the features of this class.
Final ClassFinal Class
15. Class VariantsClass Variants
CLASS class DEFINITION DEFERRED.
When the class needs to be referred before it is being defined.
As we have deferred it at the initial stage, the system would not look for this class at higher
level context, such as global class library.
16. Class Variants Cont..Class Variants Cont..
CLASS <class> DEFINITION LOAD.
•The definition of global classes loads into the memory when a local class is used first time in a
program.
•When the first access is to a ‘static component’ or in the ‘definition of an event handler
method’ then the global class needs to be loaded explicitly into the memory first.
CLASS CL_GUI_PICTURE DEFINITION LOAD.
CLASS C1 DEFINITION. PUBLIC SECTION.
METHODS HANDLER FOR EVENT PICTURE_CLICK OF CL_GUI_PICTURE.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD HANDLER.
...
ENDMETHOD.
ENDCLASS.
17. Characteristics of Object OrientedCharacteristics of Object Oriented
languagelanguage
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
18. AbstractionAbstraction
“Abstraction is the elimination of the irrelevant and amplification of the essentials”
Everything that is important to the user is visible, but everything that is not, is hidden.
Essentials
Accelerator
Brake
Steering wheel
Irrelevant
Engine mechanism
Gear mechanism
Brake mechanism
19. Visibility All
users
Subclass
methods
Class
methods
PUBLIC X X X
PROTECTED X X
PRIVATE X
EncapsulationEncapsulation
ALL
USERS
CLASS C1
PUBLIC PRIVATE
SUBCLASSES of C1
PROTECTED
METHODS
Encapsulation is Hiding data!
Encapsulation is hiding process; hence
then you hide the data. Not vice versa
21. InheritanceInheritance
Inheritance is the process of creating new classes, called derived classes or
subclasses or child classes from existing classes called base classes or super
classes or parent classes. The derived class inherits all the capabilities of the
base class but can add refinements of its own.
The base class is unchanged by this process.
VEHICLEVEHICLE
accelerate()
show_speed()
PLANEPLANE
accelerate()
show_speed()
rise()
TORPEDOTORPEDO
accelerate()
show_speed()
sink()
FIGHTER JETFIGHTER JET
accelerate()
show_speed()
rise()
launch_missile()
22. Inheritance Contd..Inheritance Contd..
Syntax for creating sub classes (local):
See Program YCLASS_INHERT_DEMO (TRZ300)
For global classes, go to transaction SE24 -> properties tab and define
the super class.
23. PolymorphismPolymorphism
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.
VEHICLEVEHICLE
accelerate()
show_speed()
PLANEPLANE
accelerate()
show_speed() Redefinition
TORPEDOTORPEDO
accelerate()
show_speed() Redefinition
25. ConstructorConstructor
A constructor is a special method which is
automatically executed by the runtime
environment.
They are used to set an object dynamically to a
defined initial state.
There are two type of constructor
1. Instance Constructor
They are called once for each instance after the complete creation of the instance with the
CREATE OBJECT statement.
2. Static Constructor
They are called in a program once for each class before the class is accessed for the first
time.
26. Call components (static + instance)Call components (static + instance)
• Attributes
Call instance attribute of an object
Static attribute of an object
oref->attr
Static attribute of a class class=>attr
Its own instance attribute
Its own static attribute
me->attr OR
attr
• Methods
Call instance method of an object
Static method of an object
CALL METHOD oref->meth
Static method of a class CALL METHOD class=>meth
Its own instance method
Its own static method
CALL METHOD me->meth OR
CALL METHOD meth
27. Interfaces
• Interfaces extends scope of a class by adding
there own components to the class.
• Interfaces can only be defined in ‘PUBLIC
SECTION’ of class definition.
• Interfaces do not have an implementation part
or instance.
• Interfaces can be implemented by different
classes and its methods can be implemented
differently in each class.
• Interface components can be called directly
after assigning class reference to interface
reference. (<iref> = <cref> )
29. Local Interface
Interface <intf>
Method meth1
Endinterface
Class <class> definition
PUBLIC SECTION
Interfaces <intf>
METHODS : meth1
Endclass
Class <class> implementation
method <intf>~meth1
endmethod
method meth1
endmethod
Endclass
1. Define Interface
2. Declare in class
3. Implement Interface methods
30. Local Interface Alias
Aliases are short names given to methods of interfaces for easy access.
Interface <interface>
Method method
Endinterface
Class <class> definition
Interfaces <interface>.
Aliases : m1 FOR <interface>~method
Endclass
CALL METHOD <class>->interface~method.
CALL METHOD <class>->m1.
31. Interface Contd..
Both local and global interfaces can be accessedBoth local and global interfaces can be accessed
using the following waysusing the following ways
Class reference variable
CALL METHOD <cl>CALL METHOD <cl>->-><intf>~<meth><intf>~<meth>
• Using alias :
CALL METHOD <cl>CALL METHOD <cl>->-><meth><meth>
Interface reference variable
<intf>= <cref> (class reference)
CALL METHOD <intf>CALL METHOD <intf>->-><meth><meth>
32. Interface Contd..
• For static components only CONSTANT can be accessed
using interface name <intf>=><const>
• Other STATIC COMPONENTS cannot be accessed using
interface name but only through class.
Access static attribute
<class>=><intf~attr>
Access static method
CALL METHOD <class>=><intf~meth>