Object Oriented Programming - Lecture 1, Schonweiss

Page 1

Object Oriented Programming Lecture 1 All Rights Reserved, Schonweiss.

D Rajeev, Schonweiss www.schonweiss.com


The Object Oriented Paradigm

 What is a paradigm? Why is this term used to describe the object-oriented approach to problem solving?  How does language influence thought?  What are some of the characteristics of the objectoriented way of thinking?


The Object Oriented Paradigm

 Although many of the concepts were invented in the 1960s, object oriented languages came to the public attention in 1960s.  A radically new idea that swept through the word of computing.  The most popular and driving paradigm of thought in modern day software design, and practical computer science.


Languages and Ideas  Are ideas and languages, one and the same? Or is there a separation? What is your view?  Do languages we speak influence how we express the structure of the world? Or the structure of things?  “The nth President of United States.”  Obviously language influences the ease with which we express certain ideas.  Does it occur in Computer Science? Do the language we use influence the way we come up with solutions?  How does the paradigm of thought – the language we use – influence the way we think?


Languages and Ideas

 Ideas drive language, and language in turn makes certain patterns of thinking easier than other.  The Eskimo language “Inuit”


Eskimo Words for Snow A. Snow particles (1) Snowflake qanuk 'snowflake' qanir- 'to snow' qanunge- 'to snow' [NUN] qanugglir- 'to snow' [NUN] (2) Frost kaneq 'frost' kaner- 'be frosty/frost sth.' (3) Fine snow/rain particles kanevvluk 'fine snow/rain particles kanevcir- to get fine snow/rain particles (4) Drifting particles natquik 'drifting snow/etc' natqu(v)igte- 'for snow/etc. to drift along ground' (5) Clinging particles nevluk 'clinging debris/ nevlugte- 'have clinging debris/...'lint/snow/dirt...' Source: http://www.princeton.edu/~browning/snow.html


Eskimo words for Snow B. Fallen snow (6) Fallen snow on the ground aniu [NS] 'snow on ground' aniu- [NS] 'get snow on ground' apun [NS] 'snow on ground' qanikcaq 'snow on ground' qanikcir- 'get snow on ground' (7) Soft, deep fallen snow on the ground muruaneq 'soft deep snow' (8) Crust on fallen snow qetrar- [NSU] 'for snow to crust' qerretrar- [NSU] 'for snow to crust' (9) Fresh fallen snow on the ground nutaryuk 'fresh snow' [HBC] (10) Fallen snow floating on water qanisqineq 'snow floating on water' C. Snow formations (11) Snow bank qengaruk 'snow bank' [Y, HBC] (12) Snow block utvak 'snow carved in block' (13) Snow cornice navcaq [NSU] 'snow cornice, snow (formation) about to collapse' navcite- 'get caught in an avalanche' D. Meterological events (14) Blizzard, snowstorm pirta 'blizzard, snowstorm' pircir- 'to blizzard' pirtuk 'blizzard, snowstorm' (15) Severe blizzard cellallir-, cellarrlir- 'to snow heavily' pir(e)t(e)pag- 'to blizzard severely' pirrelvag- 'to blizzard severely'


Sapir-Whorf Hypothesis

 In linguistics there is a hypothesis that the language in which an idea or thought is expressed colors or directs in a very emphatic manner that nature of the thought.  As we just saw, the Eskimo (or Innuit) languages and snow  Arabic languages and camels – over 40 words, according to some.  So some human languages, obviously, seem to increase the ease with which we can express certain ideas.


A DNA Researcher’s Story

A student working in DNA research had the task of finding repeated sequences of M values in a long sequence of values.


… The programmer dutifully wrote a two loop solution, in FORTRAN


A DNA Researcher’s Story

 The program took an excruciatingly long while to solve, and she discussed it with another student, who happened to be proficient in APL, an interpreted language much slower compared to Fortran, a complied language, considered among the most “efficient” of those times.


The APL Programmer’s Solution  Arrange all strings in an array.


APL Programmer’s Solution  And Sort!


APL Programmer’s Solution The Solutions: O(M*N^2) vs. O(M*NLogN)  What drove the APL programmer to the more efficient solution?  How did he naturally think of it?  Not that APL is faster or better than Fortran, but loops are very difficult to write in APL, but sorting is a simple thing to do on an array, with a built in operator that is part of the language.


A Description of APL  It is concise, using symbols rather than words and applying functions to entire arrays without using explicit loops.  It is solution focused, emphasizing the expression of algorithms independently of machine architecture or operating system.  It has just one simple, consistent, and recursive precedence rule: the right argument of a function is the result of the entire expression to its right.  It facilitates problem solving at a high level of abstraction.


Take Away Insight

 The language we use, and the concepts there-in, drives and influences our paradigm of thought.  If a language is closer to a world, like the Eskimo, then its more suited to describe concepts in the Eskimo world.


So How is the World Structured?


The Word Paradigm  1 : EXAMPLE, PATTERN; especially : an outstandingly clear or typical example or archetype  2 : an example of a conjugation or declension showing a word in all its inflectional forms  3 : a philosophical and theoretical framework of a scientific school or discipline within which theories, laws, and generalizations and the experiments performed in support of them are formulated; broadly : a philosophical or theoretical framework of any kind The last definition was popularized by Thomas Kuhn, in his book “Structure of Scientific Revolutions.”


Thinking Object Oriented  Non OO languages are the languages of the computer. [ “Store a variable in a memory location…” Very close to how the memory of the computer is organized, and how things function in it]  The entire space of memory is a unit, and procedures act on it.  No separation exists in terms of units of a machine, fulfilling its own responsibility, maintaining its own state, with no external interference.  The “paradigm” is one of the machine’s structure.


Thinking Object Oriented Paradoxically, the style of problem solving embodied in the object-oriented technique is frequently the method used to address problems in everyday life. Thus, computer novices are often able to grasp the basic ideas of object-oriented programming easily, whereas people who are more computer literate are often blocked by their own preconceptions. Alan Kay, for example, found that it was often easier to teach Smalltalk to children than to computer professionals [Kay 1977].


Discuss Ideas.

So what kind of a paradigm do we need if we are to model the real world?


Section II


OO:A Way of Viewing the World  Lets first explore some key aspects of how things run in the real world.  Then let us see if we can get things to be the same way in our programming languages.  i.e. Model the techniques involved in the way we easily and naturally think about solving problems, and the way world works, into the vocabulary of our programming languages. And have a completely new way of thinking about programming – more real world, more humanthinking like, than computer –memory like.


The Problem

Suppose an individual named Chris wishes to send flowers to a friend named Robin, who lives in another city. Because of the distance, Chris cannot simply pick the flowers and take them to Robin in person. Nevertheless, it is a task that is easily solved. Chris simply walks to a nearby flower shop, run by a florist named Fred. Chris will tell Fred the kinds of flowers to send to Robin, and the address to which they should be delivered. Chris can then be assured that the flowers will be delivered expediently and automatically


Community and Agents


How did things happen here?  Each person finds an appropriate agent, sends him a message containing a request.  Chris does not need to know exactly how Fred will accomplish things. He only needs to know that Fred can do it.  A community of objects that fulfill their responsibilities.  Responsibility implies non-interference.


A New Paradigm of Thinking About Programs

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.


Messages and Methods

 All Objects Receive Messages, and Respond to them, stating their change, and/or giving out messages to other objects.  Another term for the word message is a request. You make request, when you know the object you send the request to has a method to handle the request.  “Hey can you look up and tell me what the cosine of o.5 is?” “Hey could you please draw a painting on the this canvas that looks like this?”


Messages, Agents / Receivers, Response


Independent, Re-usable Components

An important part of object-oriented programming is the development of reusable components, and an important first-step in the use of reusable components is a willingness to trust software written by others.


Messages Vs Procedure Calls  What makes a procedure call different from a “Message” [ other term for “Request”]  The First is: The Presence of a Designated Receiver.  The second is that the interpretation of the message (that is, the method used to respond to the message) is determined by the receiver and can vary with different receivers. Chris could give a message to a friend named Elizabeth, for example, and she will understand it and a satisfactory outcome will be produced (that is, flowers will be delivered to their mutual friend Robin). However, the method Elizabeth uses to satisfy the request (in all likelihood, simply passing the request on to Fred) will be different from that used by Fred in response to the same request.  If Chris were to ask Kenneth, a dentist, to send flowers to Robin, Kenneth may not have a method for solving that problem. If he understands the request at all, he will probably issue an appropriate error diagnostic.


Messages Vs Procedure Calls  There, the distinction between message passing and procedure calling is that, in message passing, there is a designated receiver, and the interpretation the selection of a method to execute in response to the message may 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. Thus, we say there is late binding between the message (function or procedure name) and the code fragment (method) used to respond to the message. This situation is in contrast to the very early (compile-time or link-time) binding of name to code fragment in conventional procedure calls.  A real world example of “cricket player” responding to a ball thrown to him.  Of driving a vehicle. Of eating food. The precise behavior is only known at runtime.


Responsibilities  A fundamental concept is to describe behavior in terms of responsibilities.  Chris's request for action indicates only the desired outcome (flowers sent to Robin). Fred is free to pursue any technique that achieves the desired objective, and in doing so will not be hampered by interference from Chris.


Responsibilities  A traditional program often operates by acting on data structures, for example changing fields in an array or record.  In contrast, an object oriented program requests data structures (that is, objects) to perform a service. This difference between viewing software in traditional, structured terms and viewing it from an object-oriented perspective can be summarized by a twist on a wellknown quote: Ask not what you can do to your data structures, but rather ask what your data structures can do for you.


Responsibilities

 In short, we create a universe of things, of objects, that fulfill responsibilities, doing things for each others, and mutual fulfillment of responsibilities in response to requests passed animate the world.


Things Belong to Classes

Although Chris has only dealt with Fred a few times, Chris has a rough idea of the transaction that will occur inside Fred's flower shop. Chris is able to make certain assumptions based on previous experience with other florists, and hence Chris can expect that Fred, being an instance of this category, will fit the general pattern. We can use the term Florist to represent the category (or class) of all florists. Let us incorporate these notions into our next principle of object-oriented programming.


All Things Belong to Class Hierarchies

 All shopkeepers have methods that allow one to transact money for goods with them, and since. Since Fred is Florist, he is a shopkeeper as well, Chris can use that interface to transfer money in return of a receipt.  A Florist is a particular kind of shopkeeper.




Inheritance  The principle that the knowledge and abilities of a more general category is available for those more specific is called inheritance.  In OO parlance, we say Florist will inherit the attributes of the class 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 tree.  An abstract parent class is a class (such as Mammal) for which there are no direct instances; it is used only to create a hierarchy, and inherit behavior into subclasses.


New Unique Behavior  Mammals all give birth to live children. Platypus is a mammal, but lays eggs!  In such cases, where the behavior changes, one must over-ride the behavior .  We do this by decreeing that information contained in a subclass can override information inherited from a parent class.  We can over-ride and redefine a method…


Method Binding and Overriding

 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 is 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.


Computation as Simulation

 The essence of OOPs may be thought of as viewing the programming space as where you simulate a universe.  Instead of manipulating memory locations, and thinking in terms of memory locations, and other systemoriented concepts, we think at a higher level of abstraction.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.