Anda di halaman 1dari 35

Oops in ABAP

By
Pavani
Different approaches in Programming

Unstructured Programming.
Procedural Programming.
Object Oriented Programming.
Unstructured Programming

report zdemo_01.
DATA : sal type p decimals 2,
itax type p decimals 2, Characteristics
net_sal type p decimals 2 .

sal = 12000. Consists of only one main program.

IF sal lt 5000 . The program stands for a sequence of


itax = 0. commands which modify data that is global
ELSE. throughout the whole program.
itax = sal * '0.01'.
ENDIF.
net_sal = sal - itax.
write:/5 sal , itax , net_sal. Disadvantages

sal = 3500.
Difficult to manage once the program becomes
IF sal lt 5000 . large.
itax = 0.
Same sequence of statements are repeated at
ELSE.
itax = sal * '0.01'. multiple places, if they are needed at multiple
ENDIF. locations.
net_sal = sal - itax.
write:/5 sal , itax , net_sal.
Procedural Programming

report z_demo_01.
DATA : sal type p decimals 2 ,
itax type p decimals 2 ,
net_sal type p decimals 2.
sal = 12000. Programmer combines related sequences of
PERFORM sub_calc_tax USING statements into one single place, called
sal itax net_sal. procedure.
sal = 3500.
PERFORM sub_calc_tax USING  A procedure call is used to invoke the
sal itax net_sal. procedure.
After the sequence is processed, flow of
FORM sub_calc_tax USING P_SAL
P_ITAX P_NET_SAL. control proceeds right after the position where
IF p_sal lt 5000 . the call was made.
p_itax = 0.
ELSE.
p_itax = sal * '0.01'.
ENDIF.
p_net_sal = p_sal - p_itax.
ENDFORM.
Object oriented programming

Object oriented programming means new way


of organizing data and codes that increased
the control over the complexity of the
software development process.

In O-O Programming we take bottom-up


approach where we construct objects to
class,class to subsystems ,subsystems to
systems making project management easier.
Object Oriented Approach
– Key Features

Better Programming Structure


 Real world entity can be modeled very well
 Stress on data security and access
Data encapsulation and abstraction
ABAP Objects

ABAP Objects is fairly a new concept in R/3


release 4.0 . The term has two meanings.
An object-oriented enhancement of the ABAP
programming language

1. It stands for the entire ABAP runtime


environment.
2. It represents the object-oriented
extension of the ABAP language
Abap Objects …

ABAP Objects is a complete set of object-oriented


statements that has been introduced into the ABAP
language.

You can use ABAP Objects in existing programs,


and can also use "conventional" ABAP in new ABAP
Objects programs.

SAP Business Objects and GUI objects - already


object-oriented themselves - will also profit from
being incorporated in ABAP Objects.
OOPs Concepts

Object and class


Inheritance
Polymorphism
Encapsulation
Interfaces

Exception handling
Object

An object is a section of source code that


contains data and provides services.

 The data forms the attributes of the Object.


The services are known as Methods.

 The methods operate on data of the objects.


Class
A group of objects that share common
properties and relationships.

Objects grouped in a class have same


structure and behaviour.

Thus object is a instance of class.

From technical point of view, objects


are runtime instances of a class.
Structure of a Class

A class contains components

 Each component is assigned to a


visibility section

 Classes implement methods


Visibility of Class
Each class component has a visibility. Objects restrict
the visibility of their resources to other users.
In ABAP Objects the whole class definition is separated
into three visibility sections:
1.Public
2.Protected
3.Private
One can never change component visibility via inheritance.
Visibility of Classes…

PUBLIC

All the components declared in the public


section are accessible to all users of the class,
and to the methods of the class and any
classes that inherit from it. The public
components of the class form the interface
between the class and its users.
Protected Section:

Components placed in protected section are visible to


the children of the class(subclass) as well as within
the class

Private Section:

Components placed here are accessible by the class


itself.
Overview --- Class

Class C1 definition
Instances of Class C1
Types of Classes

Classes are of two types

1. Global Classes
2. Local Classes
Global Classes

Globalclasses and interfaces are defined


in CLASS BUILDER.

Transaction code is SE24.

Coding standard is ZCL_<class name>.

They are stored centrally in class pools in


the class library in the R/3 repository
Defining Local Classes
REPORT ZOOPS01 .
CLASS ZCL01 DEFINITION. Defined in the global area of a
PUBLIC SECTION. local program :-
data : w_num type i value 5. CLASS <class name>
methods : m1. DEFINITION.
ENDCLASS. …..
CLASS ZCL01 IMPLEMENTATION.
ENDCLASS.
METHOD M1.
WRITE:/5 'I am M1 in C1'. All the attributes , methods,
ENDMETHOD. events and interfaces are
ENDCLASS. declared here.
START-OF-SELECTION.
Cannot be declared inside a
DATA : oref1 TYPE REF TO ZCL011 .
subroutine/function module.
CREATE OBJECT : oref1.
write:/5 oref1->w_num. Class definition cannot be
CALL METHOD : oref1->m1 . nested.
Implementing Classes

REPORT ZOOPS01 . Local class in a program is implemented as


CLASS CL01 DEFINITION.
follows:-
PUBLIC SECTION. CLASS <class name>
IMPLEMENTATION.
data : w_num type i value 5.
methods : m1. …..
ENDCLASS. ENDCLASS.
CLASS CL01 IMPLEMENTATION. Methods used by the class are described
METHOD M1. here.
WRITE:/5 'I am M1 in C1'. A class can be implemented
ENDMETHOD.
At the end of the program( like
ENDCLASS.
subroutines).
START-OF-SELECTION.
DATA : oref1 TYPE REF TO CL011 After the class definition.
.
CREATE OBJECT : oref1.
If the latter is adopted, one must then assign
subsequent non-declarative statements
write:/5 oref1->w_num.
explicitly to a processing block, such as
CALL METHOD : oref1->m1 .
START-OF-SELECTION, so that they can be
accessed.
Global vs Local Classes

Global Class Local Class

Any program Only the program where it is


Accessed by defined.
In the Class Repository Only in the program where it is
Stored in defined.
Created using transaction Created using SE38
Created by SE24

Namespace Must begin with Y or Z Can begin with any character


Data Types for References

You define class references using the


... TYPE REF TO <class>

LIKE may not be used.

LIKE can be used for local data types.


Creating Objects

Before you can create an object for a class, you need to


declare a reference variable with reference to that class.

DATA: <obj> TYPE REF TO <class>

Once you have declared a class reference variable <obj> for a


class <class>, you can create an object using the statement

CREATE OBJECT <obj>.

This statement creates an instance of the class <class>, and


the reference variable <obj> contains a reference to the object.
INHERITANCE

The mechanism of deriving a new class from


an old class is called inheritance.
The concept of inheritance is based on the
reusability of the properties of the existing
classes by the newly created class.
The old class is called base class, and the
new class is called as derived class.
The derived class inherits some (or) all of
the traits from the base class.
TYPES OF INHERITANCE

1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchal inheritance
5. Hybrid inheritance
POLYMORPHISM

Polymorphism is a feature that allows a single name/operator to be


associated with different operations depending on the type of the
data passed.
(or)
Polymorphism means ‘one name ,multiple forms’.
Polymorphism is implemented using the function overloading,
Operator overloading and dynamic binding (virtual functions).

Polymorphism are two types :


1.Compile-time polymorphism.
2.Runtime polymorphism.
The concept of function overloading and operator overloading
come under compile-time polymorphism.
Function overloading :
The concept of function-overloading allows
multiple functions to share the same name with different
argument types.
using the concept of function overloading, we
can design a family of function with one function name but with
different argument lists.

Operator overloading :
The mechanism of providing the operators with
a special meaning for a data type is called operator loading.
Virtual functions :
Runtime polymorphism is achieved by using
virtual functions.
Virtual functions allows programmers to declare functions in a
base class, which can be defined in each derived class.
Class A
{
Public:
......................................
......................................

Virtual returntype fn_name(arguments).


{
…………………………………
………………………………..

}
}
Encapsulation / Data hiding :
This feature provides the facility to bind both the
data members and member functions of a single
programming construct.
Ex: Encapsulation is an example of ‘class’.
Interfaces

Interfaces are used to define a group of constants, variables


and Methods. Interfaces are just like classes, methods are
declared with out any implementation.
The implementation part of the methods are declared in
subclasses. Any number of classes can implement the same
interface.
Some rules to be remembered:
1. All methods in an interface are by default public.
2. An interface can extend any number of interfaces, but don’t
implement any interface.
3. A class can implement any number of interfaces but no
interfaces can be extended to a class.
EXCEPTION HANDLING

Exceptions refers to unusual conditions in a program that


causes the programs to fail (or) certain conditions that lead
errors.
The error handling mechanism is called exception handling.
Exceptions are classified into two groups:
1. Synchronous exceptions
2. Asynchronous exceptions
Synchronous Exceptions
The exceptions which occur during the
program execution , due to some fault in the
input-data (or) technique that is not suitable to
handle the current class of data .

Ex : errors such as out-of-range , overflow,


underflow.
Asynchronous exceptions

The exceptions caused by events (or)


faults unrelated to the program and beyond the
control of the program are called asynchronous
exceptions.

EX: errors such as keyboard interrupts, H/W


malfunctions, disk failures.
ADVANTAGES OF OOPS

 Complex software systems become easier to


understand

 Reduces the overall amount of maintenance


required.

 Allows you to reuse individual components.

 The amount of work involved in revising and


maintaining the system is reduced.
Thank You

Anda mungkin juga menyukai