1 / 51

Exploring Object-oriented Programming

Learn the basic principles of object-oriented programming and the concepts of types, classes, objects, and interfaces. Understand how inheritance, polymorphism, and delegation work in object-oriented programming.

gaul
Download Presentation

Exploring Object-oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DEV-08: Exploring Object-oriented Programming Shelley Chase Development Architect – Progress OpenEdge

  2. Today’s Agenda • Basic Principles of Object-oriented Programming • Types, Classes, Objects, and Interfaces • Inheritance, Polymorphism, and Delegation DEV-08, Exploring Object-oriented Programming

  3. 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

  4. 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

  5. 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

  6. Designing an Object-oriented Application Progress DataSet Customer Table Customer Table Order Table Object: Order Take an Order Object: Customer Check Credit Create a Customer Object: beOrder Assign Salesperson DEV-08, Exploring Object-oriented Programming

  7. Basic Object-oriented Principles • Abstraction • Encapsulation • Hierarchies DEV-08, Exploring Object-oriented Programming

  8. Abstraction 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

  9. Abstraction - Example Object: Order CreateOrder UpdateOrder GetOrderTotal Next “What should an Order object do?” InternalOrder Two types of Orders ExternalOrder DEV-08, Exploring Object-oriented Programming

  10. Encapsulation 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

  11. Encapsulation - Example Object: Order orderNum AS INT custNum AS INT CalculatePrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) Implementation Outside View Public methods of Order class CreateOrder UpdateOrder GetOrderTotal Next CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) DEV-08, Exploring Object-oriented Programming

  12. Encapsulation - Example continued Hmm... I’d like to change CalculatePrice to CalculateTotalPrice Object: Order GetOrderTotal calls CalculatePrice( ) orderNum AS INT custNum AS INT CalculatePrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) DEV-08, Exploring Object-oriented Programming

  13. Encapsulation - Example continued This change was easy because users of the object will not be affected. Object: Order GetOrderTotal now calls CalculateTotalPrice( ) orderNum AS INT custNum AS INT CalculateTotalPrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) DEV-08, Exploring Object-oriented Programming

  14. Hierarchies 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

  15. Hierarchies - Example Order uses ShipInfo (Aggregation) Order references is a is a ExternalOrder ShipInfo InternalOrder InternalOrder and ExternalOrder inherit from Order (Inheritance) DEV-08, Exploring Object-oriented Programming

  16. 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

  17. Today’s Agenda • Basic Principles of Object-oriented Programming • Types, Classes, Objects, and Interfaces • Inheritance, Polymorphism, and Delegation DEV-08, Exploring Object-oriented Programming

  18. Type 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

  19. Type - Example Order Types: • Order • InternalOrder • Subtype of Order • ExternalOrder • SubType of Order is a is a ExternalOrder InternalOrder A subtype can appear anywhere a super type is expected DEV-08, Exploring Object-oriented Programming

  20. 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

  21. Class Class: Order orderNum AS INT custNum AS INT CalculateTotalPrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) A Class implements a Type • A Class defines and implements a user-defined type • A Class is a template (blueprint) for an object: • Data • Methods • Relationships to other classes MethodsData DEV-08, Exploring Object-oriented Programming

  22. Object MyOrder orderNum = 10 custNum = 3 Total Price = $45.00 YourOrder orderNum = 61 custNum = 58 Total Price = $318.34 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 DEV-08, Exploring Object-oriented Programming

  23. 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

  24. Interface - Example Class: Order orderNum AS INT custNum AS INT CalculateTotalPrice( ) PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) Class Order implements IList interface Interface: IList PUBLIC: Next( ) Compiler checks for method definition in implementing class DEV-08, Exploring Object-oriented Programming

  25. 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

  26. 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

  27. 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

  28. Today’s Agenda • Basic Principles of Object-oriented Programming • Types, Classes, Objects, and Interfaces • Inheritance, Polymorphism, and Delegation DEV-08, Exploring Object-oriented Programming

  29. Inheritance 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

  30. Access Levels for Class Members Order PRIVATE PROTECTED PUBLIC • PRIVATEmembers available: • Only within the class • PROTECTEDmembers available: • Within the class • Within the class hierarchy • PUBLICmembers available: • Within the class • Within the class hierarchy • To users outside the class DEV-08, Exploring Object-oriented Programming

  31. 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

  32. Inheritance Example Class: Order Class InternalOrder inherits 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. Polymorphism – Example continued DEFINEmyOrder 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

  40. 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

  41. 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

  42. Delegation – Example Order PRIVATE: ShipInfo shipObj = NEW ShipInfo( ) PUBLIC: GetShipDate( ) shipObj:GetDate( ) Class Order references a ShipInfo object ShipInfo PRIVATE: id shipdate promisedate PUBLIC: SetDate( ) GetDate( ) … calls DEV-08, Exploring Object-oriented Programming

  43. 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

  44. That’s Object-oriented ProgrammingWhat did we learn… DEV-08, Exploring Object-oriented Programming

  45. 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

  46. 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

  47. 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

  48. 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

  49. Questions? DEV-08, Exploring Object-oriented Programming

  50. Thank you for your time! DEV-08, Exploring Object-oriented Programming

More Related