Professional Documents
Culture Documents
Module 1 Module 4
Module 2 Module 5
Module 3
Positioning
The course is intended as quick refresher / fast intro to OO programming in ABAP.
For deeper study, read e.g. …
Notes:
You can encapsulate data (access) by functions
All data is accessible to all functions
data
data
Cars
code ID name speed
code
Notes:
1 Porsche 911 170 New instance append to table
code 2 VW Bug 1300 120 Row identified by a 'key'
code
Implementation
Implementation of the methods
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
Further Instructions
Define a local class for car e.g. lcl_car Create car objects in your main program
Define attributes of car (private) Create 3 different cars (instances) with 3 different tank
capacities and gas consumptions.
– name of car (e.g. 'Porsche Carrera 911')
Call methods on the objects to drive various distances
– capacity of gas tank in liters
Call methods on the objects to ask for the current tank level
– fuel consumption - liters per 100 km the car consumes and output these values (use WRITE statements)
– current tank level in liters (e.g. 10,2 liters)
Define methods (public) References
– constructor: sets the attributes
name, capacity and fuel consumption ABAP Objects syntax reference by sample code
– refuel: fill up the gas tank returning the number of liters used
– drive_distance: reduces the gas in the tank ABAP Workbench Shortcuts
– get_current_tank_level: returns the current tank level CTRL + Space Code Completion
in percentage (e.g. “35% filled”)
SHIFT + F1 Format Source
F2 Check Source
F3 Activate Source
F9 Select all Objects in Activation List
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( )
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
CL_CAR
Class vs. Instance Level
Instance Attributes exist per instance no_of_instances = 6 name = Porsche
911
– defined with "DATA“
Class attributes exist only once per class,
shared for all instances = 'static'
– defined with "CLASS-DATA"
name = VW Bug
1300
Definition
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
factory method
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
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.
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( )
Generalization
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 any_storable_obj->save_your_data_to_db( ).
Interface Definition
Interface Implementation
Client
• Interface references allow access to all interface elements
i.e. regardless of implementing class
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
Content lif_vehicle
<<interface>>
Look at sample solution
Show objects in debugger get_current_tank_level( )
refuel( )
drive_distance( )
lcl_car lcl_truck
- name
- inst_table
- tank_capacity
- name
- fuel_consumption_per_km
- tank_capacity
- curr_tank_level
- fuel_consumption_per_km
- curr_tank_level …special truck methods
+ get_instance( )
…special car methods
Polymorphism: Inheritance
Polymorphism: Inheritance/Subclassing/Derivation
Class Hierarchy
Motivation
• Inheritance is a means to implement conceptual
hierarchies and/or share common code between
classes lcl_vehicle
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
Parent Class
Subclass
lif_vehicle
Exercise: Move common code to a super-class <<interface>>
Content
Look at sample solution
Show objects in debugger
• 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
Cparent S¹ Cany¹
S¹ S² S² Cany²
Inheritance Delegation
Implementation
Definition Implementation
- name
- tank_capacity
Main Program - 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
Definition Implementation
- inst_table
- name
Main Program - 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
lcl_car lcl_truck
- name
- inst_table
- tank_capacity
- name
- fuel_consumption_per_km
- tank_capacity
- curr_tank_level
- 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
lcl_vehicle
<<abstract>>
- name
- tank_capacity
- fuel_consumption_per_km
- curr_tank_level
lcl_car lcl_truck
- inst_table
reserve_tank_level
…
+ get_instance( )
+ refuel( ): redefinition
+ get_reserve_tank_level()
Main Program
Back to Exercise
No part of this publication may be reproduced or transmitted in any form or for any purpose Google App Engine, Google Apps, Google Checkout, Google Data API, Google Maps,
without the express permission of SAP AG. The information contained herein may be Google Mobile Ads, Google Mobile Updater, Google Mobile, Google Store, Google Sync,
changed without prior notice. Google Updater, Google Voice, Google Mail, Gmail, YouTube, Dalvik and Android are
trademarks or registered trademarks of Google Inc.
Some software products marketed by SAP AG and its distributors contain proprietary
software components of other software vendors. INTERMEC is a registered trademark of Intermec Technologies Corporation.
Microsoft, Windows, Excel, Outlook, PowerPoint, Silverlight, and Visual Studio are Wi-Fi is a registered trademark of Wi-Fi Alliance.
registered trademarks of Microsoft Corporation.
Bluetooth is a registered trademark of Bluetooth SIG Inc.
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 Motorola is a registered trademark of Motorola Trademark Holdings LLC.
Architecture, Power Systems, POWER7, POWER6+, POWER6, POWER, PowerHA, Computop is a registered trademark of Computop Wirtschaftsinformatik GmbH.
pureScale, PowerPC, BladeCenter, System Storage, Storwize, XIV, GPFS, HACMP,
RETAIN, DB2 Connect, RACF, Redbooks, OS/2, AIX, Intelligent Miner, WebSphere, Tivoli, SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer,
Informix, and Smarter Planet are trademarks or registered trademarks of IBM Corporation. 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
Linux is the registered trademark of Linus Torvalds in the United States and other countries. and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are trademarks or registered Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal
trademarks of Adobe Systems Incorporated in the United States and other countries. Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services
Oracle and Java are registered trademarks of Oracle and its affiliates. mentioned herein as well as their respective logos are trademarks or registered trademarks
of Business Objects Software Ltd. Business Objects is an SAP company.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin products and services mentioned herein as well as their respective logos are trademarks or
are trademarks or registered trademarks of Citrix Systems Inc. registered trademarks of Sybase Inc. Sybase is an SAP company.
HTML, XML, XHTML, and W3C are trademarks or registered trademarks of W3C®, Crossgate, m@gic EDDY, B2B 360°, and B2B 360° Services are registered trademarks
World Wide Web Consortium, Massachusetts Institute of Technology. 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, All other product and service names mentioned are the trademarks of their respective
Retina, Safari, Siri, and Xcode are trademarks or registered trademarks of Apple Inc. companies. Data contained in this document serves informational purposes only. National
product specifications may vary.
IOS is a registered trademark of Cisco Systems Inc.
The information in this document is proprietary to SAP. No part of this document may be
RIM, BlackBerry, BBM, BlackBerry Curve, BlackBerry Bold, BlackBerry Pearl, BlackBerry reproduced, copied, or transmitted in any form or for any purpose without the express prior
Torch, BlackBerry Storm, BlackBerry Storm2, BlackBerry PlayBook, and BlackBerry App written permission of SAP AG.
World are trademarks or registered trademarks of Research in Motion Limited.