DEV-12: Object-oriented Programming in OpenEdge® ABL
Evan BleicherSenior Development Manager,
Progress OpenEdge
© 2007 Progress Software Corporation2 DEV-12, Object-oriented Programming in OpenEdge ABL
Terminology / Concept Review
Type - Strong-typing at compile time• Data member – state
• Methods – behavior
• Package – type name fully qualified
Class – Defines type; data and methods
Object – Runtime instance of a class
Interface – Set of method definitions; contract
Inheritance – Inherit / specialize from super class
© 2007 Progress Software Corporation3 DEV-12, Object-oriented Programming in OpenEdge ABL
Today’s Agenda
Class Statement
Type Names
Methods
Object Hierarchy
Properties
Class Compiler Enhancements
Roadmap 10.1C and Beyond
© 2007 Progress Software Corporation4 DEV-12, Object-oriented Programming in OpenEdge ABL
File name: Acme\Inv\Order.cls
CLASS Acme.Inv.Order:
END CLASS.
CLASSStatement
CLASS <class-type> [package.]classname Single inheritence Multiple interface
ProPath
\Acme
\Inv
\Order.cls
\Order.r
© 2007 Progress Software Corporation5 DEV-12, Object-oriented Programming in OpenEdge ABL
Class Statement - Optional Modifiers
FINAL• Class can not be inherited
USE-WIDGET-POOL• Creates an unnamed widget pool scoped to
the class
• Dynamically created handle based within the class placed in pool
• Automatically deleted when class is destroyed
• Can be specified anywhere in the hierarchy
© 2007 Progress Software Corporation6 DEV-12, Object-oriented Programming in OpenEdge ABL
Today’s Agenda
Class Statement
Type Names
Methods
Object Hierarchy
Properties
Class Compiler Enhancements
Roadmap 10.1C and Beyond
© 2007 Progress Software Corporation7 DEV-12, Object-oriented Programming in OpenEdge ABL
Type Name
Type defines• State• Behavior• Inheritance relationship with other types
Enable strong-typing• Early binding - types determined at compile
time• Type-consistency enforced at compile time
and runtime
Full type names used to reference all classes
A Type is a definition
© 2007 Progress Software Corporation8 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Type Names
DEF VAR myOrder AS CLASS Acme.Inv.Order.
DEF VAR myInternalOrder AS CLASS Acme.Inv.InternalOrder.
DEF VAR myExternalOrder AS CLASS Acme.Inv.ExternalOrder.
myOrder = NEW Acme.Inv.Order ( ).
myInternalOrder = NEW Acme.Inv.InternalOrder ( ).
myExternalOrder = NEW Acme.Inv.ExternalOrder ( ).
© 2007 Progress Software Corporation9 DEV-12, Object-oriented Programming in OpenEdge ABL
USING Statement (10.1B)
Identifies a package of classes or a single qualified class
Supported in both Procedures and Class files
Applied at compile time to unqualified class names
First executable statement in file
Supports use of unqualified class name references
© 2007 Progress Software Corporation10 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Type Names with USING
USING Acme.Inv.*.
DEF VAR myOrder AS CLASS Order.
DEF VAR myInternalOrder AS CLASS InternalOrder.
DEF VAR myExternalOrder AS CLASS ExternalOrder.
myOrder = NEW Order ( ).
myInternalOrder = NEW InternalOrder ( ).
myExternalOrder = NEW ExternalOrder ( ).
© 2007 Progress Software Corporation11 DEV-12, Object-oriented Programming in OpenEdge ABL
Today’s Agenda
Class Statement
Type Names
Methods
Object Hierarchy
Properties
Class Compiler Enhancements
Roadmap 10.1C and Beyond
© 2007 Progress Software Corporation12 DEV-12, Object-oriented Programming in OpenEdge ABL
Methods
Keywords defining methods• METHOD
• CONSTRUCTOR
• DESTRUCTOR
Supports access modifiers• PRIVATE, PROTECTED, PUBLIC
• Default is PUBLIC
Define data type it returns or VOID
Methods define the behavior of an object
© 2007 Progress Software Corporation13 DEV-12, Object-oriented Programming in OpenEdge ABL
Overriding
Method in subclass can specialize method in super class
Can replace or augment behavior in super class
Overridden method must have:• Same signature
• OVERRIDE keyword
To access super class’ methods use • SUPER:method-name ( )
Specialization of methods
© 2007 Progress Software Corporation14 DEV-12, Object-oriented Programming in OpenEdge ABL
METHOD PUBLIC DECIMAL getOrderTotal ( ):
/* Calculate total */
RETURN totalOrder.
END METHOD.
Example:Overriding Acme\Inv\Order.cls
METHOD PUBLIC OVERRIDE DECIMAL getOrderTotal ( ): DEFINE VAR dTotal AS DECIMAL. DEFINE VAR dDiscount AS DECIMAL INITIAL 0.85.
dTotal = SUPER:getOrderTotal ( ). RETURN dTotal * dDiscount.END METHOD.
Acme\Inv\InternalOrder.cls
OVERRIDE keyword
Pre / Post processing
© 2007 Progress Software Corporation15 DEV-12, Object-oriented Programming in OpenEdge ABL
Polymorphism
Execution of an overridden method in a subclass from a reference to a super class
Code written using super class• Tightly coupled to inheritance and overriding
• Super class used at compile time, subclass assigned at runtime
• Method call on super class dispatched to subclass’ method
Same method can perform different behavior in subclass
© 2007 Progress Software Corporation16 DEV-12, Object-oriented Programming in OpenEdge ABL
Polymorphism – Example
Acme.Inv.InternalOrder
Acme.Inv.OrderMETHOD PUBLIC DECIMAL getOrderTotal ( ):
Acme.Inv.ExternalOrder
METHOD PUBLIC OVERRIDE DECIMAL getOrderTotal ( ):
METHOD PUBLIC OVERRIDE DECIMAL getOrderTotal ( ):
© 2007 Progress Software Corporation17 DEV-12, Object-oriented Programming in OpenEdge ABL
Polymorphism - Example Continued
USING Acme.Inv.*.
DEFINE VARIABLE myOrder AS CLASS Order.DEFINE VARIABLE bInternalCust AS Logical.DEFINE VARIABLE dTotal AS Decimal.
IF (bInternalCust = TRUE)THENmyOrder = NEW InternalOrder( ).
ELSE myOrder = NEW ExternalOrder( ).
dTotal = myOrder:getOrderTotal( ).
Super Class Reference
Calls either Acme.Inv.InternalOrder or Acme.Inv.ExternalOrder getOrderTotal ( )
© 2007 Progress Software Corporation18 DEV-12, Object-oriented Programming in OpenEdge ABL
Benefits of Polymorphism
Supports generic programming using super class or interface• Type used at compile time is super class or
interface
Specialized behavior is called at runtime automatically• Built on inheritance and overriding
© 2007 Progress Software Corporation19 DEV-12, Object-oriented Programming in OpenEdge ABL
Overloading (10.1B)
Different behavior / same name – based on different signature
Routines with the same name• Constructors and Methods
Must have different signatures• Number, type and mode of parameters• Can not differ by only return type or access
mode
No keyword to identify overloaded method
Multiple methods with the same name
© 2007 Progress Software Corporation20 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Overloading
METHOD PUBLIC INTEGER getOrder ( ):
END METHOD.
METHOD PUBLIC INTEGER getOrder (
INPUT iCustNum AS INTEGER):
END METHOD.
METHOD PUBLIC INTEGER getOrder (
INPUT cSalesRep AS CHARACTER):
END METHOD.
No arguments
One argument
- integer
One argument - character
© 2007 Progress Software Corporation21 DEV-12, Object-oriented Programming in OpenEdge ABL
Widening (10.1B)
Object-oriented features are strongly-typed• Data type of parameter must match
DATE DATETIME DATETIME-TZ
INTEGER INT64 DECIMAL
Mode is significant – Input, Output, Input-Output
Widening allows passing ‘smaller’ data type for ‘larger’
© 2007 Progress Software Corporation22 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:WideningMETHOD PUBLIC INTEGER updateSalesDate (
INPUT dtTransRecord AS DATETIME-TZ):
END METHOD.
DEFINE VARIABLE myDate as DATE.
updateSalesRecord (INPUT myDate).
DEFINE VARIABLE myDateTime as DATETIME.
updateSalesRecord (INPUT myDateTime).
DEFINE VARIABLE myDateTimeTZ as DATETIME-TZ.
updateSalesRecord (INPUT myDateTimeTZ).
© 2007 Progress Software Corporation23 DEV-12, Object-oriented Programming in OpenEdge ABL
Hey, how does widening affect overloading?
Follows Overloading rules:• Number of parameters must match exactly
• Mode of parameter must match exactly
Relaxes data type matching rules:• With widening the caller’s data type does not
have to match exactly the callee’s data type
Overloading looks for the best match
Application design concern:• May result in ambiguity error
© 2007 Progress Software Corporation24 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Widening with Overloading
METHOD PUBLIC INTEGER updateSalesRecord (
INPUT dtTransDate AS DATETIME-TZ,
INPUT szSalesRep AS CHARACTER):
METHOD PUBLIC INTEGER updateSalesRecord (
INPUT dtTransDate AS DATE,
INPUT szSalesRep AS CHARACTER):
updateSalesRecord (INPUT myDate, INPUT “ABC”).
updateSalesRecord (INPUT myDateTime, INPUT “ABC”).
updateSalesRecord (INPUT myDateTimeTZ, INPUT “ABC”).
© 2007 Progress Software Corporation25 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Widening with Overloading
METHOD PUBLIC INTEGER UpdateSalesRecord (
INPUT dtTransDate AS DATETIME-TZ,
INPUT iSalesAmount AS INTEGER):
METHOD PUBLIC INTEGER UpdateSalesRecord (
INPUT dShipDate AS DATE,
INPUT deUnitsSold AS DECIMAL):
UpdateSalesRecord (INPUT myDate, INPUT myInt64).
UpdateSalesRecord (INPUT myDateTime, INPUT myDecimal).
UpdateSalesRecord (INPUT myDate, INPUT myInteger).
Does not compile – no match
Does not compile –
Ambiguity error
© 2007 Progress Software Corporation26 DEV-12, Object-oriented Programming in OpenEdge ABL
Overloading:Other considerations
Interoperability remains for static / dynamic • Table / Table-Handle
• Dataset / Dataset-Handle
Overloading supported for class and interface types
May result in runtime ambiguity error• Buffer-field
• Dynamic-function
© 2007 Progress Software Corporation27 DEV-12, Object-oriented Programming in OpenEdge ABL
Raising Error (10.1B)
Methods can raise error
RETURN ERROR [<error string>]
Caller handles error condition • NO-ERROR
• ON ERROR phrase
Retrieve <error string> via RETURN-VALUE function
© 2007 Progress Software Corporation28 DEV-12, Object-oriented Programming in OpenEdge ABL
Error Raised in an Expression
All methods invoked left-to-right• Order of operands apply to results of methods
On error the result of expression is unchanged
myInt = objA:methA( ) – objB:methB( ) * objC:methC( ).
© 2007 Progress Software Corporation29 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Error in an Expression
myInt = objA:methA( ) – objB:methB( ) * objC:methC( ).
If methA raises error, methB and methC are not invoked• myInt’s value remains unchanged
If methB raises error, methC is not invoked• myInt’s value remains unchanged
© 2007 Progress Software Corporation30 DEV-12, Object-oriented Programming in OpenEdge ABL
Today’s Agenda
Class Statement
Type Names
Methods
Object Hierarchy
Properties
Class Compiler Enhancements
Roadmap 10.1C and Beyond
© 2007 Progress Software Corporation31 DEV-12, Object-oriented Programming in OpenEdge ABL
Object Instantiation
Constructor of specified class is invoked on NEW
Constructor used to initialize data members
Constructor must call super class’s constructor• Via SUPER ( )
OpenEdge provides a call to the ‘default’ constructor• One with no parameters
• Only if super class has no other constructor
© 2007 Progress Software Corporation32 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Object Instantiation
refD = NEW D (D’s params).
Invokes D’s constructor
Class A
Class D
Class C
CONSTRUCTOR PUBLIC D (D’s params ):
SUPER (C’s params).
/* D’s constructor body */
END.
CONSTRUCTOR PUBLIC C (C’s params ):
SUPER (B’s params).
/* C’s constructor body */
END.
Class B
© 2007 Progress Software Corporation33 DEV-12, Object-oriented Programming in OpenEdge ABL
Overloading of Constructor
Classes can have multiple constructors
Same rules as overloading for methods
Parameters specified in NEW determine which constructor is invoked
© 2007 Progress Software Corporation34 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:OverloadingCONSTRUCTOR PUBLIC Order ( ):
/* Constructor code goes here */END CONSTRUCTOR.
CONSTRUCTOR PUBLIC Order (INPUT iCustNum AS INTEGER):
/* Constructor code goes here */END CONSTRUCTOR.
CONSTRUCTOR PUBLIC Order (INPUT cSalesRep AS CHARACTER):
/* Constructor code goes here */END CONSTRUCTOR.
No argumentsOne argument - Character
One argument - Integer
© 2007 Progress Software Corporation35 DEV-12, Object-oriented Programming in OpenEdge ABL
Raising Error (10.1B)
Constructors can raise error
RETURN ERROR [<error string>]
Caller handles error condition • NO-ERROR• ON ERROR phrase
Object reference unchanged
Destructor invoked for all classes in hierarchy whose constructor completed successfully
© 2007 Progress Software Corporation36 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Raising Error in a Constructor
DEFINE VARIABLE refD AS CLASS D.
refD = NEW D ( ) NO-ERROR.
IF ERROR-STATUS:ERROR THEN
MESSAGE “INSTANTIATION FAILED: “
RETURN-VALUE.
Class D
Class C
Class A
CONSTRUCTOR PUBLIC D (D’s params ):
SUPER (C’s params).
/* D’s constructor body */
RETURN ERROR “ret code 101”.
END.
Class B
© 2007 Progress Software Corporation37 DEV-12, Object-oriented Programming in OpenEdge ABL
Object Deletion
Destructor of specified class is invoked on DELETE OBJECT
Destructor can not be called directly
OpenEdge calls all destructors in the class hierarchy
Destructors invoked from subclass to super class
© 2007 Progress Software Corporation38 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Object Deletion
DELETE OBJECT refD.
Invokes D’s Destructor
Class A
Class D
Class C
DESTRUCTOR PUBLIC D ( ):
/* D’s destructor */
END.
DESTRUCTOR PUBLIC C ( ):
/* C’s destructor */
END.
Class B
© 2007 Progress Software Corporation39 DEV-12, Object-oriented Programming in OpenEdge ABL
Today’s Agenda
Class Statement
Type Names
Methods
Object Hierarchy
Properties
Class Compiler Enhancements
Roadmap 10.1C and Beyond
© 2007 Progress Software Corporation40 DEV-12, Object-oriented Programming in OpenEdge ABL
Properties (10.1B)
Combine data member and method
Provide getter and setter accessors• Promotes data encapsulation
Optional access mode on accessors
Caller uses Property as data member
Support data encapsulation
© 2007 Progress Software Corporation41 DEV-12, Object-oriented Programming in OpenEdge ABL
DEFINE PUBLIC PROPERTY Salary AS DECIMAL GET ( ): /* Verify current user’s status */
RETURN Salary. END GET. PROTECTED SET (INPUT iVal AS DECIMAL): /* Is it within range? */ IF ival LT 100000 THEN Salary = ival. ELSE RETURN ERROR. END SET.
Example:Defining Property
© 2007 Progress Software Corporation42 DEV-12, Object-oriented Programming in OpenEdge ABL
Example:Accessing a Property
Same syntax as setting / retrieving a data member
Can be used in expression
EmployeeObj:Salary = newValue.
DISPLAY EmployeeObj:Salary.
DeptObj:Record (INPUT EmployeeObj:Name,
INPUT EmployeeObj:Salary).
© 2007 Progress Software Corporation43 DEV-12, Object-oriented Programming in OpenEdge ABL
DEFINE PUBLIC PROPERTY pi AS DECIMAL INITIAL 3.14
GET ( ):
RETURN pi.
END GET.
Example 2:Defining Read-Only Property (Constant)
DEFINE PUBLIC PROPERTY pi AS DECIMAL INITIAL 3.14
GET.
© 2007 Progress Software Corporation44 DEV-12, Object-oriented Programming in OpenEdge ABL
Example 3:Defining Property – not using default memory
DEFINE PUBLIC PROPERTY numUnits AS INTEGER
GET ( ):
/* Retrieve from temp-table – current value */
RETURN someTT:currentInventory.
END GET.
IF numRequested LT orderObj:numUnits THEN
orderObj:processOrder(INPUT numRequested).
ELSE
...
© 2007 Progress Software Corporation45 DEV-12, Object-oriented Programming in OpenEdge ABL
Today’s Agenda
Class Statement
Type Names
Methods
Object Hierarchy
Properties
Class Compiler Enhancements
Roadmap 10.1C and Beyond
© 2007 Progress Software Corporation46 DEV-12, Object-oriented Programming in OpenEdge ABL
Compiling Tips and Techniques
Strong-typing only works if you compile – running “compile-on-the-fly” delays errors until runtime• Be safe – always rebuild/compile your code
Recompiling only super classes can result in runtime errors – always recompile subclasses that depend on super classes to be safe• Exceptions to this rule…
© 2007 Progress Software Corporation47 DEV-12, Object-oriented Programming in OpenEdge ABL
Call new method
Recompile
Add new method
Compile
Compiling Tips and Techniques
No recompile of subclasses required when:• Methods / data is added to
super class
• Only implementation changes, not signature
When subclass accesses new methods and data, must recompile
AA
BB EE
FF
KK
JJ
HH
GGDD
CC
II
© 2007 Progress Software Corporation48 DEV-12, Object-oriented Programming in OpenEdge ABL
Compiling Tips and Techniques
COMPILER: MULTI-COMPILE = TRUE• Uses cache to store classes
already compiled in session• “Cache” cleared when set to
FALSE• OE Architect uses this
option
Otherwise COMPILE builds full class hierarchy – no timestamp check
Total files compiled:
MULTI-COMPILE = FALSE: 25
MULTI-COMPILE = TRUE: 8
AA
BB
EE FF HHGG
DDCC
Build procedures (make.p)
© 2007 Progress Software Corporation49 DEV-12, Object-oriented Programming in OpenEdge ABL
Recommended Coding Standards
Always use packages in your type name: Acme.DAOrder• First node is your company name
Class and Interface names use Pascal Case• Interface names start with leading “I”: Acme.IList
Methods and data member use camelCase with lowercase first letter
Use object-oriented design patterns -encapsulation, abstraction, and delegation
© 2007 Progress Software Corporation50 DEV-12, Object-oriented Programming in OpenEdge ABL
Today’s Agenda
Class Statement
Type Names
Methods
Object Hierarchy
Properties
Interface
Class Compiler Enhancements
Roadmap 10.1C and Beyond
© 2007 Progress Software Corporation51 DEV-12, Object-oriented Programming in OpenEdge ABL
10.1A
Class
Interface
Inheritance
Overriding
Access mode for data members
Strong type checking
Compiler improvements
© 2007 Progress Software Corporation52 DEV-12, Object-oriented Programming in OpenEdge ABL
10.1B
USING
Overloading
Properties
Default access mode methods - PUBLIC
Raising error from Constructor / Methods
Compiler system handle changes
© 2007 Progress Software Corporation53 DEV-12, Object-oriented Programming in OpenEdge ABL
D I S C L A I M E R
Under Development
This talk includes information about potential future products and/or product enhancements.
What we are going to say reflects our current thinking, but the information contained herein is preliminary and subject to change. Any future products we ultimately deliver may be materially different from what is described here.
D I S C L A I M E R
© 2007 Progress Software Corporation54 DEV-12, Object-oriented Programming in OpenEdge ABL
Targeted for 10.1C
Structured Error Handling• Similar to Try, Catch, Throw
Statics• Constructors, Data Members, Methods
Character to Longchar widening
NEW function
© 2007 Progress Software Corporation55 DEV-12, Object-oriented Programming in OpenEdge ABL
Future Release
Remote objects• Instantiating a class on the AppServer
Strongly typed application events• Publish / Subscribe for methods
Arrays of object references
Properties in interfaces
Inheritance of interfaces
Reflection
Dynamic invocation
© 2007 Progress Software Corporation56 DEV-12, Object-oriented Programming in OpenEdge ABL
In Summary
Object-oriented ABL supports standard object-oriented design patterns – abstraction, encapsulation, inheritance, polymorphism
ABL supports standard object-oriented constructs – interfaces, overriding, overloading, properties, strong-typing
More object-oriented functionality coming
© 2007 Progress Software Corporation57 DEV-12, Object-oriented Programming in OpenEdge ABL
Additional Object-oriented Exchange Sessions
DEV-6: Getting Started with Object-oriented ABL Programming (Evan Bleicher)
ARCH-7: A Class-based Implementation of the OERA (John Sadd)
ARCH-9: Using Object-oriented ABL Features in N-Tier Environment (Mike Fechner)
ARCH-12: Leveraging Design Patterns in ABL Applications (Phil Magnay)
DEV-20: Using Classes and Procedures in OpenEdge 10.1B (Phil Magnay)
© 2007 Progress Software Corporation58 DEV-12, Object-oriented Programming in OpenEdge ABL
For More Information, go to…
PSDN
Progress eLearning Community• What's New OE 10.1 Object Oriented
Programming
Documentation• 10.1B Object-oriented Programming manual
• 10.1B New and Revised Features manual
© 2007 Progress Software Corporation59 DEV-12, Object-oriented Programming in OpenEdge ABL
Questions?
© 2007 Progress Software Corporation60 DEV-12, Object-oriented Programming in OpenEdge ABL
Thank you for your time!
© 2007 Progress Software Corporation61 DEV-12, Object-oriented Programming in OpenEdge ABL