Chapter 01_Fundamentals of ABAP Objects 1.ppt

29
IBM Global Services Fundamentals of ABAP Object s | 11.01 March-2005 © 2005 IBM Corporation Fundamentals of ABAP Objects

description

ABAP obj

Transcript of Chapter 01_Fundamentals of ABAP Objects 1.ppt

IBM Global Services

Fundamentals of ABAP Objects | 11.01

March-2005 © 2005 IBM Corporation

Fundamentals of ABAP Objects

IBM Global Services

© 2005 IBM Corporation2 March-2005Fundamentals of ABAP Objects | 11.01

Objectives

The participants will be able to: Recognize the concept of Object Oriented Programming (OOP)

Identify the features of Object Oriented Programming

Recall the history of ABAP Object Oriented Programming

Advantages of ABAP OOP over conventional ABAP Procedural Programming

Analyze the basic building blocks of ABAP Objects

IBM Global Services

© 2005 IBM Corporation3 March-2005Fundamentals of ABAP Objects | 11.01

What is Object Oriented Programming (OOP) ?

The fundamental idea behind Object Oriented Programming (OOP) is to combine both data and the functions (methods) those operate on that data into a single unit. Such an unit is called Object, i.e. key principle of OOP is “Data controlling access to code”.

IBM Global Services

© 2005 IBM Corporation4 March-2005Fundamentals of ABAP Objects | 11.01

Advantages of Object Oriented Programming

Better Programming Structure

Real world entity can be modeled very well

Stress on data security and access

Data encapsulation and abstraction

Reduction in code redundancy

IBM Global Services

© 2005 IBM Corporation5 March-2005Fundamentals of ABAP Objects | 11.01

Features of Object Oriented Programming

Abstraction Modeling real world entities and processes in a more natural way.

Ecapsulation Hiding data and its related logic behind well defined interfaces.

Inheritance Reusing attributes and methods while allowing for specialization.

Polymorphism Simplifying by hiding varying implementations behind the same interface.

Code Reuse Same code can be reused multiple times by using inheritance.

IBM Global Services

© 2005 IBM Corporation6 March-2005Fundamentals of ABAP Objects | 11.01

History of ABAP Object Oriented Programming

SAP Basis Release 4.5 delivered the first version of ABAP Objects.

SAP Basis Release 4.6 delivered complete version of ABAP Objects by introducing ‘Inheritance’.

SAP Web Application Server 6.10/6.20 enhanced ABAP Objects with Friendship and Object Services.

IBM Global Services

© 2005 IBM Corporation7 March-2005Fundamentals of ABAP Objects | 11.01

ABAP as Hybrid Language

IBM Global Services

© 2005 IBM Corporation8 March-2005Fundamentals of ABAP Objects | 11.01

ABAP as Hybrid Language (Contd.)

IBM Global Services

© 2005 IBM Corporation9 March-2005Fundamentals of ABAP Objects | 11.01

Advantages of ABAP OOP over conventional ABAP Procedural Programming

ABAP Objects provides advance level of data encapsulation that improves the maintainability and stability of ABAP programs.

ABAP Objects provides instantiation of multiple instances of a single class.

ABAP Objects enhances code reuse through “Inheritance”.

ABAP Objects helps us to work with an object’s business logic through a standalone interface.

ABAP Objects makes it easy to incorporate event driven programming models.

ABAP Objects are more explicit, and therefore simpler to use.

ABAP Objects offers cleaner syntax and semantic rules.

ABAP Objects offers the only way to use new ABAP technology.

IBM Global Services

© 2005 IBM Corporation10 March-2005Fundamentals of ABAP Objects | 11.01

Advantages of ABAP OOP over conventional ABAP Procedural Programming (Contd.)

ABAP Objects provides advance level of data encapsulation that improves the maintainability and stability of ABAP programs.

ABAP Objects provides instantiation of multiple instances of a single class.

ABAP Objects enhances code reuse through “Inheritance”.

ABAP Objects helps us to work with an object’s business logic through a standalone interface.

ABAP Objects makes it easy to incorporate event driven programming models.

ABAP Objects are more explicit, and therefore simpler to use.

ABAP Objects offers cleaner syntax and semantic rules.

ABAP Objects offers the only way to use new ABAP technology.

IBM Global Services

© 2005 IBM Corporation11 March-2005Fundamentals of ABAP Objects | 11.01

Basic building blocks of OOP

Classes and Objects are the basic building blocks of Object Oriented Programming. When a real world entity is modeled into OOP world then it is known as Class, characteristics as attributes and functionality as methods.

Objects is an instance of a Class.

Example :

What are thecharacteristics of the box? (Attributes)

Inside color is blue (Private)

Outside color is white (Public)

What is the status of the box ? (Events)

The box is semi open

Functions of the box? (Methods)

It can store things

It can occupy space

IBM Global Services

© 2005 IBM Corporation12 March-2005Fundamentals of ABAP Objects | 11.01

Classes ( Global + Local )

Classes can be of two types:

Global Class (Created using class builder (SE24) and stored in class repository as Class pool)

Local Class (Created in any ABAP program)

Global vs. Local Classes

Global Classes Local Classes

Accessed from ? Any Program Only with in the Program where it is defined

Where store ? In the class repository In the program where it is defined

Tools required to create ?

Class builder (SE24) With ABAP editor (SE38)

Namespace ? Must begin with ‘Y’ or ‘Z’

Any

IBM Global Services

© 2005 IBM Corporation13 March-2005Fundamentals of ABAP Objects | 11.01

Declaring a Class (Local)

Classes are template for Objects.

This declares and defines a local class “test ”.

In ABAP program this belongs to Global Section.

Class definition cannot be nested.

Classes cannot be defined inside subroutines or function modules.

A class definition declares : Its components :

Attributes, Methods, Events.

The visibility of its components :

Public, Protected and Private.

A class declaration has two parts.

Definition Implementation

CLASS test DEFINITION.PUBLIC SECTION.{ Attributes, Methods, Events }PROTECTED SECTION.{ Attributes, Methods, Events }PRIVATE SECTION.{ Attributes, Methods, Events }ENDCLASS.

CLASS test IMPLEMENTATION.<class body>{Method implementation is done here}ENDCLASS.

IBM Global Services

© 2005 IBM Corporation14 March-2005Fundamentals of ABAP Objects | 11.01

Components of Class ( Instance + Static )

Instance components: DATA

For instance attributes

METHODS For instance methods

EVENTS For instance events

Static components: CLASS-DATA

For static attributes

CLASS-METHODS For static methods

CLASS-EVENTS For static events

CONSTANTS For constants

Instance components exist separately in each instance (object) of the class.

Static components only exist one per class and are valid for all instances of the class.

Static components are declared with the CLASS- * keywords.

To access instance components, instance component selector (->) is used.

To access static components, static component selector (=>) is used.

IBM Global Services

© 2005 IBM Corporation15 March-2005Fundamentals of ABAP Objects | 11.01

Visibility sections in a Class

All components of a class must belong to a visibility section. Components can be public, protected or private.

Public components form the external interface of the class – they are visible to all users of the class as well as to methods within the class and to methods of subclasses.

Protected components form the interface of the class to its subclasses they are visible to methods of the heirs of the class as well as to methods within the class.

Private components can only be used in the methods of the class itself.

IBM Global Services

© 2005 IBM Corporation16 March-2005Fundamentals of ABAP Objects | 11.01

Methods

CLASS c1 DEFINITION.

PUBLIC SECTION.

METHODS: do_something

IMPORTING ...i1 TYPE…

EXPORTING…e1 TYPE…

CHANGING …c1 TYPE…

EXCEPTIONS …en.

PRIVATE SECTION.

DATA: …

ENDCLASS.

CLASS c1 IMPLEMENTATION.

METHOD do_something.

ENDMETHOD.

ENDCLASS.

Methods are the functionality of a class , ABAP codes are written within a method to incorporate the functionality.

Methods are processing blocks with a parameter interface very similar to function modules.

Methods are of two types: Standard Methods.

e.g. METHODS meth.

Event handler methods:

METHODS meth FOR EVENT evt OF class.

This type of methods are written to trap events.

Methods are called with a CALL METHOD statement.

IBM Global Services

© 2005 IBM Corporation17 March-2005Fundamentals of ABAP Objects | 11.01

Constructors

METHODS constructor

IMPORTING …

EXPORTING …

CREATE OBJECT obj EXPORTING …

CLASS-METHOD class_constructor

Each class has one constructor. It is a predefined, public instance method of the class, with the name CONSTRUCTOR (or CLASS_CONSTRUCTOR for static constructor).

Constructors are special methods that produce a defined initial state of objects and classes.

Constructors are executed once for each instance. They are called automatically after you have created an instance of the class with the CREATE OBJECT statement.

Instance constructor

Static Constructor

IBM Global Services

© 2005 IBM Corporation18 March-2005Fundamentals of ABAP Objects | 11.01

Some more features of Class

CLASS class_name DEFINITION DEFERRED. This is used in forward referencing.

CLASS class_name DEFINITION LOAD. If the first access to a global class in a program is to its static components then explicit

Loading of the class definition is necessary. In release 6.40 this statement is not required.

CLASS class_name DEFINITION CREATE PUBLIC| PROTECTED | PRIVATE.

‘CREATE PUBLIC’ addition is provided automatically by compiler if no create addition is used.

The additions CREATE PROTECTED and CREATE PRIVATE allow you to control the instantiation of your class.

IBM Global Services

© 2005 IBM Corporation19 March-2005Fundamentals of ABAP Objects | 11.01

CLASS c1 DEFINITION.

PUBLIC SECTION.

DATA: int TYPE I VALUE ’10’.

METHODS display_int.

ENDCLASS.

CLASS c1 IMPLEMENTATION.

METHOD display_int.

WRITE / int.

ENDMETHOD.

ENDCLASS.

DATA : oref TYPE REF TO c1.

START-OF-SELECTION.

CREATE OBJECT oref.

WRITE / oref-> int.

CALL METHOD oref-> display_int.

Objects and Object references

Classes are the templates of objects; actual objects must be created and referenced to be of use (except for the static components of a class)

Reference variables (TYPE REF TO) contain references to objects- Users can only access instance objects through reference variables.

To use objects: Declare reference variables.

Create objects, assigning theirreferences.

Use the object components

IBM Global Services

© 2005 IBM Corporation20 March-2005Fundamentals of ABAP Objects | 11.01

Self- Reference

CLASS c1 DEFINITION.

PUBLIC SECTION.

DATA: int TYPE I VALUE ’10’.

METHODS display_int.

ENDCLASS.

CLASS c1 IMPLEMENTATION.

METHOD display_int.

DATA : int TYPE I VALUE ’20’.

WRITE:/ int,

ME->int.

ENDMETHOD.

ENDCLASS.

DATA : oref TYPE REF TO c1.

CREATE OBJECT oref.

CALL METHOD oref-> display_int.

If an objects internally needs to provide its own reference. For example to another object, it can use the local reference variable “ME”.

“ME” is predefined and always contains a reference to the address of its own object.

Note : “ME” is equivalent to “THIS” pointer in C++.

IBM Global Services

© 2005 IBM Corporation21 March-2005Fundamentals of ABAP Objects | 11.01

Multiple instantiation

CLASS c1 DEFINITION.

PUBLIC SECTION.

METHODS meth.

ENDCLASS.

CLASS c1 IMPLEMENTATION.

ENDCLASS.

DATA: oref1 TYPE REF TO c1,

oref2 TYPE REF TO c1.

START-OF-SELECTION.

CREATE OBJECT oref1, oref1.

Programs can instantiate multiple objects of the same class.

IBM Global Services

© 2005 IBM Corporation22 March-2005Fundamentals of ABAP Objects | 11.01

Deleting Objects

DATA: oref1 TYPE REF TO c1,

oref2 TYPE REF TO c2.

...

CREATE OBJECT oref1, oref2.

oref1 = oref2.

CLEAR oref1.

CLEAR oref2.

oref2

oref1

9999oref2

oref1

8888Object C2

Object C1

8888oref2

oref1

8888Object C2

Object C1

oref2

oref1

8888

oref2

oref1

Object C2

Object C1

Object C2

Object C1

IBM Global Services

© 2005 IBM Corporation23 March-2005Fundamentals of ABAP Objects | 11.01

Functional Methods

METHODS meth

IMPORTING…

RETURNING VALUE (r)…

CALL METHOD oref->meth

EXPORTING i1 = a1….in = an

RECEIVING r = a.

…oref->meth()…

…oref->meth(a)…

…oref->meth( i1 = a1….in = an)…

e.g.,

var = oref-> meth( i1 = a1….in = an).

Instead of CALL METHOD, functional methods can be performed in expressions.

A Functional method can have zero to many IMPORTING parameters and exactly one RETURNING parameter, that must be passed by value.

A Functional method can be instance method or it can be static method.

ConventionalMethod call

Method call specific to

Functional method

IBM Global Services

© 2005 IBM Corporation24 March-2005Fundamentals of ABAP Objects | 11.01

Pointer tables

DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c1, oref3 TYPE REF TO c1.

DATA: oref TYPE REF TO c1, oref_tab TYPE TABLE OF REF TO c1.

START-OF-SELECTION.…DO 3 TIMES. CREATE OBJECT oref. APPEND oref TO oref_tab.ENDDO.…LOOP AT oref_tab INTO oref. CALL METHOD oref->meth.ENDLOOP.

Pointer tables are used to store multiple instances of same class. This method reduces coding and more elegant against creating separate, separate object reference variables for storing every objects of the same class.

Reference variables are handled like any other data object with an elementary data type.

IBM Global Services

© 2005 IBM Corporation25 March-2005Fundamentals of ABAP Objects | 11.01

Dynamic Method calls

CLASS c1 DEFINITION.

PUBLIC SECTION.

METHODS: meth1,meth2.

DATA fld TYPE …

DATA oref TYPE REF TO c1.

CREATE OBJECT oref.

•Do something to assign meth1 or meth2 to fld at runtime.

fld = ‘METH1’ or ‘METH2’.

CALL METHOD oref->(fld).

Instance, self-reference, and static method can all be called dynamically; the class name for static methods can also be determined dynamically:

Variants:

- oref->(method)

- me->(method)

- class=>(method)

- (class)=>method

- (class)=>(method)

A method’s parameters can be passed dynamically using PARAMETER-TABLEand EXCEPTION-TABLE additions to the CALL METHOD statement.

IBM Global Services

© 2005 IBM Corporation26 March-2005Fundamentals of ABAP Objects | 11.01

Demonstration

Creating a local class with different components in different visibility sections and showing how to instantiate the class as well as how to access the instance and static components.

IBM Global Services

© 2005 IBM Corporation27 March-2005Fundamentals of ABAP Objects | 11.01

Practice

Creating a local class with different components in different visibility sections and showing how to instantiate the class as well as how to access the instance and static components.

IBM Global Services

© 2005 IBM Corporation28 March-2005Fundamentals of ABAP Objects | 11.01

Summary

Features of Object oriented programming are: Abstraction

Ecapsulation

Inheritance

Polymorphism

Code Reuse

Classes and Objects are the basic building blocks of Object Oriented Programming

When a real world entity is modeled into OOP world then it is known as Class, characteristics as attributes and functionality as methods.

Objects is an instance of a Class. Classes can be of two types:

Global Class (Created using class builder (SE24) and stored in class repository as Class pool)

Local Class (Created in any ABAP program)

IBM Global Services

© 2005 IBM Corporation29 March-2005Fundamentals of ABAP Objects | 11.01

Questions

What is Object Oriented Programming ?

What are the main advantages of Object Oriented Programming over Procedural Programming ?

What is a Class?

What is an Object?

Which transaction we use to maintain global class?

What is a constructor?

What are the various visibility sections present in a ABAP class?

What is the basic difference between static component and instance component?

Can we access the static component of a class by the object name instead of the class name?