Title: DEV-08: Exploring Object-oriented Programming
1DEV-08 Exploring Object-oriented Programming
- Shelley Chase
- Development Architect Progress OpenEdge
2Todays Agenda
- Basic Principles of Object-oriented Programming
- Types, Classes, Objects, and Interfaces
- Inheritance, Polymorphism, and Delegation
3What 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)
4Object-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
5Object-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
6Designing an Object-oriented Application
Object Order
Take an Order
Object Customer
Check Credit
Create a Customer
Object beOrder
Assign Salesperson
7Basic Object-oriented Principles
- Abstraction
- Encapsulation
- Hierarchies
8Abstraction
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
9Abstraction - Example
What should an Order object do?
InternalOrder
Two types of Orders
ExternalOrder
10Encapsulation
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
11Encapsulation - Example
- Implementation Outside View
Public methods of Order class
CreateOrder UpdateOrder GetOrderTotal Next
CreateOrder( ) UpdateOrder( ) GetOrderTotal(
) Next( )
12Encapsulation - Example continued
Hmm... Id like to change
CalculatePrice to CalculateTotalPrice
Object Order
orderNum AS INT custNum AS INT CalculatePrice(
)
PUBLIC CreateOrder( ) UpdateOrder(
) GetOrderTotal( ) Next( )
- GetOrderTotal calls CalculatePrice( )
13Encapsulation - Example continued
This change was easy because users of the object
will not be affected.
Object Order
orderNum AS INT custNum AS INT CalculateTotalPri
ce( )
PUBLIC CreateOrder( ) UpdateOrder(
) GetOrderTotal( ) Next( )
- GetOrderTotal now calls CalculateTotalPrice( )
14Hierarchies
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
15Hierarchies - Example
Order uses ShipInfo (Aggregation)
Order
references
is a
is a
ExternalOrder
ShipInfo
InternalOrder
InternalOrder and ExternalOrder inherit from
Order (Inheritance)
16Summary 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
17Todays Agenda
- Basic Principles of Object-oriented Programming
- Types, Classes, Objects, and Interfaces
- Inheritance, Polymorphism, and Delegation
18Type
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
19Type - Example
- Types
- Order
- InternalOrder
- Subtype of Order
- ExternalOrder
- SubType of Order
Order
is a
is a
ExternalOrder
InternalOrder
A subtype can appear anywhere a super type is
expected
20Benefits of Types (Strong-Typing)
- Compile time checking for type consistency
- myObj mySubObject. (must be subType)
- myObjmethod(). (validates signature)
- myObjdata 3. (validates data type)
- Results in safer, bug-free code because all code
paths checked at compile time
21Class
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
Methods Data
22Object
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
23Interface
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
24Interface - Example
- Class Order implements IList interface
Interface IList
PUBLIC Next( )
Compiler checks for method definition in
implementing class
25Interface 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 ).
26Benefits 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
27Summary 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
28Todays Agenda
- Basic Principles of Object-oriented Programming
- Types, Classes, Objects, and Interfaces
- Inheritance, Polymorphism, and Delegation
29Inheritance
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
30Access Levels for Class Members
- PRIVATE members available
- Only within the class
- PROTECTED members available
- Within the class
- Within the class hierarchy
- PUBLIC members available
- Within the class
- Within the class hierarchy
- To users outside the class
31Inheritance Example
Class Order
PRIVATE orderNum AS INT custNum AS
INT CalculateTotalPrice( )
PROTECTED GetCredit( ) PUBLIC CreateOrder(
) UpdateOrder( ) GetOrderTotal( ) Next( )
32Inheritance 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( )
33Inheritance 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
34Method Overriding Example 1
Order
PROTECTED GetCredit( ) credit
FindCreditScore( ).
- Class InternalOrder inherits Order
InternalOrder
PROTECTED GetCredit ( ) credit -1.
/unlimited/
35Method Overriding Example 2
Order
PROTECTED GetCredit( ) credit
CalculateCredit( ).
- Class ExternalOrder inherits Order
ExternalOrder
PROTECTED GetCredit( ) credit
SUPERGetCredit( )
extraMoney.
36Benefits 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
37Polymorphism
One interface, many implementations
- Execution of an overridden method in a subclass
from a reference to a super class - superclassmethod( )
- 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
38Polymorphism Example
Order
PROTECTED GetCredit( ) credit
CalculateCredit( ).
InternalOrder
ExternalOrder
PROTECTED GetCredit ( ) credit -1.
/unlimited/
PROTECTED GetCredit( ) credit
SUPERGetCredit( )
extraCreditPoints.
39Polymorphism Example continued
DEFINE myOrder AS Order. if (bInternalCust
TRUE) myOrder NEW InternalOrder( ). else
myOrder NEW ExternalOrder( ). myOrderGetCred
it( ).
Super Class reference
Calls InternalOrderGetCredit( ) or
ExternalOrderGetCredit( )
40Benefits 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
41Delegation
- 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
42Delegation Example
- Class Order references a ShipInfo object
ShipInfo
PRIVATE id shipdate promisedate PUBLIC SetDate
( ) GetDate( )
calls
43Benefits 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
44Thats Object-oriented ProgrammingWhat did we
learn
45Terminology / 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
46Benefits 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
47Recommended 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
48In 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
49Questions?
50Thank you for your time!
51(No Transcript)