Anda di halaman 1dari 3

SAPTechnical.

COM - Object oriented programming (OOP) explained with an example

Home Tips Tutorials Sample Specs Certification Q's Interview Q's Jobs Testimonials Contact Us

For Tutorials/Tips in SAP Functional areas, visit SAPFunctional.COM

Object oriented programming (OOP) explained with an example


By Gaurab Banerji

Document Categories:
q q q q q q q q q q q q q q q q q

ABAPTM Business Workflow LSMW SAP Script/Smart Forms eXchange Infrastructure (XI) ABAP-HR ALE & IDocs ALV BAPI BASIS BSP eCATT Object Oriented Programming SAP Query Userexits/BADIs WebDynpro for Java/ABAPTM Others

Create a class that keeps track of a bank account balance. Then write a program to use this class.
Steps involved:
q

Run the class builder utility (SE24). Create a class called ZACCOUNTxx, where xx is the last two digits of your logon ID. Declare a PRIVATE attribute BALANCE of type DMBTR to store the account balance. Create the following PUBLIC methods:
r

SET_BALANCE (Sets the balance to a new value)


s IMPORTING NEW_BALANCE TYPE DMBTR DEPOSIT (Adds a deposit amount to the balance and returns the new balance)

Email a Friend What's New?


r
q

IMPORTING AMOUNT TYPE DMBTR

q q

q q q q

q q q q q q q q q q

q q

WebDynpro for ABAP - Implementing Enhancements in a view Displaying text box and radio button using WebDynpro for ABAP The features of ABAP New Editor Creating Custom Rules for use in Workflow definition Working on Polymorphism Step-by-step creation of a BAPI Working with Menu Painter Working with import, export and change parameters of a class Insert data into the database table using classes Smart Forms - Working with Loop Designing Tables in Smart Forms Creating Search Help (Elementary and Collective) Working with Smart Styles Workflow - Purchase Order Release Strategy Loading data into infocubes Triggering workflow using BTE Ad Hoc agent assignment in Workflow Sending mails recursively, upon reaching the deadline, until the task is completed Distributing Material Master data using Standalone and Change Pointers XI - ABAP Proxy communication (Step-by-step) Defining and implementing a BADI (step-by-step with screenshots) Automatic IDOC generation whenever a PO is created/changed

s RETURNING NEW_BALANCE TYPE DMBTR WITHDRAW (Subtracts a deposit amount from the balance and returns the new balance.)

IMPORTING AMOUNT TYPE DMBTR RETURNING NEW_BALANCE TYPE DMBTR

s EXCEPTIONS INSUFFICIENT_FUNDS Activate all elements of your class.

Write a program called Z_USE_ACCOUNT_xx, where xx is the last two digits of your logon ID. This program should do the following:
r

Instantiate an instance of the Account class. Set the account balance to some initial value.
Make several deposits and withdrawals, printing the new balance each time. Do not allow the balance to become less than zero. (Use the exception to detect this.)

Test and debug your program.

"Extra Credit": If you have extra time, try any of the following:
q

Replace the SET_BALANCE method with a constructor. Pass the opening balance when you instantiate the account object. Create a static attribute and methods to set and get the name of the bank that holds the accounts.

Contribute?

Step-by-step approach with screen-shots Go to SE24 (Class builder)

http://www.saptechnical.com/Tutorials/ABAP/OOP/Example1.htm (1 of 3)11/23/2007 11:11:46 AM

SAPTechnical.COM - Object oriented programming (OOP) explained with an example

What's Hot?
LSMW Tutorials

Type in ZACCOUNTAA as the name of the class and press Create.

Join the Mailing List


Enter name and email address below: Name: Email:

Subscribe

Unsubscribe

GO

Define 3 methods DEPOSIT, SET_BALANCE and WITHDRAW.

Place the mouse cursor in DEPOSIT and hit the Parameters button.

Write the parameters imported / exported for DEPOSIT method. Similarly for SET_BALANCE

And WITHDRAW

For withdraw we define an exception.

http://www.saptechnical.com/Tutorials/ABAP/OOP/Example1.htm (2 of 3)11/23/2007 11:11:46 AM

SAPTechnical.COM - Object oriented programming (OOP) explained with an example

We can see the attributes and methods by pressing Display object list button on top.

Now we IMPLEMENT the 3 methods. Double click the method DEPOSIT.

...Continued

Google Search

Web

SAPtechnical.COM

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM Home Contribute About Us Privacy Terms Of Use Disclaimer Safe Companies: Advertise on SAPTechnical.COM | Post Job Contact Us

20062007 SAPTechnical.COM. All rights reserved. All product names are trademarks of their respective companies. SAPTechnical.COM, Inc. is in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver, and and any other SAP trademarks are registered trademarks of SAP AG in Germany and in several other countries. Every effort is made to ensure content integrity. Use information on this site at your own risk.
Graphic Design by Round the Bend Wizards

http://www.saptechnical.com/Tutorials/ABAP/OOP/Example1.htm (3 of 3)11/23/2007 11:11:46 AM

Anda mungkin juga menyukai