DEV-08: Exploring Object-oriented Programming Shelley Chase Development Architect – Progress OpenEdge
Today’s Agenda Basic Principles of Object-oriented Programming Types, Classes, Objects, and Interfaces Inheritance, Polymorphism, and Delegation DEV-08, Exploring Object-oriented Programming
What are Objects? You interact with objects everyday A customer An order All objects contains state and behavior What they can do and what changes when they do Software objects represent these as: Data ( like 4GL variables ) Methods ( like 4GL procedures) Your car The telephone DEV-08, Exploring Object-oriented Programming
Object-oriented Programming “Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class...” Grady Booch DEV-08, Exploring Object-oriented Programming
Object-oriented Application Development A way to design and build applications Objects bundle together data (state) and methods (behavior) Objects facilitate separating definition from implementation Much more than just syntax You might have already done object-oriented programming in the 4GL DEV-08, Exploring Object-oriented Programming
Designing an Object-oriented Application Object: Order Take an Order Object: Customer Check Credit Create a Customer Object: beOrder Assign Salesperson Progress DataSet Customer Table Order Table Customer Table DEV-08, Exploring Object-oriented Programming
Basic Object-oriented Principles Abstraction Encapsulation Hierarchies DEV-08, Exploring Object-oriented Programming
Abstraction Abstraction is used to manage complexity Public View of an Object Abstraction is used to manage complexity Focus on the essential characteristics Eliminate the details Find commonalities among objects Defines the public contract Public definition for users of the object The “Outside view” Independent of implementation DEV-08, Exploring Object-oriented Programming
“What should an Order object do?” Abstraction - Example “What should an Order object do?” Object: Order CreateOrder UpdateOrder GetOrderTotal Next InternalOrder Two types of Orders ExternalOrder DEV-08, Exploring Object-oriented Programming
Encapsulation Encapsulation hides implementation Hide Implementation Details Encapsulation hides implementation Promotes modular software design – data and methods together Data access always done through methods Often called “information hiding” Provides two kinds of protection: State cannot be changed directly from outside Implementation can change without affecting users of the object DEV-08, Exploring Object-oriented Programming
Encapsulation - Example Implementation Outside View Object: Order orderNum AS INT custNum AS INT CalculatePrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) Public methods of Order class CreateOrder UpdateOrder GetOrderTotal Next CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) DEV-08, Exploring Object-oriented Programming
Encapsulation - Example continued Hmm... I’d like to change CalculatePrice to CalculateTotalPrice Object: Order orderNum AS INT custNum AS INT CalculatePrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) GetOrderTotal calls CalculatePrice( ) DEV-08, Exploring Object-oriented Programming
Encapsulation - Example continued Object: Order This change was easy because users of the object will not be affected. orderNum AS INT custNum AS INT CalculateTotalPrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) GetOrderTotal now calls CalculateTotalPrice( ) DEV-08, Exploring Object-oriented Programming
Hierarchies Define relationships between objects Object Relationships Define relationships between objects Objects defined in terms of other objects Allows state and behavior to be shared and specialized as necessary Encourages code reuse Two important hierarchy types: Inheritance Aggregation DEV-08, Exploring Object-oriented Programming
Hierarchies - Example Order is a is a references InternalOrder Order uses ShipInfo (Aggregation) Order is a is a references InternalOrder ExternalOrder ShipInfo InternalOrder and ExternalOrder inherit from Order (Inheritance) DEV-08, Exploring Object-oriented Programming
Summary : Object-oriented Principles Abstraction Break up complex problem Focus on public view, commonalities Encapsulation Hide implementation details Package data and methods together Hierarchies Build new objects by combining or extending other objects DEV-08, Exploring Object-oriented Programming
Today’s Agenda Basic Principles of Object-oriented Programming Types, Classes, Objects, and Interfaces Inheritance, Polymorphism, and Delegation DEV-08, Exploring Object-oriented Programming
Type A Type defines the state and behavior Enables strong-typing A Type is a definition A Type defines the state and behavior Identifies inheritance relationships with other types No concern for implementation Enables strong-typing Early binding - types determined at compile time Type-consistency enforced at compile time and runtime DEV-08, Exploring Object-oriented Programming
Type - Example Types: Order Order InternalOrder ExternalOrder is a Subtype of Order ExternalOrder SubType of Order Order is a is a InternalOrder ExternalOrder A subtype can appear anywhere a super type is expected DEV-08, Exploring Object-oriented Programming
Benefits of Types (Strong-Typing) Compile time checking for type consistency myObj = mySubObject. (must be subType) myObj:method(…). (validates signature) myObj:data = 3. (validates data type) Results in safer, bug-free code because all code paths checked at compile time DEV-08, Exploring Object-oriented Programming
Class Class: Order A Class defines and implements a user-defined type A Class implements a Type Class: Order orderNum AS INT custNum AS INT CalculateTotalPrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) A Class defines and implements a user-defined type A Class is a template (blueprint) for an object: Data Methods Relationships to other classes Methods Data DEV-08, Exploring Object-oriented Programming
Object An Object is created at runtime An Object is an instance of a Class An Object is created at runtime Maintains independent state in data members Code shared among object instances The term Object is often used to refer to both classes and instances MyOrder orderNum = 10 custNum = 3 Total Price = $45.00 YourOrder orderNum = 61 custNum = 58 Total Price = $318.34 DEV-08, Exploring Object-oriented Programming
Interface An Interface implements a Type An Interface is a collection of method definitions for a set of behaviors – a “contract” No implementation provided A Class can implement an interface Must implement all methods in the interface Behavior can be specialized Compiler validates implementation of interface DEV-08, Exploring Object-oriented Programming
Interface - Example Class Order implements IList interface orderNum AS INT custNum AS INT CalculateTotalPrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) Interface: IList PUBLIC: Next( ) Compiler checks for method definition in implementing class DEV-08, Exploring Object-oriented Programming
Interface – Example continued Write generic routine using interface MoveNext( listObj AS IList ) listObj.Next( ). /* Calls method in real object */ Call with any object that implements IList myOrder = NEW Order( ). /* implements IList */ MoveNext( myOrder ). or myCust = NEW Customer( ). /* implements IList */ MoveNext( myCust ). DEV-08, Exploring Object-oriented Programming
Benefits of Interfaces Allows many different classes to be treated in a like manner All classes that implement an interface are guaranteed to have same set of methods Enables generic programming IList example allows any collection of objects to be navigated using Next( ) Behavior can be specialized as needed DEV-08, Exploring Object-oriented Programming
Summary : Object-oriented Constructs Type Enforces type consistency at compile time Class Defines type with data and methods and provides implementation Object Runtime instantiation of class Interface Defines type with methods – no implementation provided DEV-08, Exploring Object-oriented Programming
Today’s Agenda Basic Principles of Object-oriented Programming Types, Classes, Objects, and Interfaces Inheritance, Polymorphism, and Delegation DEV-08, Exploring Object-oriented Programming
Inheritance Super Class (Base class) Subclass (Derived class) Relationship between Classes Super Class Super Class (Base class) Provides common functionality and data members Subclass (Derived class) Inherits public and protected members from the super class Can extend or change behavior of super class by overriding methods … Subclass DEV-08, Exploring Object-oriented Programming
Access Levels for Class Members PRIVATE members available: Only within the class PROTECTED members available: Within the class Within the class hierarchy PUBLIC members available: To users outside the class Order PRIVATE PROTECTED PUBLIC DEV-08, Exploring Object-oriented Programming
Inheritance Example Class: Order PRIVATE: orderNum AS INT custNum AS INT CalculateTotalPrice( ) PROTECTED: GetCredit( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) DEV-08, Exploring Object-oriented Programming
Class InternalOrder inherits Order Inheritance Example Class InternalOrder inherits Order Class: Order PRIVATE: orderNum AS INT custNum AS INT CalculateTotalPrice( ) InternalOrder PROTECTED: GetCredit( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) PROTECTED: GetCredit( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) PROTECTED: GetCredit( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) DEV-08, Exploring Object-oriented Programming
Inheritance and Method Overriding Method overriding used to specialize behavior Subclass may override a method in its super class (hierarchy) Method signatures must match Overriden method can: Completely override behavior of super class Augment behavior by providing its own behavior and calling super class method DEV-08, Exploring Object-oriented Programming
Method Overriding – Example 1 Order PROTECTED: GetCredit( ) credit = FindCreditScore( ). Class InternalOrder inherits Order InternalOrder PROTECTED: GetCredit ( ) credit = -1. /*unlimited*/ DEV-08, Exploring Object-oriented Programming
Method Overriding – Example 2 Order PROTECTED: GetCredit( ) credit = CalculateCredit( ). Class ExternalOrder inherits Order ExternalOrder PROTECTED: GetCredit( ) credit = SUPER:GetCredit( ) + extraMoney. DEV-08, Exploring Object-oriented Programming
Benefits of Inheritance and Overriding Inheritance supports modular design Common behavior put in super class and used by subclass Subclass can override to specialize behavior Inheritance is strongly-typed InternalOrder myOrder = NEW InternalOrder. myOrder.GetCredit( ). myOrder knows it is an InternalOrder DEV-08, Exploring Object-oriented Programming
Polymorphism One interface, many implementations Execution of an overridden method in a subclass from a reference to a super class superclass:method( ) 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 at runtime DEV-08, Exploring Object-oriented Programming
Polymorphism – Example Order PROTECTED: GetCredit( ) credit = CalculateCredit( ). InternalOrder ExternalOrder PROTECTED: GetCredit ( ) credit = -1. /*unlimited*/ PROTECTED: GetCredit( ) credit = SUPER:GetCredit( ) + extraCreditPoints. DEV-08, Exploring Object-oriented Programming
Polymorphism – Example continued DEFINE myOrder AS Order. if (bInternalCust = TRUE) myOrder = NEW InternalOrder( ). else myOrder = NEW ExternalOrder( ). myOrder:GetCredit( ). Super Class reference Calls InternalOrder:GetCredit( ) or ExternalOrder:GetCredit( ) DEV-08, Exploring Object-oriented Programming
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 DEV-08, Exploring Object-oriented Programming
Delegation Delegation is the use of other objects within a class Class forwards method calls to the contained object Class wraps the delegate object Creates an instance of the object Defines a “stub” method for any referenced methods that should be public No access to protected or private members DEV-08, Exploring Object-oriented Programming
Class Order references a ShipInfo object Delegation – Example Class Order references a ShipInfo object Order PRIVATE: ShipInfo shipObj = NEW ShipInfo( ) PUBLIC: GetShipDate( ) shipObj:GetDate( ) ShipInfo PRIVATE: id shipdate promisedate PUBLIC: SetDate( ) GetDate( ) … calls DEV-08, Exploring Object-oriented Programming
Benefits of Delegation Delegation supports modular design Purposed class does work Class uses delegate to provide needed functionality Class can determine what to put in API With inheritance super class dictates API; with delegation wrapper class decides what to expose DEV-08, Exploring Object-oriented Programming
That’s Object-oriented Programming What did we learn… DEV-08, Exploring Object-oriented Programming
Terminology / Concept Review Abstraction – Public API Encapsulation – Hide implementation details Hierarchy – Relationships between classes Strong-typing – Type consistency enforced Class – Data members and methods Object – Runtime instance of a class Interface – Set of method definitions; contract Inheritance – Inherit/specialize from super class Polymorphism – Most-derived method called from super class reference Delegation – Other objects do the work DEV-08, Exploring Object-oriented Programming
Benefits of OO Programming Promotes modular design Data and methods that operate on that data are contained in one place Commonalities put into super classes Code reuse through hierarchies Inheritance and delegation Strong-typing Compile-time type checking Runtime type checking DEV-08, Exploring Object-oriented Programming
Recommended OO Books Object-Oriented Analysis and Design with Applications (2nd Edition) by Grady Booch Object-Oriented Modeling and Design by James R Rumbaugh… Design Patterns, Elements of Reusable Object-oriented Software by Erich Gamma… DEV-08, Exploring Object-oriented Programming
**Attend Session DEV-10 for details In Summary Object-oriented programming is more than syntax – must be part of design Many benefits in OO Programming Can be combined with procedural programming, not all or nothing OpenEdge™ 10.1 Language enhancements support object-oriented programming naturally **Attend Session DEV-10 for details DEV-08, Exploring Object-oriented Programming
Questions? DEV-08, Exploring Object-oriented Programming
Thank you for your time! DEV-08, Exploring Object-oriented Programming
DEV-08, Exploring Object-oriented Programming