Sei sulla pagina 1di 26

Topics to be covered on Day 1

Basic concepts of object orientation


Inheritance
Keywords Basics of object orientation
Inheritance, Casting and Interfaces
Interfaces
Casting

1
Topics to be covered on Day 2

Object oriented event concept


Class-based exceptions
Shared objects
Custom BAPI development
Consuming external web service
Basics of dynamic programming

2
ABAP

ABAP supports a hybrid programming model:

A object-oriented programming model that is based on classes and


interface of ABAP objects
A procedural programming model that is based on procedural calls and
system event handling
Both models are interoperable you can call classes from classic
procedures, and you can call classic procedures from methods.

3
Concepts of OOP

Abstraction
- Ability to reflect real-world processes
- real-world modeled in classes and mapped in objects
Encapsulation
- Implementation details are hidden behind interfaces.
- Ensure that the abstract representation of an object is used only in accordance
with its specification.
Inheritance
- Abstractions derived from existing ones.
Instantiation
- Enables you to create multiple instances of a class.

4
OOP ABAP

Class is the model or the template


Object is an instance of class
Local class is created within any ABAP program and only visible there
Global class is created with the Class Builder tool and visible in any
ABAP program

5
Keywords and Syntax

6
Class Definition & Implementation

CLASS class_name DEFINITION.


PUBLIC SECTION.
....
PROTECTED SECTION.
....
PRIVATE SECTION.

ENDCLASS.
CLASS class_name IMPLEMENTATION.
....
ENDCLASS.

7
Components of Class

Attributes
- The data objects within a class
- All data types of the ABAP type hierarchy can be used
- DATA statement is used for instance attributes
- CLASS-DATA for static attribute

Methods
- Processing block with a parameter interface
- Local data
- Instance method can access all attributes and
events
- Static method can access only static attributes and events

Events
Allow the objects of a class to publish its status. Other objects can then respond to the change in status.
1. Event trigger does not know the event handler.
2. Event trigger therefore does not initially know if the event will even have an effect.
8
Visibility Section

Public
- All components are public and can be addressed by all
subclasses, the class itself and other external classes.
Protected
- Components of this section are protected and can only be
addressed by the class itself and the subclasses
Private
- Can only be used in the methods of the class itself

9
Define Attribute

CLASS attributes DEFINITION.


PRIVATE SECTION.
DATA objectValue TYPE i.
CLASS-DATA objectCount TYPE i.
ENDCLASS.

10
Define Instance Method
CLASS class_name DEFINITION.
PUBIC SECTION.
CLASS-METHODS meth
IMPORTING .. Ii TYPE type
EXPORTING .. Ei TYPE type ..
CHANGING .. Ci TYPE type ..
EXCETPIONS .. Ei
ENDCLASS.

11
Components Methods

Two special methods


constructor
Implicitly called for each instantiation of a new object
Execute by runtime environment.

class constructor
First time a class is accessed

ABAP Objects does not provide any destructors for application development.

12
Functional Method

Functional method

Method with any number of input parameters but only one return value.
It can be used in operand positions for functions and expressions.

Syntax
IF obj->functional_method( ) = abap_true.
do some thing
ENDIF.

13
Event
Each method can trigger the events of its class
Methods can be declared as handler methods for events

CLASS raising_class DEFINITION.


PUBLIC SECTION.
EVENTS: raised_event.
ENDCLASS.

CLASS raising_class IMPLEMENTATION.


METHOD raising_method.
RAISE EVENT raised_event.
ENDMETHOD.
ENDCLASS.

14
Event Handler class
CLASS handler_class DEFINITION.
PUBLIC SECTION.
METHODS: handler_method FOR EVENT
raised_event OF raising_class.
ENDCLASS.

CLASS handler_class IMPLEMENTATION.


METHOD handler_method.
some logics here
ENDMETHOD.
ENDCLASS.

SET HANDLER
handler_instance->handler_method
FOR ALL INSTANCES.

15
Define Object Reference Variables

To create and access an object, object references in reference variables are


needed.

Define Object Ref Variable


DATA ref TYPE REF TO class
Instantiating
Syntax:
CREATE OBJECT ref.

16
Method Call
Call instance method:
Syntax:
CALL METHOD oref->get_data.

Call class method:


Syntax:
CALL METHOD class=>get_data.

Interface Method Call:


CLASS lcl_company_employees IMPLEMENTATION.
METHOD lif_employee~add_employee.

17
Exception Handling

Exceptions are events that arise during the execution of an ABAP program
at which the program is interrupted, because it is not possible to
continue processing the program in a meaningful way.

Exceptions are either treatable or untreatable.

18
Inheritence
Re-implement the method of supper class

In Class declaration part:


Syntax:
METHODS meth REDEFINITION.

Must always take the definition of the superclasses into account


METHOD constructor.
CALL METHOD super->constructor.
ENDMETHOD.

19
Final Class
When needed to protect a class or individual method from uncontrolled specialization
Syntax:
CLASS class DEFINITION FINAL.

ENDCLASS

Final Method
Syntax:
CLASS class DEFINITION FINAL.
PUBLIC SECTION.
METHODS: method FINAL.
ENDCLASS

20
Interface & Abstract class

What is an interface ?
It is an entity just like a class but interface cant have implementation

Interface components are always public

Interfaces are incorporated solely in the public visibility section of a class.


Simple Interface

INTERFACE zif_order.
METHODS: set_order_data IMPORTING iv_order_Data TYPE STRING.
METHODS: create_order.
ENDINTERFACE.
*
CLASS zcl_sales_order DEFINITION.
PUBLIC SECTION.
INTERFACES: zif_order.
ENDCLASS.

21
Interface & Abstract class

What is an abstract class?


Abstract Class is a special kind of class which cant be instantiated
Abstract class should at least contain one abstract method
CLASS zcl_base_functions DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: set_my_name ABSTRACT IMPORTING iv_text TYPE string .
METHODS: write_name.
PRIVATE SECTION.
DATA: my_name TYPE string.
ENDCLASS. "zcl_base_functions DEFINITION
*
CLASS zcl_base_functions IMPLEMENTATION.
METHOD write_name.
ENDMETHOD. "write_name
ENDCLASS. "zcl_base_functions IMPLEMENTATION

22
Interface & Abstract class

Differences !!
Multiple Inheritance
Adding new method
Default behavior
Visibility

23
Casting

Types:
Narrowing cast
Widening Cast

24
Narrowing Cast

What is narrowing cast?

The assignment of a subclass instance to a reference variable of the type


"reference to super class" is described as a narrowing cast, because we are
switching from a more detailed view to a one with less detail.

Super class: vehicle (contains general methods)


Sub class: truck (contains more specific methods)

DATA: REF_TRUCK TYPE REF TO TRUCK.


CREATE OBJECT REF_TRUCK.

Narrowing cast:
Declare a variable with reference to the super class.
DATA: REF_VEHICLE TYPE REF TO VEHICLE.
REF_VEHICLE = REF_TRUCK.

25
Widening Cast

What is Widening cast ?

When we assign the instance of the Super class to the Subclass, than it is
called the Widening Cast, because we are moving to the More Specific View
from the Less specific view.
Special assignment with the casting operator ?=

Super class: vehicle (contains general methods)


Sub class: truck (contains more specific methods)

DATA: REF_TRUCK TYPE REF TO TRUCK,


REF_VEHICLE TYPE REF TO VEHICLE.
CREATE OBJECT : REF_TRUCK, REF_VEHICLE.

Widening cast:
REF_TRUCK ?= REF_VEHICLE .

26

Potrebbero piacerti anche