DEV08 Exploring Objectoriented Programming Shelley Chase Development Architect

DEV-08: Exploring Object-oriented Programming Shelley Chase Development Architect – Progress Open. Edge

Today’s Agenda n Basic Principles of Objectoriented Programming n Types, Classes, Objects, and Interfaces n Inheritance, Polymorphism, and Delegation 2 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

What are Objects? n You interact with objects everyday – Your car – The telephone – A customer – An order n All objects contains state and behavior – What they can do and what changes when they do n Software objects represent these as: – Data – Methods ( like 4 GL variables ) ( like 4 GL procedures) 3 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Object-oriented Programming “Object-oriented programming is a method of implementation which programs are organized as cooperative collections of objects, each of which represents instance of some class. . . ” in an Grady Booch 4 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Object-oriented Application Development n A way to design and build applications – Objects bundle together data (state) and methods (behavior) – Objects facilitate separating definition from implementation n Much more than just syntax – You might have already done object-oriented programming in the 4 GL 5 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Designing an Object-oriented Application Object: Order Take an Order Object: Customer Create a Customer Assign Salesperson Customer Table 6 © 2005 Progress Software Corporation Check Credit Object: be. Order Customer Table Progress Data. Set Order Table DEV-08, Exploring Object-oriented Programming

Basic Object-oriented Principles n Abstraction n Encapsulation n Hierarchies 7 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Abstraction Public View of an Object n Abstraction is used to manage complexity – Focus on the essential characteristics – Eliminate the details – Find commonalities among objects n Defines the public contract – Public definition for users of the object – The “Outside view” – Independent of implementation 8 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Abstraction - Example Object: Order Create. Order Update. Order Get. Order. Total Next 9 © 2005 Progress Software Corporation “What should an Order object do? ” Internal. Order Two types of Orders External. Order DEV-08, Exploring Object-oriented Programming

Encapsulation Hide Implementation Details n Encapsulation hides implementation – Promotes modular software design – data and methods together – Data access always done through methods – Often called “information hiding” n Provides two kinds of protection: – State cannot be changed directly from outside – Implementation can change without affecting users of the object 10 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Encapsulation - Example Implementation Outside View Object: Order order. Num AS INT cust. Num AS INT Calculate. Price( ) PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) 11 © 2005 Progress Software Corporation Public methods of Order class Create. Order Update. Order Get. Order. Total Next DEV-08, Exploring Object-oriented Programming

Encapsulation - Example continued Object: Order order. Num AS INT cust. Num AS INT Calculate. Price( ) PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) 12 © 2005 Progress Software Corporation Hmm. . . I’d like to change Calculate. Price to Calculate. Total. Price Get. Order. Total calls Calculate. Price( ) DEV-08, Exploring Object-oriented Programming

Encapsulation - Example continued Object: Order order. Num AS INT cust. Num AS INT Calculate. Total. Price( ) PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) 13 © 2005 Progress Software Corporation This change was easy because users of the object will not be affected. Get. Order. Total now calls Calculate. Total. Price( ) DEV-08, Exploring Object-oriented Programming

Hierarchies Object Relationships n 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 n Two important hierarchy types: – Inheritance – Aggregation 14 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Hierarchies - Example Order is a Internal. Order is a External. Order uses Ship. Info (Aggregation) references Ship. Info Internal. Order and External. Order inherit from Order (Inheritance) 15 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Summary : Object-oriented Principles n Abstraction – Break up complex problem – Focus on public view, commonalities n Encapsulation – Hide implementation details – Package data and methods together n Hierarchies – Build new objects by combining or extending other objects 16 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Today’s Agenda n Basic Principles of Objectoriented Programming n Types, Classes, Objects, and Interfaces n Inheritance, Polymorphism, and Delegation 17 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Type A Type is a definition n A Type defines the state and behavior – Identifies inheritance relationships with other types – No concern for implementation n Enables strong-typing – Early binding - types determined at compile time – Type-consistency enforced at compile time and runtime 18 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Type - Example Types: n Order is a Internal. Order is a n – Subtype of Order External. Order n A subtype can appear anywhere a super type is expected 19 © 2005 Progress Software Corporation Internal. Order External. Order – Sub. Type of Order DEV-08, Exploring Object-oriented Programming

Benefits of Types (Strong-Typing) n Compile time checking for type consistency my. Obj = my. Sub. Object. my. Obj: method(…). my. Obj: data = 3. n (must be sub. Type) (validates signature) (validates data type) Results in safer, bug-free code because all code paths checked at compile time 20 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Class A Class implements a Type n A Class is a template (blueprint) for an object: – Data – Methods – Relationships to other classes 21 © 2005 Progress Software Corporation Data A Class defines and implements a userdefined type order. Num AS INT cust. Num AS INT Calculate. Total. Price( ) Methods n Class: Order PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) DEV-08, Exploring Object-oriented Programming

Object An Object is an instance of a Class n An Object is created at runtime – Maintains independent state in data members – Code shared among object instances n The term Object is often used to refer to both classes and instances 22 © 2005 Progress Software Corporation My. Order order. Num = 10 cust. Num = 3 Total Price = $45. 00 Your. Order order. Num = 61 cust. Num = 58 Total Price = $318. 34 DEV-08, Exploring Object-oriented Programming

Interface An Interface implements a Type n An Interface is a collection of method definitions for a set of behaviors – a “contract” – No implementation provided n A Class can implement an interface – Must implement all methods in the interface n Behavior can be specialized – Compiler validates implementation of interface 23 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Interface - Example Class Order implements IList interface Interface: IList PUBLIC: Next( ) Compiler checks for method definition in implementing class 24 © 2005 Progress Software Corporation Class: Order order. Num AS INT cust. Num AS INT Calculate. Total. Price( ) PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) DEV-08, Exploring Object-oriented Programming

Interface – Example continued n Write generic routine using interface Move. Next( list. Obj AS IList ) list. Obj. Next( ). /* Calls method in real object */ n Call with any object that implements IList my. Order = NEW Order( ). Move. Next( my. Order ). /* implements IList */ or my. Cust = NEW Customer( ). /* implements IList */ Move. Next( my. Cust ). 25 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Benefits of Interfaces n 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 n IList example allows any collection of objects to be navigated using Next( ) – Behavior can be specialized as needed 26 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Summary : Object-oriented Constructs n Type – Enforces type consistency at compile time n Class – Defines type with data and methods and provides implementation n Object – Runtime instantiation of class n Interface – Defines type with methods – no implementation provided 27 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Today’s Agenda n Basic Principles of Objectoriented Programming n Types, Classes, Objects, and Interfaces n Inheritance, Polymorphism, and Delegation 28 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Inheritance Relationship between Classes n Super Class (Base class) Super Class – Provides common functionality and data members n … Subclass (Derived class) – Inherits public and protected members from the super class – Can extend or change behavior of super class by overriding methods 29 © 2005 Progress Software Corporation Subclass DEV-08, Exploring Object-oriented Programming

Access Levels for Class Members n PRIVATE members available: – Only within the class n Order PROTECTED members available: PRIVATE – Within the class hierarchy n PUBLIC members available: PROTECTED PUBLIC – Within the class hierarchy – To users outside the class 30 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Inheritance Example Class: Order PRIVATE: order. Num AS INT cust. Num AS INT Calculate. Total. Price( ) PROTECTED: Get. Credit( ) PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) 31 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Inheritance Example Class: Order PRIVATE: order. Num AS INT cust. Num AS INT Calculate. Total. Price( ) PROTECTED: Get. Credit( ) PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) 32 © 2005 Progress Software Corporation Class Internal. Order inherits Order Internal. Order PROTECTED: Get. Credit( ) PUBLIC: Create. Order( ) Update. Order( ) Get. Order. Total( ) Next( ) DEV-08, Exploring Object-oriented Programming

Inheritance and Method Overriding n Method overriding used to specialize behavior – Subclass may override a method in its super class (hierarchy) – Method signatures must match n Overriden method can: – Completely override behavior of super class – Augment behavior by providing its own behavior and calling super class method 33 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Method Overriding – Example 1 Order Class Internal. Order inherits Order PROTECTED: Get. Credit( ) credit = Find. Credit. Score( ). Internal. Order PROTECTED: Get. Credit ( ) credit = -1. /*unlimited*/ 34 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Method Overriding – Example 2 Order Class External. Order inherits Order PROTECTED: Get. Credit( ) credit = Calculate. Credit( ). External. Order PROTECTED: Get. Credit( ) credit = SUPER: Get. Credit( ) + extra. Money. 35 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Benefits of Inheritance and Overriding n Inheritance supports modular design – Common behavior put in super class and used by subclass – Subclass can override to specialize behavior n Inheritance is strongly-typed Internal. Order my. Order = NEW Internal. Order. my. Order. Get. Credit( ). my. Order knows it is an Internal. Order 36 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Polymorphism One interface, many implementations n Execution of an overridden method in a subclass from a reference to a super class superclass: method( ) n 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 37 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Polymorphism – Example Order PROTECTED: Get. Credit( ) credit = Calculate. Credit( ). Internal. Order External. Order PROTECTED: Get. Credit ( ) credit = -1. /*unlimited*/ PROTECTED: Get. Credit( ) credit = SUPER: Get. Credit( ) + extra. Credit. Points. 38 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Polymorphism – Example continued DEFINE my. Order AS Order. if (b. Internal. Cust = TRUE) my. Order = NEW Internal. Order( ). else my. Order = NEW External. Order( ). Super Class reference my. Order: Get. Credit( ). Calls Internal. Order: Get. Credit( ) or External. Order: Get. Credit( ) 39 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Benefits of Polymorphism n Supports generic programming using super class or interface – Type used at compile time is super class or interface n Specialized behavior is called at runtime automatically – Built on inheritance and overriding 40 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Delegation n Delegation is the use of other objects within a class – Class forwards method calls to the contained object n 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 41 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Delegation – Example Class Order references a Ship. Info object Order Ship. Info PRIVATE: Ship. Info ship. Obj = NEW Ship. Info( ) PRIVATE: id shipdate promisedate PUBLIC: Get. Ship. Date( ) ship. Obj: Get. Date( ) PUBLIC: Set. Date( ) Get. Date( ) … 42 © 2005 Progress Software Corporation calls DEV-08, Exploring Object-oriented Programming

Benefits of Delegation n Delegation supports modular design – Purposed class does work – Class uses delegate to provide needed functionality n Class can determine what to put in API – With inheritance super class dictates API; with delegation wrapper class decides what to expose 43 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

That’s Object-oriented Programming What did we learn… 44 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Terminology / Concept Review Abstraction Encapsulation Hierarchy – Public API – Hide implementation details – Relationships between classes n Strong-typing Class Object Interface Inheritance Polymorphism n Delegation – Type consistency enforced – Data members and methods – Runtime instance of a class – Set of method definitions; contract – Inherit/specialize from super class – Most-derived method called from super class reference – Other objects do the work n n n n 45 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Benefits of OO Programming n Promotes modular design – Data and methods that operate on that data are contained in one place – Commonalities put into super classes n Code reuse through hierarchies – Inheritance and delegation n Strong-typing – Compile-time type checking – Runtime type checking 46 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Recommended OO Books n Object-Oriented Analysis and Design with Applications (2 nd Edition) by Grady Booch n Object-Oriented Modeling and Design by James R Rumbaugh… n Design Patterns, Elements of Reusable Objectoriented Software by Erich Gamma… 47 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

In Summary n Object-oriented programming is more than syntax – must be part of design n Many benefits in OO Programming – Can be combined with procedural programming, not all or nothing n Open. Edge™ 10. 1 Language enhancements support object-oriented programming naturally **Attend Session DEV-10 for details 48 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Questions? 49 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

Thank you for your time! 50 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming

51 © 2005 Progress Software Corporation DEV-08, Exploring Object-oriented Programming
- Slides: 51