Presentation is loading. Please wait.

Presentation is loading. Please wait.

DEV-41: The Power of Polymorphism

Similar presentations


Presentation on theme: "DEV-41: The Power of Polymorphism"— Presentation transcript:

1 DEV-41: The Power of Polymorphism
Polymorphism is a powerful concept in object-oriented programming. It allows the developer to design flexible, service-oriented applications in a very efficient, maintainable manner. This session covers, in some detail, what polymorphism is and how it can be used to operate on application-specific data through a common interface. We’ll include a general discussion on the subject as well as introductions to the OpenEdge object-oriented language constructs for developing effective polymorphic routines. Polymorphism is the idea that specific actions can be performed though a common interface This makes it very easy to design flexible, efficient, maintainable applications. DEV-41: The Power of Polymorphism Sarah Marshall QA Architect

2 Polymorphism -- Agenda
Having many forms As it applies to object-oriented programming Response to a single message or request can take many forms 2. Taking advantage of polymorphism allows for… Modular, generic programming Reuse of common behavior to create servicable routines that are easy to plug into an application The common behavior can be easily customized Therefore, can make common requests without having to know about the specific types Agenda Talk about what polymorphism is How to implement it using inheritance and method overriding How to implement it using interfaces How to take advantage of polymorphism through dynamic programming for truly generic code What is polymorphism Inheritance and method overriding Implementing interfaces Dynamic programming DEV-41: The Power of Polymorphism

3 What Do Super Procedures Give You?
Polymorphism is the mechanism by which you can ask a generic object for something, and get a specific response depending on what the specific type of object it is. Who is using Super Procedures? Using super procedures you can define common behavior in one routine. You can then add that routine to your procedure and it becomes available when you need to access that common behavior You can customize the behavior by adding specialized code before or after you run the common code Not strongly typed – error prone, you can’t know at compile time that your calls are going to be correct – compiler wont’ assist you. Not dynamic – have to know what super procedures to add under what conditions. Late-bound (types are checked, parameters verified etc until run-time) can’t do really generic programming. /** super.p **/ PROCEDURE DoIt: /* common behavior*/ END PROCEDURE. Some ability to do generic programming Some ability to customize behavior Some ability to customize the response to a message /** run.p **/ RUN Super.p SET hDoIt. h:ADD-SUPER-PROCEDURE(hDoIt). PROCEDURE DoIt: /* customized behavior */ RUN SUPER. END PROCEDURE. The beginnings of polymorphism! DEV-41: The Power of Polymorphism

4 International Customer
What is Polymorphism? Behavior common to all types Customer POLYMORPHISM: the idea that one message or request can result in many specific actions or responses depending on the type of object responding Establish some groundwork for understanding the object oriented programming model So what is an object… An object has properties that describe it An object has behaviors that manipulate it Like a lamp – properties (size, color, its state – on or off) and behaviors (turn on and turn off) Objects in an application Customers Orders Employees Vendors Products Lots of different kinds of objects can share common behaviors – CUSTOMER But within that category are subsets of objects that have qualities specific to their individual types – domestic customers, international customers etc… So given this model, polymorphism is the mechanism by which I can request behavior for a generic Customer And get a response specific to the type of customer it is. Customized behavior for specific types Domestic Customer International Customer Preferred Customer Internal Customer DEV-41: The Power of Polymorphism

5 Inheritance Customer Domestic Customer Preferred Customer
Define a super class that establishes a basic set of data and behavior Many different types of objects will require this behavior, or some form of it, so they “inherit” that data and behavior. With one statement the subtype gets all common behavior defined in the super class. Hence, the specific types do not each need to define this behavior – they get it for FREE Everything that’s defined for the base class is now available to its subclasses Subclasses can also then define their own specific behavior Contact Information Calculate Shipping Open Log File Print Reports Customer Contact Information Calculate Shipping Open Log File Print Reports Contact Information Calculate Shipping Open Log File Print Reports Domestic Customer Preferred Customer Alternate billing address Subscription to mailing list CLASS DomCust INHERITS Cust CLASS PreCust INHERITS Cust DEV-41: The Power of Polymorphism

6 Vocabulary OBJECT An entity with properties that describe it and
behaviors that manipulate it OBJECT State, behavvior and identity – basic building block of object-oriented software design Something you can do things to a data structure and a set of operations that can manipulate the data A program design is broken up into functional objects TYPE A named category of data that is characterized by a set of values together with a format for denote these values A collection of operations that interpret and manipulate the values. SUPER CLASS Class that describes common data and behavior for a generic category of objects Defines methods that perform generic operations that all members of this category have in common SUBCLASS Class that inherits from a SUPER CLASS. By inheriting from another class it gets for free all of the data and behavior defined in that class Many specific types of objects can inherit from one common super class The specific types may also provide behavior that is specific to that individual kind of object TYPE A category of data that can have values, a format for denoting them, and operations that act upon them SUPER CLASS A class that describes common data and behavior for a category of objects SUBCLASS A class that inherits a super type, acquiring the common data and behavior, as well as describing its own DEV-41: The Power of Polymorphism

7 User-defined Types DEFINE VARIABLE myvar AS INTEGER.
Defining a class creates a TYPE. define a variable of that type – the variable with which you refer to the object – OBJECT REFERENCE Built in types: INTEGER, CHARACTER, HANDLE etc… DEFINE VARIABLE myvar AS INTEGER. Customer User-defined types DEFINE VARIABLE myvar AS CLASS Cust. DEV-41: The Power of Polymorphism

8 Object References DEFINE VARIABLE DomObj AS CLASS DomCust.
DomObj = NEW DomCust(). DomObj refers to an instance of a Domestic Customer DEFINE VARIABLE CustObj AS CLASS Cust. CustObj = NEW DomCust(). Custobj refers to an instance of a Domestic Customer or an International Customer or a Preferred Customer or any other subclass of Cust… Define an object reference for a specific type Use the NEW statement to create an instance of that object – the variable will refer to that instance An object reference defined for a specific type can be used to refer only to an instance of that type and everything that it has access –- its inherited data and behavior and its own, specialized data and behavior Define an object reference for a super type allows for more dynamic programming Use the NEW statement to create an instance of the super type OR any subclass that inherits from it – the variable will refer to that instance if you define it as the super class you don’t need to know up front which of its subclasses you are going to be referring to. BUT you only have access to what it inherited from the super class The behavior defined by the generic, super type is part of the subclasses. DEV-41: The Power of Polymorphism

9 Inheritance and Object References
DomesticCustomer Because the subclass inherits from its super class it can be said to be “a kind of” the super class. It therefore works conceptually to talk about Domestic Customer generally as a kind of Customer. This means, as I mentioned, that I can use a reference to the super class type to refer to an instance of any of its subclasses And using that reference I can access any of the common functionality that is defined by the super class. Define very generic variable for the super class type Use it to define two different subclass types Because the variable is defined for the super type I can be used to invoke any method provided by that type, without knowing what specific subtype it refers to. Taking a look at the message statement you see how generic this code looks. Customer Is a kind of Preferred Customer Customer Is a kind of DEF VARIABLE CustObj1 AS CLASS Cust. DEF VARIABLE CustObj2 AS CLASS Cust. CustObj1 = NEW DomCust( ). CustObj2 = NEW PreCust( ). MESSAGE CustObj1:CalcShipping( ). MESSAGE CustObj2:CalcShipping( ). DEV-41: The Power of Polymorphism

10 Accessing Additional Information
If the object reference is defined for the subtype, it has access to everything, so what happens when its defined for the super type? What about the methods defined in the subclass specific to those types? Using the generic object reference gives you access to the generic, common methods. Get to methods that are only defined for this specific type by CASTing. What does this do… It’s the compiler that disallows you from accessing those subtype methods So the CAST tells the compiler that at runtime you know you will have an instance of that subtype. Basically telling the compiler to trust you, you know what you’re doing. Contact Information Calculate Shipping Open Log File Print Reports Customer Contact Information Calculate Shipping Open Log File Print Reports Contact Information Calculate Shipping Open Log File Print Reports Domestic Customer Preferred Customer Alternate billing address Subscription to mailing list CAST(CustObj1, PreCust):AddToMailList DEV-41: The Power of Polymorphism

11 Method Overriding Methods defined in super class
We’ve established that the generic super type defines common behavior. We’ve established that the subclass that inherits it has access to it What if the subclass that inherits it wants to do something a little different Each specific sub type might have something special it need to do consistent with its particular type, so we OVERRIDE. Now there are two versions of the methods – the generic one in the super class, the more specific one in the sub class The common behavior is defined in the subclass, and the behavior is customized in the sub class. Methods defined in super class Behavior common to all types Methods overridden in subclass Behavior specific to the type Access to the specific data Specialized calculations Customer METHOD PUBLIC INTEGER CalcShipping( ): Domestic Customer METHOD PUBLIC OVERRIDE INTEGER CalcShipping( ): DEV-41: The Power of Polymorphism

12 Inheritance and Method Overriding
THIS IS WHERE THE POLYMORPHISM KICKS IN Because I can use a variable for the generic type to invoke the methods it defines, I don’t need to know which of the specific types is going to respond. Invoking the method on a variable defined for the super type actually runs the method defined in the subtype. The common method is invoked, but the overriden method runs. Domestic Customer Customer $13.99 How much is shipping? CalcShipping() Where is the customer located? Spring St, Boston GetCustomerInfo() DEV-41: The Power of Polymorphism

13 Inheritance and Method Overriding
THIS IS WHERE THE POLYMORPHISM KICKS IN Because I can use a variable for the generic type to invoke the methods it defines, I don’t need to know which of the specific types is going to respond. Invoking the method on a variable defined for the super type actually runs the method defined in the subtype. The common method is invoked, but the overriden method runs. Customer FREE How much is shipping? CalcShipping() Preferred Customer Where is the customer located? Main St, Chicago GetCustomerInfo() DEV-41: The Power of Polymorphism

14 What’s Actually Happening…
(red box) This is the polymorphic behavior  this is where the method invoked on the super class variable actually runs the overridden version of the method defined in the more specific subclasses 1. Define a generic variable CustObj for a Customer object Customer Domestic Customer 2. Assign the variable to a new Domestic Customer object 3. Invoke CalcShipping() CustObj:CalcShipping() 4. Overridden method executes in Domestic Customer, not Customer Shipping = $13.99 DEV-41: The Power of Polymorphism

15 Accessing Common Behavior
Pre-processing, run super class method prior to specific behavior Post-processing, run subclass method after specific behavior There are two things to consider in the overriding method First, the overriding method can access the common behavior overridden method in the super class. Can certainly replace the implementation defined in the super type OR could augment the behavior in the super type Call it, then execute custom behavior Execute customer behavior then call it. METHOD PUBLIC OVERRIDE INT CalcShipping( ): /* type-specific behavior */ SUPER:CalcShipping( ). END METHOD. DEV-41: The Power of Polymorphism

16 Accessing Common Behavior
Pre-processing, run super class method prior to specific behavior Post-processing, run subclass method after specific behavior METHOD PUBLIC OVERRIDE INT CalcShipping( ): SUPER:CalcShipping( ). /* type-specific behavior */ END METHOD. DEV-41: The Power of Polymorphism

17 Accessing Common Behavior
Subclass has access to common behaviors defined in the super class Choose to do pre-processing and invoke the common behavior before applying type-specific behavior Second, the subclass can access all the common behavior in the super class. 1. Can also access all other common behaviors defined in the super type that I did not need to augment So for a particular request choose to execute common response to that request then my customized response to that request then other common behavior defined in the super class. METHOD PUBLIC OVERRIDE INT CalcShipping( ): SUPER:CalcShipping(). /* type-specific behavior */ PrintReport( ). END METHOD. DEV-41: The Power of Polymorphism

18 Accessing Common Behavior
POLYMORPHISM IN ACTION So now…I can make the same request using the generic object reference defined for the super class. First the common behavior will run Then the customized behavior specific to this type Then the common method that was not overriden, but available to all subclasses To get the final result and print the shipping report. Customer How much is the shipping? CalcShipping() Domestic Customer Calculate Common shipping charges $13.99 Print the report DEV-41: The Power of Polymorphism

19 What’s Actually Happening…
(red box) The polymorphic behavior is here  this is how the method invoked on the generic super class variable actually runs specific behavior defined for the particular type it refers to. 1. Using generic variable CustObj, invoke CalcShipping(). CustObj:CalcShipping() 2. Overridden method executes in Domestic Customer, not Customer METHOD PUBLIC OVERRIDE INT CalcShipping( ): SUPER:CalcShipping(). /* type-specific behavior */ PrintReport( ). END METHOD. DEV-41: The Power of Polymorphism

20 Interfaces ICustomer Domestic Customer Preferred Customer
Define an interface which describes a set of behavior, but does not provide implementation for it It is DECLARATIVE in that there is no behavior or default implementation It establishes a contract which requires the implementing class to define certain behaviors, and guarantees to the user of the class that certain behaviors will be available. Any class that implements the interface is required to provide implementation for the behaviors described by it. So…the methods declared in the interface need to be defined in each class that implements it BUT…they do not need to be the same – the are different and specific for each type that implements the interface. Get Credit Status Calculate Order Total ICustomer Domestic Customer Get Credit Status Calculate Order Total CLASS DomCust INHERITS Cust IMPLEMENTS ICust Get Credit Status Calculate Order Total Preferred Customer CLASS DomCust INHERITS Cust IMPLEMENTS ICust DEV-41: The Power of Polymorphism

21 Object References DEFINE VARIABLE DomObj AS CLASS DomCust.
DomObj = NEW DomCust(). DomObj refers to an instance of a Domestic Customer DEFINE VARIABLE CustObj AS CLASS ICust. CustObj = NEW DomCust(). Custobj refers to an instance of a Domestic Customer or an International Customer or a Preferred Customer or any class that implements ICust… As before, one thing you can do is define a variable for a specific type …use it with the NEW statement to create an instance of that type You can then access all the methods defined for that type However, you could also define a very generic variable for the interface type – it’s just another kind of type. …use it with the NEW statement to create an instance of any type that implements that interface Don’t need to know what it’s going to be use used for – it is very generic and can be used for anything that implements the interface DEV-41: The Power of Polymorphism

22 The Interface Guarantee
Domestic Customer THE INTERFACE GUARANTEE Because the class implements the common methods declared in the interface it can be said to have the behavior described by the interface It therefore works conceptually to talk about Domestic Customer generally as having the behavior described by the interface This means, as I mentioned, I can use a reference of to the interface to refer to an instance of any class that implements it And using that reference I can access any of the common functionality that is described by the interface I Don’t need to know what the specific type is going to be – if it implements the interface then it is guaranteed to have access to the behaviors. Define very generic variable for the interface type Use it to define two different classes that implement the interface Because the variable is defined for the interface I can be used it to invoke any method declared in the interface, without knowing what specific subtype it refers to. Taking a look at the message statement you see how generic this code looks. ICustomer implements Preferred Customer ICustomer implements DEF VARIABLE CustObj1 AS CLASS ICust. DEF VARIABLE CustObj2 AS CLASS ICust. CustObj1 = NEW DomCust( ). CustObj2 = NEW PreCust( ). MESSAGE CustObj1:CalcOrderTotal( ). MESSAGE CustObj2:CalcOrderTotal( ). DEV-41: The Power of Polymorphism

23 One Message, Different Responses
How much is the order total? So using my generic variable, defined for the interface, I can execute a method that asks – HOW MUCH IS THE ORDER TOTAL? ICustomer ICustomer DEV-41: The Power of Polymorphism

24 One Message, Different Responses
How much is the order total? The specific type to which it refers will have defined an implementation that is specific and therefore have a different response to the question A domestic customer calculates ground shipping and applicable tax A preferred customer gets a 10% discount and FREE shipping The code that invokes the method is identical, but the code that actually executes in response to the request is different. Domestic Customer Preferred Customer Total = $171.49 Total = $135.00 Subtotal $150.00 Ground shipping $ Domestic tax $ Order Total $ Subtotal $150.00 FREE shipping $ Discount < > Order Total $ DEV-41: The Power of Polymorphism

25 What’s Actually Happening…
(red box) This is the polymorphic behavior  The interface itself does not have an implementation, but by defining the object reference variable as the interface you are guaranteed to have the behavior of the methods it describes in any class that implements it. 1. Define a generic variable CustObj for ICust interface ICustomer Domestic Customer 2. Assign the variable to a new Domestic Customer object 3. Invoke CalcOrderTotal() CustObj:CalcOrderTotal() 4. Method is guaranteed by the interface, and executes in Domestic Customer Total = $171.49 DEV-41: The Power of Polymorphism

26 Dynamic Programming With OERA there is a set of objects treated as business entities. You want to have methods that act on a business entity. Could do that by hardcoding a CASE or an IF in order to NEW the right guy. Something changes, have to update the CASE/IF CustObj Can be defined for the interface or the super class METHOD PUBLIC DomCust MakeCustObj (INPUT CustType AS CHARACTER): DEFINE VARIABLE CustObj AS CLASS Cust. CASE CustType: WHEN “DomCust” THEN CustObj = DomCust( ). WHEN “PreCust” THEN CustObj = PreCust( ). END CASE. RETURN CustObj. END METHOD. DEV-41: The Power of Polymorphism

27 Dynamic Programming The NEW statements that we saw created an instance of class type that was known at the time the code was written. However the DYNAMIC-NEW can be used to make this an even more generic routine. At compile time you only need to know that you will have an instance of a class that implements the interface, or an instance of the super class. At runtime A character expression evaluates to a string that identifies a class type you will instantiate the specific sub-type that your application requires and invoke the common methods on it. That’s where polymorphism will kick in and the specific behavior defined for the individual types will execute, giving you a very generic routine that is useful anywhere you work with Customer object, in this case. Define a variable here, but it could be an input parameter or return value. It’s very explicit here for the purpose of demonstration. But you can imagine the situation where CustType is not hardcoded but determined at runtime, on the fly. Also, recognizing that the more specific types implement behavior that is not part of the common set, but specific to that type, the DYNAMIC-CAST is available, which evaluates a character expression to identify the specific class type you are using, and cast that object in order to invoke it’s own specific methods. CustObj Can be defined for the interface or the super class METHOD PUBLIC DomCust MakeCustObj (INPUT CustType AS CHARACTER): DEFINE VARIABLE CustObj AS CLASS Cust. CustObj = DYNAMIC-NEW (CustType + “Cust”). RETURN CustObj. END METHOD. DEV-41: The Power of Polymorphism

28 In Summary Response to one message takes many forms
So that’s an overview of polymorphism and some aspects of object oriented programming. The notion that a generic routine can be developed to respond to a request, and yet provide very specific, individualized results goes a long way in designing modular, reusable routines. This is provided either through inheritance and method overriding, or through the use of interfaces, but the idea that a single API can be used by many different kinds of objects, and yet provide for customization at the level of each individual type creates a great deal of flexibility in object oriented program design Response to one message takes many forms One API used by many objects allows for generic programming Creates customizable, reusable and maintainable code. ` DEV-41: The Power of Polymorphism

29 ? Questions DEV-41: The Power of Polymorphism

30 Thank You DEV-41: The Power of Polymorphism

31 DEV-41: The Power of Polymorphism


Download ppt "DEV-41: The Power of Polymorphism"

Similar presentations


Ads by Google