55
ABAP Objects Intro / Refresher Preparation for Agile Software Engineering Course Juergen Heymann, Nena Raab CPO Software Engineering

ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

  • Upload
    others

  • View
    37

  • Download
    8

Embed Size (px)

Citation preview

Page 1: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

ABAP Objects Intro / Refresher Preparation for Agile Software Engineering Course

Juergen Heymann, Nena Raab

CPO Software Engineering

Page 2: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 2

Purposes & Format

Purpose: Prepare for AgileSE Course ABAP

Fast Introduction / Refresher of Object Oriented ABAP (OO ABAP)

Learn how to develop OO ABAP source code based

Format: Self-study course with set of modules with

Short video intro to concept

Programming exercise

Short video on sample solution

Modules build on each other

Page 3: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 3

Learning Modules

From Functions to Objects

Object Basics

OO Key Concept: Encapsulation

Module 1

Inheritance/Subclassing

Module 5

Local and Global Classes (SE24)

Class Attributes and Methods

Design Pattern: Factory Method

Module 2

OO Key Concept: Polymorphism

Interfaces

Various Uses of Interfaces

Module 4

Introduction to UML and object modeling

Module 3

Page 4: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 4

Assumptions / Prerequisites

The course is developed from the perspective of an experienced ABAP

developer. Assumptions:

You are familiar with ABAP programming (non-OO) and the tools

You have access to a system for the exercises

Positioning

The course is intended as quick refresher / fast intro to OO programming in ABAP.

For deeper study, read e.g. …

Page 5: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

ABAP Objects Refresher

Module 1:

From Functions to Objects

Object Basics

OO Key Concept: Encapsulation

Page 6: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 6

From Functions to Objects 1

Characteristics of the Procedural Programming Model

Notes:

You can encapsulate data (access) by functions

All data is accessible to all functions

data data data

data data

Separation of data and function

• Data represents state of ‘real world objects’

• Functions implement behavior of 'real world objects

Usually direct access to data (non-encapsulated)

Modularization is used to encapsulate functions

function function function

form

routine

function form

routine

function

form

routine

Page 7: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 7

From Functions to Objects 2

Multiple Instances

When you want 'multiple instances' you use internal- or DB-tables

Row in table holds the data for one instance

e.g. order items table

Code works on 'the current instance' = a 'current row of the table'

address 'current instance' by field-symbol or by taking it into a work-area

hard to address multiple 'current instances' you work with

hard to have deeper structures

Notes:

New instance append to table

Row identified by a 'key'

Cars

ID name

speed

1 Porsche 911 170

2 VW Bug 1300 120

code

code

code

code

Page 8: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 8

From Functions to Objects 3

Objects – a Real World Object Representation

What changes going to Objects?

Putting Code and Data together

Instance specific Data and code that 'belongs

together' is put together

Instance specific Data Instance Attributes

Functions/Forms Methods

Methods work only on their object's data

(keep their object data consistent)

Data is usually hidden ('private') and methods are

public

Each Object Instance is created separately

'CREATE OBJECT' instead of 'append line'

method

method

public private

set_name

set_speed speed

Class Car

set_name

set_speed

Porsche 911

170

Object 1:

set_name

set_speed

VW Bug 1300

120

Object 2:

Class: Code + structure to define objects

Object: Concrete instance of a class

Terminology

data

name

Page 9: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 9

Implementation

Definition

What the Code Looks Like Definition – Implementation - Client

Definition of a class (here local in program)

Definition of attributes of the class

Definition of the methods with parameters

Implementation of the methods

DATA … TYPE REF TO are reference variables that

can hold references to objects (of a certain type)

the CREATE OBJECT statement creates a

new instance of a class

methods are called on the object to

change the data of this object

Client code (here: main program)

Page 10: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 10

From Functions to Objects 4

Instances and References

Instances / Objects

A new object is created by "CREATE OBJECT objref"

(objref = name of reference variable)

You can have many objects/instances of a class.

(they live in memory)

Object References

Objects are 'accessed' via a reference that 'points to'

the object instance

References are stored in reference variables of TYPE REF TO …

Reference variables are typed so that only references

of objects of that specified type can be stored there

Reference values can be copied and cleared

car3 = car1 only copies the reference, not the object!

Garbage Collection

Objects no longer reachable by any reference are

'reclaimed' (instance discarded, memory reused)

data car1 type ref to lcl_car.

data car2 type ref to lcl_car.

data car3 type ref to lcl_car.

create object: car1, car2, car3.

car1

car2

car3

car3 = car1. "– copy reference

clear car2. "– clear reference

car1

X car2

car3

Page 11: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 11

Definition

OO Key Concept: Encapsulation Public and Private

Motivation

You want to encapsulate/hide internal details

such as data and internal operations

You want to provide a well-defined, simple and

consumable public interface to the client

The private object components (data, methods)

can be restructured without the need to change

clients of the object

Some Principles

Keep your data 'private‘ since private data can

only be modified within the class

The object itself ensures the consistency of its

data

Page 12: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 12

Definition

Special Method: Constructor

A special method of a class is the

CONSTRUCTOR

This method is called implicitly by the

system when the object is created

Arguments can be defined that then have to

be passed to the "CREATE OBJECT"

statement

Constructors are optional

Constructors are usually used to 'initialize'

an object to a consistent state

Page 13: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 13

Exercise 1: Basic OO Concepts and ABAP OO Syntax

Exercise: Modeling different types of cars with their fuel consumption

Create a program/report (in $TMP) that you will write your local classes in

Further Instructions

Define a local class for car e.g. lcl_car

Define attributes of car (private)

– name of car (e.g. 'Porsche Carrera 911')

– capacity of gas tank in liters

– fuel consumption - liters per 100 km the car consumes

– current tank level in liters (e.g. 10,2 liters)

Define methods (public)

– constructor: sets the attributes

name, capacity and fuel consumption

– refuel: fill up the gas tank returning the number of liters used

– drive_distance: reduces the gas in the tank

– get_current_tank_level: returns the current tank level

in percentage (e.g. “35% filled”)

Create car objects in your main program

Create 3 different cars (instances) with 3 different tank

capacities and gas consumptions.

Call methods on the objects to drive various distances

Call methods on the objects to ask for the current tank level

and output these values (use WRITE statements)

References

ABAP Objects syntax reference by sample code

ABAP Workbench Shortcuts

CTRL + Space Code Completion

SHIFT + F1 Format Source

F2 Check Source

F3 Activate Source

F9 Select all Objects in Activation List

Page 14: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 14

Review of Exercise 1

Content

Look at the sample solution

Show objects in debugger

lcl_car

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

+ constructor( )

+ refuel( )

+ drive_distance( )

+ get_curr_tank_level( )

Page 15: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

ABAP Objects Refresher

Module 2:

Local and Global Classes (SE24)

Class Attributes and Methods

Design Pattern: Factory Method

Page 16: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 16

Local and Global Classes

Local Classes

Can be defined within any source unit

e.g. report, function, global class …

Are only visible in that source unit

same class name can be used in many

source units

Naming: Usually start with LCL_...

Global Classes

Are globally visible in the system like any

other function module, DDIC element, …

Are edited with the Class Builder

transaction (SE24)

Naming: Usually start with

</namespace/>CL_...

Using global classes: like local classes

Prog1

Prog2

lcl_car

lcl_car

lcl_truck

Page 17: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 17

Demo SE24

Page 18: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 18

Static (Class Level) Attributes

Class vs. Instance Level

Instance Attributes exist per instance

– defined with "DATA“

Class attributes exist only once per class,

shared for all instances = 'static'

– defined with "CLASS-DATA"

Usage

Suppose you want to know the 'number of

cars created'. Where would you do that?

Increment where? How guarantee that it is

incremented only once per instance?

Constructor of 'create method' …

CL_CAR

class-data:

no_of_instances = 6

separate instances and its data

name = Porsche

911

name = VW Bug

1300

Page 19: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 19

Definition

Using Static (Class Level) Attributes

Example: Instance Counter

Counter exists only once for the class, i.e. is

shared by all instances

Counter is public read-only

Constructor increments the count

Access static attributes with "=>"

Access class attributes using static operator "=>“

e.g. class_name=>attribute_name

Static attribute

Page 20: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 20

Definition

Using Static (Class Level) Methods

To access non-public class attributes

you need class methods

Methods exist on class level

Defined with "CLASS-METHODS"

Class methods have no access to instance

attributes

A static method cannot be overwritten by

a subclass

Call static methods using "=>"

access class methods using static operator "=>“

e.g. class_name=>method_name

Page 21: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 21

Definition

Example: Factory Method Using Create Method instead of Constructor

Motivation

• The class itself wants to control the object

instantiation, not the client

• E.g. ensure that object exists only once

or once per key

• The client wants always the same instance

without holding the instance

Other Benefits • Client and class creation are loosely coupled

• You can return the object of a generic type see

interfaces

• You can use where-used-list to find the

object creators

Example: Introduce "factory method“

If a class method is used to create an

object then you must set 'create private'

(no 'create object' outside class)

factory method

Page 22: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 22

Exercise 2: Class Methods and Data Implement Instance Management

Exercise: Implement a factory method to create an object

Ensure that an object with a given ID (='key') exists only once

Further Instructions

Copy the code from exercise 1 and change it in the following way:

Replace the constructor by a static class method, e.g. "get_instance“ (keeping the parameter)

Change class definition to “create private“ to prevent the creation of objects outside the class.

Fix the appearing syntax errors (replace all CREATE OBJECTs with calls to get_instance)

Remember in the creation method which cars were created (by name key):

use a static internal table with row type [string, type ref to lcl_car]

If get_instance() is called again with the same key (e.g. 'VW Golf'), return the existing instance.

Page 23: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 23

Review of Exercise 2

Content

Look at sample solution

Show objects in debugger

lcl_car

- inst_table

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

+ get_instance( )

+ refuel( )

+ drive_distance( )

+ get_curr_tank_level( )

Page 24: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

ABAP Objects Refresher

Module 3:

Introduction to UML and object modeling

Page 25: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 25

UML (Unified Modeling Language): Class Syntax

Page 26: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 26

UML: Class Diagram Static View of Classes and Their Relationships ('Associations')

Aggregation relation (= 'contains' 'is-part-of')

Generalization

Page 27: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 27

UML: Association Syntax

Page 28: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 28

UML: Sequence Diagram Who Calls Who and in What Order

Delegation Principle Visualized

Page 29: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 29

CRC Modeling Classes – Responsibilities – Collaborations

CRC Modeling Method

Similar to UML classes but more abstract

Use cards with

class name

responsibilities of class (bullet points: data

held, methods / operations offered)

collaborations (names of other classes it

collaborates with)

CRC cards are usually moved around to

show relationships by location and arrows

<Class Name>

<Responsibilities> <Collaborations>

Student

has address

knows professors

enrolls in class

drop class

take exam

professor

classes

exam scores

Page 30: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

ABAP Objects Refresher

Module 4:

OO Key Concept: Polymorphism

Interfaces

Various uses of interfaces

Page 31: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 31

OO Key Concept: Polymorphism Depend Upon Abstractions, not on Concrete Implementations

Motivation

• Support Polymorphism = 'having multiple forms‘

• Several implementations for the same abstract interface

examples in the non-OO world: BAdIs or Customizing with callback function modules

• You want to offer (future) extensibility

Some Examples

Imagine 'generic save' method on each object

Imagine an archive manager that needs data

in a specific format of its objects to be archived

Imagine a refuel method for all kinds of vehicles

any_storable_obj->save_your_data_to_db( ).

archive_manager->archive( archivable_obj ).

any_vehicle>refuel( 45,2 ).

Page 32: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 32

Interface Implementation

• A class can implement many interfaces

Interface Definition

… is like the definition of a class

• It contains elements like types, methods, …

• But all elements are public

Specify class when using interface reference

Object reference can be 'converted' to an

interface reference type that it implements (cast)

Client

• Interface references allow access to all interface elements

i.e. regardless of implementing class

‘full name’ of element: <if_name>~<elem_name>

Polymorphism: Interfaces Definition – Implementation – Client

Page 33: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 33

Exercise 3: Interfaces for Generic Object Access

Exercise: Access different vehicle types via a common interface

Further Instructions

Copy the code from exercise 2 and change it in the following way:

Define an interface 'lif_vehicle' with methods

– Copy the methods refuel, drive_distance and get_current_tank_level into the interface (from exercise 1)

Define and implement a class ‘lcl_truck’ that implements the 'lif_vehicle' interface.

Let the ‘lcl_car’ class implement the 'lif_vehicle‘ interface

– Remove methods contained in the interface from the class definition

– Rename the method names of the implementation

In the report:

– Create several trucks in addition to the cars and drive some distances

– Collect references to all vehicles in an internal table using the interface type

– Loop over the table, check that the current tank level is less than 100% and then refuel all of them

Page 34: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 34

Review of Exercise 3

Content

Look at sample solution

Show objects in debugger

lcl_car

- inst_table

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

+ get_instance( ) …special car methods

lcl_truck

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

…special truck methods

lif_vehicle

<<interface>>

get_current_tank_level( )

refuel( )

drive_distance( )

Page 35: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 35

Various Uses of Interfaces Some Purposes

1. Generic interface / access to objects from frameworks

Which data is required from the client in which format

Other interactions with the client e.g. notifications, call backs

Example: Each class whose objects need to be archived implement the 'archiving interface'.

2. Decouple implementation from interface

• Exchange the implementing class

3. Decoupling during development

• E.g. you need to 'use' / call a class that is built by another developer but is not there yet

• You agree together on the interface. Then the 'caller' can use the interface while the developer has

time to implement the interface.

Page 36: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

ABAP Objects Refresher

Module 5:

Polymorphism: Inheritance

Page 37: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 37

Polymorphism: Inheritance/Subclassing/Derivation Class Hierarchy

Motivation

• Inheritance is a means to implement conceptual

hierarchies and/or share common code between

classes

• A super class implements the characteristics that are

common to all its subclasses

• Subclasses are specializations that can do more

Inheritance

• The subclass inherits the data and behavior

from its superclass: it can access/redefine all

components of its super class

Technical Restriction

Class/static components cans not be redefined

A class can only inherit from one class

lcl_vehicle

+ refuel()

+ drive_distance()

Page 38: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 38

Subclass

• Subclass can define further attributes and methods

• Subclass can access all components of its super class

that are protected or public

• Subclass can redefine all methods of its super class

that are protected or public

• Subclass can call the implementation of the parent class

Parent Class

• The parent class must be opened up for derivation:

the final flag has to be removed

• New visibility level protected:

components visible in class and subclasses

Polymorphism: Inheritance/Subclassing/Derivation Parent Class - Subclass

Page 39: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 39

Exercise: Move common code to a super-class

Further Instructions Copy code from exercise 3 to new program

Create class lcl_vehicle that implements the lif_vehicle interface

(which contains the three methods…)

Move data and implementation of interface methods from lcl_car to

vehicle level and make it protected so that the subclasses can

access them

Define lcl_car and lcl_truck as subclasses of vehicle (note: the

existing interface method implementations are no longer needed

since they are implemented in the super-class vehicle)

The truck can do more: It has a reserve tank that has its own

capacity and must be refueled as well and needs its own get tank

level method

– You can call the refuel() method of the vehicle to get the main tank

refueled

Create a few vehicles and drive some distances using the vehicle

interface (table) as in exercise 3

lcl_car

- inst_table

+ get_instance( )

lcl_truck

reserve_tank_level

+ refuel( ): redefinition

+ get_reserve_tank_level()

lcl_vehicle

<<abstract>>

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

lif_vehicle

<<interface>>

get_current_tank_level( )

refuel( )

drive_distance( )

Exercise 4: Use Inheritance

Page 40: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 40

Review of Exercise 4

Content

Look at sample solution

Show objects in debugger

Page 41: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 41

Polymorphism: Inheritance and Delegation Additional Notes

• Inheritance entails a very strong coupling between classes and should not be misused just

as a convenient means to share implementation code!

• Key Rule: A subclass must do the same (or more) as its parent / super class

• Liskov Substitution Principle: “Behavior 'promises' by the ‘interface’ of a class C must be satisfied

by all its subtypes S’

• Typical violation: Square class is subclass of Rectangle? NO: Subclass ≠ Subset!

Rectangle client code will expect to set width and height independently

• Therefore it is wise to prefer delegation over inheritance whenever possible,

i.e. have a class beside that implements the common parts

Cparent

S¹ S²

Cany¹ S¹

S² Cany²

Inheritance Delegation

Page 42: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

ABAP Objects Syntax

Page 43: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 43

ABAP Objects Sample Code as Syntax Reference

QT3: ABAP_ASE_OO_INTRO_LANG_REF

Definition Main Program

Implementation

Page 44: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

Sample Solutions

Page 45: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 45

Exercise 1 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P1_SOL Implementation

Definition Implementation

Page 46: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 46

Exercise 1 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P1_SOL

Main Program

Back to Exercise

lcl_car

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

+ constructor()

+ refuel( )

+ drive_distance( )

+ get_curr_tank_level( )

Page 47: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 47

Exercise 2 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P2_SOL

Definition Implementation

Page 48: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 48

Exercise 2 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P2_SOL

Main Program

Back to Exercise

lcl_car

- inst_table

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

+ get_instance( )

+ refuel( )

+ drive_distance( )

+ get_curr_tank_level( )

Page 49: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 49

Exercise 3 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P3_SOL

New Interface New Truck Class – Implementation

New Truck Class – Definition

Page 50: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 50

Exercise 3 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P3_SOL

Changed Car Class – Implementation

Changed Car Class – Definition

Page 51: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 51

Exercise 3 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P3_SOL

Main Program

Back to Exercise

lcl_car

- inst_table

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

+ get_instance( ) …special car methods

lcl_truck

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

…special truck methods

lif_vehicle

<<interface>>

get_current_tank_level( )

refuel( )

drive_distance( )

Page 52: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 52

Exercise 4 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P4_SOL

New Abstract Vehicle Class Changed Car Class

Page 53: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 53

Exercise 4 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P4_SOL

Changed Truck Class

lcl_car

- inst_table

+ get_instance( )

lcl_truck

reserve_tank_level

+ refuel( ): redefinition

+ get_reserve_tank_level()

lcl_vehicle

<<abstract>>

- name

- tank_capacity

- fuel_consumption_per_km

- curr_tank_level

lif_vehicle

<<interface>>

get_current_tank_level( )

refuel( )

drive_distance( )

Page 54: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2011 SAP AG. All rights reserved. 54

Exercise 4 (QT3 System)

QT3: ABAP_ASE_OO_INTRO_P4_SOL

Main Program

Back to Exercise

Page 55: ABAP Objects Intro / Refresher - All Questions - SAP …...Learn how to develop OO ABAP source code based Format: Self-study course with set of modules with Short video intro to concept

© 2013 SAP AG. All rights reserved.

© 2011 SAP AG. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice.

Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.

Microsoft, Windows, Excel, Outlook, PowerPoint, Silverlight, and Visual Studio are registered trademarks of Microsoft Corporation.

IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, z10, z/VM, z/OS, OS/390, zEnterprise, PowerVM, Power Architecture, Power Systems, POWER7, POWER6+, POWER6, POWER, PowerHA, pureScale, PowerPC, BladeCenter, System Storage, Storwize, XIV, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, AIX, Intelligent Miner, WebSphere, Tivoli, Informix, and Smarter Planet are trademarks or registered trademarks of IBM Corporation.

Linux is the registered trademark of Linus Torvalds in the United States and other countries.

Adobe, the Adobe logo, Acrobat, PostScript, and Reader are trademarks or registered trademarks of Adobe Systems Incorporated in the United States and other countries.

Oracle and Java are registered trademarks of Oracle and its affiliates.

UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.

Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems Inc.

HTML, XML, XHTML, and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.

Apple, App Store, iBooks, iPad, iPhone, iPhoto, iPod, iTunes, Multi-Touch, Objective-C, Retina, Safari, Siri, and Xcode are trademarks or registered trademarks of Apple Inc.

IOS is a registered trademark of Cisco Systems Inc.

RIM, BlackBerry, BBM, BlackBerry Curve, BlackBerry Bold, BlackBerry Pearl, BlackBerry Torch, BlackBerry Storm, BlackBerry Storm2, BlackBerry PlayBook, and BlackBerry App World are trademarks or registered trademarks of Research in Motion Limited.

Google App Engine, Google Apps, Google Checkout, Google Data API, Google Maps, Google Mobile Ads, Google Mobile Updater, Google Mobile, Google Store, Google Sync, Google Updater, Google Voice, Google Mail, Gmail, YouTube, Dalvik and Android are trademarks or registered trademarks of Google Inc.

INTERMEC is a registered trademark of Intermec Technologies Corporation.

Wi-Fi is a registered trademark of Wi-Fi Alliance.

Bluetooth is a registered trademark of Bluetooth SIG Inc.

Motorola is a registered trademark of Motorola Trademark Holdings LLC.

Computop is a registered trademark of Computop Wirtschaftsinformatik GmbH.

SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork, SAP HANA, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries.

Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company.

Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase Inc. Sybase is an SAP company.

Crossgate, m@gic EDDY, B2B 360°, and B2B 360° Services are registered trademarks of Crossgate AG in Germany and other countries. Crossgate is an SAP company.

All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary.

The information in this document is proprietary to SAP. No part of this document may be reproduced, copied, or transmitted in any form or for any purpose without the express prior written permission of SAP AG.