Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 OBJECT-ORIENTED THINKING Ziya Karakaya Atılım University Tel: (312) 460 20 20 / 5345 Faks: (312) 460 2037 E-posta:

Similar presentations


Presentation on theme: "1 OBJECT-ORIENTED THINKING Ziya Karakaya Atılım University Tel: (312) 460 20 20 / 5345 Faks: (312) 460 2037 E-posta:"— Presentation transcript:

1 1 OBJECT-ORIENTED THINKING Ziya Karakaya Atılım University Tel: (312) 460 20 20 / 5345 Faks: (312) 460 2037 E-posta: zıya@atilim.edu.tr

2 2 Object-Oriented Paradigm ä Paradigm: “example” or “model”. ä Paradigm sentence would help you remember how to conjugate a verb in a foreign language ä A model is an example that helps you understand how the world works

3 3 Illustration of OOP Concepts : Sending Flowers to a Friend ä Suppose I wish to send flowers to a friend, Sally, who lives in a city many miles away. ä What should I do?

4 4 Agents and Communities ä Solution: Find an appropriate agent, namely Flora, and pass to her a message containing my request ä It is the responsibility of Flora to satisfy my request ä There is some method – some algorithm or some set of operations – used to satisfy my request ä This information, i.e., details, is usually hidden from my inspection.

5 5 The community of agents helping me Sally’s Florist Flora Me Delivery Person Sally Flower Arranger Wholesaler Grower Gardener

6 6 An Observation ä An object-oriented program is structured as a community of interacting agents, called objects. ä Each object has a role to play. ä Each object provides a service, or performs an action, that is used by other members of the community.

7 7 Messages and Methods ä Action is initiated in OOP by the transmission of a message to an agent (an object) responsible for the action. ä The message encodes the request for an action and is accompanied by any additional information (arguments) needed to carry out the request. ä The receiver is the object to whom the message is sent. If the receiver accepts the message, it accepts the responsibility to carry out the indicated action. ä In response to a message, the receiver will perform some method to satisfy the request.

8 8 Information Hiding Principle ä The client sending the request need not know the actual means by which the request will be honored. ä An important part of the OOP is the development of reusable components ä “thrusting” others

9 9 Message Passing vs. Procedure Calling ä PC: there is no designated receiver ä MP: Interpretation of the message is dependent on the receiver and can vary with different receivers ä Usually, the specific receiver for any given message will not be known until run time, so the determination of which method to invoke cannot be made until then. ä late binding – between a message and the code fragment (method) used to respond to the message. ä vs. compile-time or link-time binding in PC.

10 10ResponsibilitiesResponsibilities ä Describe behavior in terms of responsibilities ä increases level of abstraction ä greater independence between objects, a critical factor in solving complex problems ä Protocol: The entire collection of responsibilities associated with an object

11 11 ä “Ask not what you can do to your data structures, but what your data structures can do for you”

12 12 Classes and Instances ä We can use the term Florist to represent the category (or class) of all florists. ä I am able to make certain assumptions because I have information about florists in general, and I expect that Flora, being an instance of this category, will fit the general pattern.

13 13 ä All objects are instances of a class ä The method invoked by an object in response to a message is determined by the class of the receiver ä All objects of a given class use the same method in response to similar messages.

14 14 Class Hierarchies - Inheritance ä I have more information about Flora – not necessarily because she is a florist but because she is a shopkeeper. ä One way to think about how I have organized my knowledge of Flora is in terms of a hierarchy of categories:

15 15 The categories surrounding Flora Flora Material Object Animal Mammal Human Shopkeeper Florist

16 16 A class hierarchy of various material objects Material Object Animal Mammal Human Shopkeeper Florist Flora ArtistDentist Painter LizAdam Dog Flash Platypus Phyl Plant Flower Carnation Sally’s Flowers More abstract classes are listed near the top of the tree More specific classes and individuals are listed near the bottom.

17 17 ä The principle that knowledge of a more general category is also applicable to a more specific category is called inheritance.  The class Florist will inherit attributes of the class (or category) Shopkeeper. ä Classes can be organized into a hierarchical inheritance structure. ä A child class (or subclass) will inherit attributes from a parent class higher in the hierarchy. An abstract parent class is a class for which there are no direct instances; it is used only to create subclasses.

18 18 Method Binding, Overriding, and Exceptions  Exceptions to a general rule: Consider Phyl the Platypus ä Phyl gives birth to live offspring yet Phyl lays eggs. ä The information contained in a subclass can override information inherited from a parent class ä Most often, implementations of this approach take the form of a method in the subclass having the same name as the method in the parent class, ä combined with a rule stating how to conduct the search for a method to match a specific message.

19 19 Searching for the appropriate response ä The search for a method to invoke in response to a given message begins with the class of the receiver. ä If no appropriate method is found, the search conducted in the parent class of this class. ä The search continues up the parent class chain until either a method is found or the parent class chain is exhausted. ä In the former case, the method is executed; in the latter case, an error message is issued. ä If methods with the same name can be found higher in the class hierarchy, the method executed is said to override the inherited behavior.

20 20 ä That my wife, Liz and my florist Flora will respond to my message by different methods is an example of one form of Polymorphism. ä That I do not, need not, know exactly what method Flora will use to honor my message is an example of information hiding.

21 21 Kay's Description of Object- Oriented Programming ä Object-oriented programming is based on the principle of recursive design. 1.Everything is an object 2.Objects perform computation by making requests of each other through the passing of messages 3.Every object has it's own memory, which consists of other objects. 4.Every object is an instance of a class. A class groups similar objects. 5.The class is the repository for behavior associated with an object 6.Classes are organized into singly-rooted tree structure, called an inheritance hierarchy.

22 22 Recursive Design

23 23 Computation as Simulation ä Traditional model describing the behavior of a computer executing a program is a process-state or pigeonhole model. ä The computer is a data manager, following some pattern of instructions, wandering through memory, and publishing the results back into other slots. ä By examining the values in the slots, we can determine the state of the machine or the results produced by a computation.

24 24 Imperative Programming ä Imperative programming is the ``traditional'' model of computation. ä State ä Variables ä Assignment ä Loops ä A processing unit is separate from memory, and ``acts'' upon memory.

25 25 Visualization of Imperative Programming Sometimes called the ``pigeon-hole'' model of computation.

26 26 ä In contrast, in OO framework we never mention memory addresses, variables, assignments, or any of the conventional programming terms. ä We speak of objects, messages, responsibility for some action. ä Instead of a bit-grinding processor... plungering data structures, we have a universe of well- behaved objects that courteously ask each other to carry out their various desires.

27 27 ä Similar to discrete event simulation ä user creates computer models of the various elements of the simulation ä describes how they will interact with one another ä sets them moving ä In OOP, computation is a simulation.

28 28 The Power of Metaphor ä “Unlike the usual programming method – writing software one line at a time – NeXT’s “object- oriented” system offers larger building blocks that developers can quickly assemble the way a kid builds faces on Mr. Potato Head.” ä Easier to teach OOP concepts to computer novices than to computer professionals. ä Programming is as like the task of “training” the universe of agents to interact smoothly with each other, each providing a certain small and well- defined service to the others, each contributing to the effective execution of the whole.


Download ppt "1 OBJECT-ORIENTED THINKING Ziya Karakaya Atılım University Tel: (312) 460 20 20 / 5345 Faks: (312) 460 2037 E-posta:"

Similar presentations


Ads by Google