11
OBJECT-ORIENTED THINKINGOBJECT-ORIENTED THINKINGOBJECT-ORIENTED THINKINGOBJECT-ORIENTED THINKING
Ziya KarakayaZiya Karakaya
AtAtılım Universityılım UniversityTel: (312) 460 20 20 / 5345 Faks: (312) 460 2037 Tel: (312) 460 20 20 / 5345 Faks: (312) 460 2037
E-posta: zı[email protected]: zı[email protected]
22
Object-Oriented Object-Oriented ParadigmParadigmObject-Oriented Object-Oriented ParadigmParadigm
ParadigmParadigm: “: “exampleexample”” or or ““modelmodel””.. Paradigm sentence would help you Paradigm sentence would help you
remember how to conjugate a verb in remember how to conjugate a verb in a foreign languagea foreign language
A model is an example that helps you A model is an example that helps you understand how the world worksunderstand how the world works
33
Illustration of OOP Concepts : Illustration of OOP Concepts : Sending Flowers to a FriendSending Flowers to a Friend
Illustration of OOP Concepts : Illustration of OOP Concepts : Sending Flowers to a FriendSending Flowers to a Friend
Suppose I wish to send flowers to a Suppose I wish to send flowers to a friend, Sally, who lives in a city many friend, Sally, who lives in a city many miles away.miles away.
What should I do? What should I do?
44
Agents and CommunitiesAgents and CommunitiesAgents and CommunitiesAgents and Communities
Solution: Find an appropriate Solution: Find an appropriate agentagent, , namely Flora, and pass to her a namely Flora, and pass to her a messagemessage containing my request containing my request It is the It is the responsibilityresponsibility of Flora to of Flora to
satisfy my requestsatisfy my request There is some There is some methodmethod – some – some
algorithm or some set of operations – algorithm or some set of operations – used to satisfy my requestused to satisfy my request
This information, i.e., details, is usually This information, i.e., details, is usually hiddenhidden from my inspection.from my inspection.
55
The community of agents The community of agents helping mehelping me
The community of agents The community of agents helping mehelping me
Sally’s Florist
Flora
Me
Delivery Person
Sally
Flower Arranger
Wholesaler
Grower
Gardener
66
An ObservationAn ObservationAn ObservationAn Observation
An object-oriented program is An object-oriented program is structured as a community of structured as a community of interacting agents, called interacting agents, called objectsobjects. . Each object has a Each object has a rolerole to play. to play. Each object provides a Each object provides a serviceservice, or , or
performs an performs an actionaction, that is used by , that is used by other members of the community.other members of the community.
77
Messages and MethodsMessages and MethodsMessages and MethodsMessages and Methods
Action is initiated in OOP by the transmission Action is initiated in OOP by the transmission of a of a messagemessage to an agent (an to an agent (an objectobject) ) responsible for the action. responsible for the action. The message encodes the request for an The message encodes the request for an
action and is accompanied by any additional action and is accompanied by any additional information (arguments) needed to carry information (arguments) needed to carry out the request. out the request.
The The receiverreceiver is the object to whom the is the object to whom the message is sent. If the receiver accepts the message is sent. If the receiver accepts the message, it accepts the responsibility to carry message, it accepts the responsibility to carry out the indicated action. out the indicated action. In response to a message, the receiver will In response to a message, the receiver will
perform some perform some methodmethod to satisfy the to satisfy the request.request.
88
Information Hiding PrincipleInformation Hiding PrincipleInformation Hiding PrincipleInformation Hiding Principle
The client sending the request need not The client sending the request need not know the actual means by which the know the actual means by which the request will be honored.request will be honored.
An important part of the OOP is the An important part of the OOP is the development of reusable componentsdevelopment of reusable components ““thrustingthrusting” others” others
99
Message Passing vs.Message Passing vs.Procedure CallingProcedure Calling
Message Passing vs.Message Passing vs.Procedure CallingProcedure Calling
PC: there is no designated receiverPC: there is no designated receiver MP: Interpretation of the message is MP: Interpretation of the message is
dependent on the receiver and can vary with dependent on the receiver and can vary with different receiversdifferent receivers
Usually, the specific receiver for any given Usually, the specific receiver for any given message will not be known until run time, so message will not be known until run time, so the determination of which method to invoke the determination of which method to invoke cannot be made until then.cannot be made until then. late bindinglate binding – between a message and the – between a message and the
code fragment (method) used to respond to code fragment (method) used to respond to the message.the message.
vs. vs. compile-timecompile-time or or link-time bindinglink-time binding in PC. in PC.
1010
ResponsibilitiesResponsibilitiesResponsibilitiesResponsibilities
Describe behavior in terms of Describe behavior in terms of responsibilitiesresponsibilities increases level of abstractionincreases level of abstraction greater independence between greater independence between
objects, a critical factor in solving objects, a critical factor in solving complex problemscomplex problems
Protocol:Protocol: The entire collection of The entire collection of responsibilities associated with an responsibilities associated with an object object
1111
““Ask not what you can do to your data Ask not what you can do to your data structures, but what your data structures, but what your data structures can do for you”structures can do for you”
1212
Classes and InstancesClasses and InstancesClasses and InstancesClasses and Instances
We can use the term Florist to represent We can use the term Florist to represent the category (or class) of all florists.the category (or class) of all florists. I am able to make certain I am able to make certain
assumptions because I have assumptions because I have information about florists in general, information about florists in general, and I expect that Flora, being an and I expect that Flora, being an instance of this category, will fit the instance of this category, will fit the general pattern.general pattern.
1313
All objects are All objects are instancesinstances of a of a classclass The method invoked by an object in The method invoked by an object in
response to a message is determined by response to a message is determined by the class of the receiverthe class of the receiver
All objects of a given class use the same All objects of a given class use the same method in response to similar method in response to similar messages.messages.
1414
Class Hierarchies - Class Hierarchies - InheritanceInheritance
Class Hierarchies - Class Hierarchies - InheritanceInheritance
I have more information about Flora – I have more information about Flora – not necessarily because she is a florist not necessarily because she is a florist but because she is a shopkeeper.but because she is a shopkeeper.
One way to think about how I have One way to think about how I have organized my knowledge of Flora is in organized my knowledge of Flora is in terms of a hierarchy of categories:terms of a hierarchy of categories:
1515
The categories surrounding The categories surrounding FloraFlora
The categories surrounding The categories surrounding FloraFlora
Flora
Material Object
Animal
Mammal
Human
Shopkeeper
Florist
1616
A class hierarchy of various A class hierarchy of various material objectsmaterial objects
A class hierarchy of various A class hierarchy of various material objectsmaterial objects
Material Object
Animal
Mammal
Human
Shopkeeper
Florist
Flora
Artist Dentist
Painter
Liz Adam
Dog
Flash
Platypus
Phyl
Plant
Flower
Carnation
Sally’s Flowers
More abstract classes are listed near the top of the treeMore specific classes and individuals are listed near the bottom.
1717
The principle that knowledge of a more The principle that knowledge of a more general category is also applicable to a more general category is also applicable to a more specific category is called specific category is called inheritanceinheritance. . The class The class FloristFlorist will inherit attributes of the will inherit attributes of the
class (or category) class (or category) ShopkeeperShopkeeper.. Classes can be organized into a hierarchical Classes can be organized into a hierarchical
inheritance structure.inheritance structure. A A child classchild class (or (or subclasssubclass) will inherit ) will inherit
attributes from a attributes from a parent classparent class higher in the higher in the hierarchy. An hierarchy. An abstract parent classabstract parent class is a is a class for which there are no direct class for which there are no direct instances; it is used only to create instances; it is used only to create subclasses.subclasses.
1818
Method Binding, Overriding, Method Binding, Overriding, and Exceptionsand Exceptions
Method Binding, Overriding, Method Binding, Overriding, and Exceptionsand Exceptions
ExceptionsExceptions to a general rule: Consider to a general rule: Consider PhylPhyl the the PlatypusPlatypus Phyl gives birth to live offspring yet Phyl lays Phyl gives birth to live offspring yet Phyl lays
eggs.eggs.
The information contained in a subclass can The information contained in a subclass can override information inherited from a parent override information inherited from a parent classclass Most often, implementations of this approach Most often, implementations of this approach
take the form of a method in the subclass take the form of a method in the subclass having the same name as the method in the having the same name as the method in the parent class,parent class,
combined with a rule stating how to conduct combined with a rule stating how to conduct the search for a method to match a specific the search for a method to match a specific message.message.
1919
Searching for the Searching for the appropriate responseappropriate response
Searching for the Searching for the appropriate responseappropriate response
The search for a method to invoke in response The search for a method to invoke in response to a given message begins with the class of the to a given message begins with the class of the receiver. receiver.
If no appropriate method is found, the search If no appropriate method is found, the search conducted in the parent class of this class.conducted in the parent class of this class.
The search continues up the parent class chain The search continues up the parent class chain until either a method is found or the parent until either a method is found or the parent class chain is exhausted.class chain is exhausted. In the former case, the method is executed; In the former case, the method is executed;
in the latter case, an error message is issued. in the latter case, an error message is issued. If methods with the same name can be found If methods with the same name can be found
higher in the class hierarchy, the method higher in the class hierarchy, the method executed is said to override the inherited executed is said to override the inherited behavior.behavior.
2020
That my wife, Liz and my florist Flora That my wife, Liz and my florist Flora will respond to my message by different will respond to my message by different methods is an example of one form of methods is an example of one form of PolymorphismPolymorphism. .
That I do not, need not, know exactly That I do not, need not, know exactly what method Flora will use to honor my what method Flora will use to honor my message is an example of message is an example of information information hidinghiding..
2121
Kay's Description of Object-Kay's Description of Object-Oriented ProgrammingOriented Programming
Kay's Description of Object-Kay's Description of Object-Oriented ProgrammingOriented Programming
Object-oriented programming is based on the Object-oriented programming is based on the principle of principle of recursive designrecursive design. . 1.Everything is an 1.Everything is an objectobject 2.Objects perform computation by making 2.Objects perform computation by making
requests of each other through the passing of requests of each other through the passing of messages messages
3.Every object has it's own memory, which 3.Every object has it's own memory, which consists of other objects. consists of other objects.
4.Every object is an instance of a class. A class 4.Every object is an instance of a class. A class groups similar objects. groups similar objects.
5.The class is the repository for behavior 5.The class is the repository for behavior associated with an object associated with an object
6.Classes are organized into singly-rooted tree 6.Classes are organized into singly-rooted tree structure, called an inheritance hierarchy. structure, called an inheritance hierarchy.
2222
Recursive DesignRecursive DesignRecursive DesignRecursive Design
2323
Computation as SimulationComputation as SimulationComputation as SimulationComputation as Simulation
Traditional model describing the Traditional model describing the behavior of a computer executing a behavior of a computer executing a program is a program is a process-stateprocess-state or or pigeonholepigeonhole model. model. The computer is a data manager, The computer is a data manager,
following some pattern of following some pattern of instructions, wandering through instructions, wandering through memory, and publishing the results memory, and publishing the results back into other slots. back into other slots.
By examining the values in the slots, we can By examining the values in the slots, we can determine the state of the machine or the results determine the state of the machine or the results produced by a computation. produced by a computation.
2424
Imperative ProgrammingImperative ProgrammingImperative ProgrammingImperative Programming
Imperative programming is the Imperative programming is the ``traditional'' model of computation. ``traditional'' model of computation. State State Variables Variables Assignment Assignment Loops Loops
A processing unit is separate from A processing unit is separate from memory, and ``acts'' upon memory. memory, and ``acts'' upon memory.
2525
Visualization of Imperative Visualization of Imperative ProgrammingProgramming
Visualization of Imperative Visualization of Imperative ProgrammingProgramming
Sometimes called the ``pigeon-hole'' model of computation.
2626
In contrast, in OO framework we never In contrast, in OO framework we never mention memory addresses, variables, mention memory addresses, variables, assignments, or any of the conventional assignments, or any of the conventional programming terms. programming terms. We speak of objects, messages, We speak of objects, messages,
responsibility for some action.responsibility for some action.
Instead of a bit-grinding processor . . . Instead of a bit-grinding processor . . . plungering data structures, we have a universe plungering data structures, we have a universe of well-behaved objects that courteously ask of well-behaved objects that courteously ask each other to carry out their various desires. each other to carry out their various desires.
2727
Similar to discrete event simulationSimilar to discrete event simulation user creates computer models of the user creates computer models of the
various elements of the simulationvarious elements of the simulation describes how they will interact with describes how they will interact with
one anotherone another sets them movingsets them moving
In OOP, computation is a simulation.In OOP, computation is a simulation.
2828
The Power of MetaphorThe Power of MetaphorThe Power of MetaphorThe Power of Metaphor
““Unlike the usual programming method – Unlike the usual programming method – writing software one line at a time – NeXT’s writing software one line at a time – NeXT’s “object-oriented” system offers larger building “object-oriented” system offers larger building blocks that developers can quickly assemble blocks that developers can quickly assemble the way a kid builds faces on Mr. Potato Head.”the way a kid builds faces on Mr. Potato Head.”
Easier to teach OOP concepts to computer Easier to teach OOP concepts to computer novices than to computer professionals.novices than to computer professionals.
Programming is as like the task of “training” Programming is as like the task of “training” the universe of agents to interact smoothly the universe of agents to interact smoothly with each other, each providing a certain small with each other, each providing a certain small and well-defined service to the others, each and well-defined service to the others, each contributing to the effective execution of the contributing to the effective execution of the whole. whole.