• Like
  • Save
Oop concept
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
502
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Principles of Object-Oriented Programming Why OOP is popular? Language and thoughts A new way of viewing the world Elements of OOP Coping with complexity
  • 2. Difficult Questions• What is Object-Oriented Programming?• Why is it so popular?– OOP is a revolutionary idea, totally unlike anything that has come before in programming languages– OOP is an evolutionary step, following naturally on the heels of earlier programming abstractions
  • 3. Why is OOP Popular?A few possible reasons why OOP is popular: • Hope that it will quickly and easily lead to increased productivity and increased reliability (solve the software crises) • Similarity to techniques of thinking about problems in other domains • Hope for easy transition from existing languages (e.g., C or Pascal)
  • 4. A New Paradigm We start by considering the definition of the term ``paradigm: Par a digm n. 1. A list of all the inflectional forms of a word taken as illustrative example of the conjugation or declension to which it belongs. An example or model. [Late Latein paradigma, from Greek paradeigma, modern paradeiknunai, to compare, exhibit.]OO programming is a new paradigm • New way of thinking about what it means to compute, and about what we can structure information inside a computerLet’s first see the relationship between languages and thoughts
  • 5. Language and Thoughts In linguistics there is a hypothesis that the languages we speak directly influence the way in which we view the world (called Sapir-Whorf Hypothesis) Controversial example of languages influencing thoughts – Eskimo (or Innuit) languages have many words to describe snow – Arabic languages and camels Should not over-generalize the conclusion – With time and training, we can also distinguish snow; however, our language does not force me into doing so, so it is not natural to me – Different languages can lead one (but does not require one) to view the world in a different fashion This is also applicable to computer programming languages – Using OO language can naturally lead one to view the world in OO fashion, but does not force one to do
  • 6. An Example from Computer Languages The programming language chosen to solve a problem will decide fundamentally the way in which an algorithm is developed – A student working in DNA research had the task of finding repeated sequences of M values in a long sequence of values: ACTCGGATCTTGCATTTCGGCAATTGGACCCTGACTTGGCCA ... Wrote the simplest (and therefore, most efficient?) Fortran program : DO 10 I = 1, N-M DO 10 J = I+1, N-M FOUND = .TRUE. DO 20 K = 1, M 20 IF X[I+K-1] .NE. X[J+K-1] THEN FOUND = .FALSE. IF FOUND THEN ... 10 CONTINUE Took a depressingly long time.
  • 7. A Better SolutionA friend writing in APL found a much better solution by rearranging thedata and sorting. A C T C G G positions 1 to M C T C G G A positions 2 to M+1 T C G G A T positions 3 to M+2 C G G A T T positions 4 to M+3 G G A T T C positions 5 to M+4 G A T T C T positions 6 to M+5 ... TGGACCGGACCC ...Ran surprisingly fast
  • 8. What lead to the discovery? Why did the APL programmer find the better solution? • Fortran programmer was blinded by a culture that valued loops, simple programs • Sorting is a built-in operation in APL, so good APL programmers try to find solutions based on sorting • APL programmers were “naturally” led to better solutionsProgramming language directs the programmer’s mind to view the problem in certain way
  • 9. Churchs Conjecture Language affects the way of thinking is believable, but Sapir-Whorf hypothesis is controversial – It claims that it is possible for one working in one language to imagine thoughts that cannot be translated or understood in different languages In computer science, we have a directly opposite assertion: Churchs Conjecture: Any computation for which there exists an effective procedure can be realized by a Turing machine. Any programming language can simulate Turing machine – All languages are equivalent, so anything can be done in any language – But it may simply be easier or more efficient to use one language or another Power of OO programming languages should be understood in a similar way; they make it easy to maintain large S/W projects
  • 10. Imperative Programming ParadigmImperative programming is the ``traditional model of computation. • State • Variables • Assignment • LoopsA processing unit is separate from memory, and ``acts upon memory – Computer is the data manager – wandering through memory, pulling values in memory slots – transforming them in some manner, and pushing results back in some other slots – Although this is exactly what happens inside a computer, it is not the way people do for solving problems
  • 11. Visualization of Imperative ProgrammingSometimes called the ``pigeon-hole model of computation.
  • 12. Recursive DesignThe structure of the part mirrors the structure of the larger unit.
  • 13. Alan Kays Description of OO Programming OO 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 (bundled with arguments) 3. Every object has its 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 behaviors associated with an object 6. Classes are organized into singly-rooted tree structure, called an inheritance hierarchy. We can illustrate these principles by considering how I go about solving a problem in real life.
  • 14. Illustration of OOP Concepts - Sending Flowers to a FriendTo illustrate the concepts of OOP in an easily understood framework, considerthe problem of sending flowers to a friend who lives in a different city.I cant deliver them myself. So I use my local Florist.I tell my Florist (named Flo) the address for my friend, how much I want to spend,and the type of flowers I wish to send.Flo contacts a florist in my friends city, who arranges the flowers, then contacts adriver, who delivers the flowers.If we start to think about it, there may even be other people involved in thistransaction. There is the flower grower, perhaps somebody in charge ofarrangments, and so on.And so we see, that to solve my problem requires the interaction of an entirecommunity of individuals.
  • 15. Elements of OOP – Objects1. Everything is an object.Actions in OOP are performed by agents, called instances or objects.There are many agents working together in my scenario. We havemyself, my friend, the florist, the florist in my friends city, the driver,the flower arranger, and the grower. Each agent has a part to play,and the result is produced when all work together in the solution of aproblem.
  • 16. Elements of OOP – Messages2. Objects perform computation by making requests of each other through the passing of messages Actions in OOP are produced in response to requests for actions, called messges. An instance may accept a message, and in return will perform an action and return a value. To begin the process of sending the flowers, I give a message to Flo. She in turn gives a message to the florist in my friends city, who gives another message to the driver, and so on.
  • 17. Information HidingNotice that I, as a user of a service being provided by an object, needonly know the name of the messages that the object will accept.I need not have any idea how the actions performed in response to myrequest will be carried out.Having accepted a message, an object is responsible for carrying it out.
  • 18. Elements of OOP – ReceiversMessages differ from traditional function calls in two very importantrespects: • In a message there is a designated receiver that accepts the message (in function calls, there is no receiver • The interpretation of the message may be different, depending upon the receiver
  • 19. Different Actionsvar Flo : Florist; Beth : Wife; Ken : Dentist;begin Flo.sendFlowersTo(myFriend); { will work } Beth.sendFlowersTo(myFriend); { will also work but in different way} Ken.sendFlowersTo(myFriend); { will probably not work }end;
  • 20. Behavior and InterpretationAlthough different objects may accept the same message, the actions(behavior) the object will perform will likely be different.Usually, the specific receiver for any given message will not be knownuntil run-time, so the determination of what behavior (code) to performmay be made at run-time, a form of late binding. • Different from early binding of name to code in function callsThe fact that the same name can mean two entirely different operationsis one form of polymorphism.
  • 21. Responsibility and Non-interference• Behavior is described in terms of responsibilities • My message indicates only the desired outcome • I do not want to interfere with how the florist achieves the outcome • Responsibility is closely related to non-interference• Implies greater independence between agents, which is a critical factor in solving complex systems • OOP is responsibility-driven software design process• The difference between traditional and OO perspectives can be summarized by the following:``Ask not what you can do to your data structures, but ask what your data structures can do for you
  • 22. Elements of OOP - Recursive Design3. Every object has its own memory, which consists of other objects. Each object is like a miniature computer itself - a specialized processor performing a specific task.
  • 23. Elements of OOP – Classes4. 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. The behavior I expect from Flo is determined from a general idea I have of the behavior of Florists. We say Flo is an instance of the class Florist. Behavior is associated with classes, not with individual instances. All objects that are instances of a class use the same method in response to similar messages.
  • 24. Hierarchies of CategoriesBut there is more that I know about Flo then just that she is a Florist. Iknow she is a ShopKeeper, and a Human, and a Mammal, and aMaterial Objects, and so on.At each level of abstraction I have certain information recorded. Thatinformation is applicable to all lower (more specialized) levels.
  • 25. Class Hierarchies
  • 26. Elements of OOP – Inheritance6. Classes are organized into a singly-rooted tree structure, called an inheritance hierarchy Information (data and/or behavior) I associate with one level of abstraction in a class hierarchy is automatically applicable to lower levels of the hierarchy.
  • 27. Elements of OOP – OverridingSubclasses can alter or override information inherited from parentclasses for exceptional cases • All mammals give birth to live young • Some mammals lays eggs
  • 28. Summary: OOP View of ComputingThe OOP view of computation is similar to creating a universe ofinteracting computing objectsBecause the OOP view is similar to the way in which people go aboutsolving problems in real life (finding another agent to do the real work!),intuition, ideas, and understanding from everyday experience can bebrought to bear on computing.On the other hand, common sense was seldom useful when computerswere viewed in the process-state model, since few people solve theireveryday problems using pigeon-holes.
  • 29. Coping with ComplexityAnother way to understand OOP is to place it in a historical context.Non-linear behavior of complexity – As programming projects become large, they tend to require a team of programmers – Interesting phenomenon: a task that takes 2 months with 1 programmer requires more than 1 month even with 2 programmers``software crises came out when people realized the major problem in software development were not algorithmic, but were caused by communication difficulties and the management of complexity.
  • 30. Interconnections - the Bane of ComplexityMany software systems are complex not because they are large, butbecause they have many interconnections.Interconnections make it difficult to understand pieces in isolation, or tocarry them from one project to the next.The inability to cleanly separate out components makes it difficult todivide a task between several programmers.Complexity can only be managed by means of abstraction, byeliminating information that a programmer do not need to know.If we examine the history of mechanisms used to solve the problem of complexity we can better appreciate the role of OOP.
  • 31. Assembly LanguagesAssembly languages and linkers were perhaps the first tools used toabstract features of the bare machine. • Addresses could be represented symbolically, not as a number. • Symbolic names for operations. • Linking of names and locations performed automatically
  • 32. Procedures and FunctionsLibraries of procedures and functions (such as mathematical orinput/output libraries) provided the first hints of information hiding.They permit the programmer to think about operations in high levelterms, concentrating on what is being done, not how it is beingperformed.But they are not an entirely effective mechanism of information hiding,and they only partially solved the problem of multiple programmersmaking use of the same namesAn example - A stack
  • 33. The Problem of Stacksint datastack[100];int datatop = 0;void init() // initialize the stack{ datatop = 0; }void push(int val) // push a value on to the stack{ if (datatop < 100) datastack [datatop++] = val; }int top() // get the top of the stack{ if (datatop > 0) return datastack [datatop - 1]; return 0; }int pop() // pop element from the stack{ if (datatop > 0) return datastack [--datatop]; return 0; }
  • 34. Block Scoping Didnt Solve the Problem Problems with previous implementation – we need to use global variables for the array (no access control) – method names cannot be used elsewhere More control over name visibility using block scoping in Algol and Pascal, but any scope that accesses the four methods can access the data as wellbegin var datastack : array [ 1 .. 100 ] of integer; datatop : integer; procedure init; ... procedure push(val : integer); ... function pop : integer; ......end;
  • 35. ModulesModules basically provide collections of procedures and data withimport and export statements [Parnas 1972] – they can distinguish public part that are accessible outside and private part that are accessible only within the module – Enforces need-to-know principleSolves the problem of encapsulation -- but what if your programmingtask requires two or more stacks? – Only one stack can be manipulated with a module – Need to define a different module for another stack- with what names?Modules provide information hiding but do not allow instantiation, whichis the ability to make multiple copies of the data area – need to develop a new concept
  • 36. Abstract Data TypesAn Abstract Data Type is a programmer-defined data type that can bemanipulated in a manner similar to system-provided data types • Have the ability to instantiate many different copies of the data type. • E.g., define stack as data type and instantiate it as many as you want • Data type can be implemented using provided operations, without knowledge of internal representation. • Packages in CLU and Ada can define abstract data types
  • 37. Objects - ADTs with Message PassingCharacteristics of Objects • Encapsulation -- similar to modules • Instantiation -- similar to ADTs • Messages -- dynamic binding of procedure names to behavior • Activity is initiated by a request to a specific object, not by calling a function (do you call push with a stack and a data value, or do you ask a stack to push a data value onto itself?) • Added to message passing, overloading of names and reusing software • Interpretation of message -- it depends on objects receiving it • Push for stack and push for door are different • Inheritance -- Allow different data types to share the same code • Polymorphism -- Allow the shared code to be tailored to fit the specific circumstances
  • 38. Summary• Object-oriented programming is not simply features added to a programming language. Rather, it is a new way of thinking• Object-oriented programming views a program as a community of agents, termed objects. Each object is responsible for a specific task.• An object is an encapsulation of state (data values) and behavior (operations).• The behavior of objects is dictated by the object class.• An object will exhibit its behavior by invoking a method (similar to executing a procedure) in response to a message.• Objects and classes extend the concept of abstract data types by adding the notion of inheritance.