Beruflich Dokumente
Kultur Dokumente
Learning Modules
Module 4
Module 1
Module 5
Module 2
Inheritance/Subclassing
Module 3
Assumptions / Prerequisites
Positioning
The course is intended as quick refresher / fast intro to OO programming in ABAP.
For deeper study, read e.g.
function
function
function
function
form
routine
form
routine
form
routine
Notes:
You can encapsulate data (access) by functions
All data is accessible to all functions
data
data
data
data
data
2011 SAP AG. All rights reserved.
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
Cars
code
ID
code
code
name
speed
Porsche 911
170
VW Bug 1300
120
Notes:
New instance append to table
Row identified by a 'key'
code
method
private
public
method
data
Class Car
set_name
name
set_speed
speed
Object 1:
Object 2:
set_name
Porsche 911
set_name
VW Bug 1300
set_speed
170
set_speed
120
Implementation
Implementation of the methods
car1
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!
car2
car3
car3 = car1.
clear car2.
car1
car2
Garbage Collection
car3
10
Motivation
Definition
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
11
Definition
12
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
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
refuel: fill up the gas tank returning the number of liters used
CTRL + Space
Code Completion
SHIFT + F1
Format Source
F2
Check Source
F3
Activate Source
F9
13
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( )
14
Prog1
lcl_car
lcl_truck
Prog2
lcl_car
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
16
Demo SE24
17
CL_CAR
no_of_instances = 6
name = Porsche
911
Usage
class-data:
18
Definition
19
Definition
20
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
Other Benefits
factory method
21
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.
22
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( )
23
25
26
UML: Association
Syntax
27
28
CRC Modeling
Classes Responsibilities Collaborations
<Class Name>
<Responsibilities> <Collaborations>
class name
responsibilities of class (bullet points: data
held, methods / operations offered)
collaborations (names of other classes it
collaborates with)
Student
has address
knows professors
enrolls in class
drop class
take exam
professor
classes
exam scores
29
Motivation
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 ).
31
Polymorphism: Interfaces
Definition Implementation Client
Interface Definition
Client
Interface references allow access to all interface elements
i.e. regardless of implementing class
Specify class when using interface reference
32
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
33
Review of Exercise 3
Content
lif_vehicle
<<interface>>
get_current_tank_level( )
refuel( )
drive_distance( )
lcl_car
- inst_table
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
lcl_truck
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
special truck methods
+ get_instance( )
special car methods
34
1.
Example: Each class whose objects need to be archived implement the 'archiving interface'.
2.
3.
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.
35
Polymorphism: Inheritance/Subclassing/Derivation
Class Hierarchy
Motivation
lcl_vehicle
+ refuel()
+ drive_distance()
Inheritance
Technical Restriction
Class/static components cans not be redefined
A class can only inherit from one class
37
Polymorphism: Inheritance/Subclassing/Derivation
Parent Class - Subclass
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
Subclass
Subclass can define further attributes and methods
Subclass can access all components of its super class
that are protected or public
38
get_current_tank_level( )
refuel( )
drive_distance( )
lcl_vehicle
<<abstract>>
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
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()
39
Review of Exercise 4
Content
Look at sample solution
Show objects in debugger
40
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
Cparent
S
Inheritance
Cany
Cany
Delegation
41
QT3: ABAP_ASE_OO_INTRO_LANG_REF
Main Program
Implementation
43
Sample Solutions
Implementation
QT3: ABAP_ASE_OO_INTRO_P1_SOL
Implementation
45
QT3: ABAP_ASE_OO_INTRO_P1_SOL
lcl_car
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
+ constructor()
+ refuel( )
+ drive_distance( )
+ get_curr_tank_level( )
Back to Exercise
2011 SAP AG. All rights reserved.
46
QT3: ABAP_ASE_OO_INTRO_P2_SOL
Implementation
47
QT3: ABAP_ASE_OO_INTRO_P2_SOL
Main Program
- inst_table
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
+ get_instance( )
+ refuel( )
+ drive_distance( )
+ get_curr_tank_level( )
Back to Exercise
2011 SAP AG. All rights reserved.
48
QT3: ABAP_ASE_OO_INTRO_P3_SOL
49
QT3: ABAP_ASE_OO_INTRO_P3_SOL
50
QT3: ABAP_ASE_OO_INTRO_P3_SOL
Main Program
get_current_tank_level( )
refuel( )
drive_distance( )
lcl_car
- inst_table
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
lcl_truck
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
special truck methods
+ get_instance( )
special car methods
Back to Exercise
2011 SAP AG. All rights reserved.
51
QT3: ABAP_ASE_OO_INTRO_P4_SOL
52
QT3: ABAP_ASE_OO_INTRO_P4_SOL
lcl_vehicle
<<abstract>>
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
lcl_car
- inst_table
+ get_instance( )
lcl_truck
reserve_tank_level
+ refuel( ): redefinition
+ get_reserve_tank_level()
53
QT3: ABAP_ASE_OO_INTRO_P4_SOL
Back to Exercise
54
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.
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.
HTML, XML, XHTML, and W3C are trademarks or registered trademarks of W3C,
World Wide Web Consortium, Massachusetts Institute of Technology.
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.
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.
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.