unit-i - Object Oriented Programming (OOP) ( UnitI )...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Object Oriented Programming (OOP) ( UnitI ) ObjectOriented thinking OOP Paradigm : Objectoriented programming is frequently referred to as a new programming paradigm. The word "paradigm" means an example, or a model. A model helps you understand how the world works. For example, the Newtonian model of physics explains why apples fall to the ground. In computer science , a paradigm explains how the elements that go into making a computer program are organized and how they interact with one another. A Way of Viewing the World " I want to send some flowers to my friend sally who lives in a city many miles away. Because of the distance, I cannot pick up the flowers and carry them to sally's door in person. I go down to Flora, my local florist, tell her the type and quantity of flowers I want together with the address need to be delivered. I can be assured that the flowers will be delivered automatically. " Contd.. Gardeners Sally Grower Me Delivery person Flora Sally's Florist Flower Arranger Wholesaler The community agents helping me Agents and communities The General Principle I find an appropriate agent (Flora) and pass to her a message containing a request. It is the responsibility of Flora to satisfy my request. There is some method a set of operations or algorithm used by Flora to do this. I do not need to know the particular method that Flora will use, this information is hidden from me. 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 Action is initiated in object oriented programming by the transmission of a message to an agent (an object). The message encodes the request for action and is accompanied by additional information (arguments) needed to carry out the request. The receiver is the agent to which 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. In what sense is a message different from a procedure call? In both cases there is a set of welldefined steps that will be initiated following the request. But, there are two important distinctions. The first is that a message has a designated receiver. In a procedure call, there is no designated receiver. The second is that the interpretation of the message ( that is, the method used to respond to the message ) is dependent on the receiver and can vary with different receivers. Contd.. The specific receiver for any given message will not be known until run time. There is a late biding between the message and code fragment( method ) used to respond to the message. In a function or procedure call, binding name to code fragment will be done at compile time only. There is an early binding. Responsibilities Another fundamental concept in objectorienting programming is to describe behavior in terms of responsibilities. By discussing problem in terms of responsibilities, we increases the level of abstraction. This permits greater independence between objects. The entire collection of responsibilities associated with an object is called as a protocol. Classes and Instances Although I do not know Flora very I have a rough idea of the transactions that occur inside Flora's shop. I am able to make certain assumptions based on previous experiences with florists. I expect that Flora, being an instance of the category florist, will match the pattern. All objects are instances of a class. The method invoked by an object to respond to a message is determined by the class of the receiver. All objects of the same class use the same method in response to the same message. Class HierarchiesInheritance The principle that knowledge of more general category is also applicable to a more specific category is called inheritance. 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 class is a class for which there are no direct instances; it is used only to create subclass. Fig: Categories surrounding Flora Contd.. Material Object Flora is a florist, but Florist is a specialized form of the category Shopkeeper. Animal Mammal Human Shopkeeper Florist Flora Material Objects Animal Plant Mammal Flower Platypus Carnation Dog Human Shopkeeper Artist Dentist Florist Potter Flash Flora Elizabeth Kenneth Phyl Sally's flowers Fig: A class hierarchy for various material objects Method Binding, Overriding and Exceptions platypus is a mammal. Mammals give life birth to live child but platypus does not, she lays eggs. This is an exception to the general rule. Information contained in a subclass can override information inherited from a parent class. When methods with the same name are available higher in the class hierarchy the method that executes is said to override the inherited behavior The search for a method to invoke in response to a given message starts with the class of the receiver. If no appropriate method is found, the search continues up the parent class chain until either a method is found or the until the parent class chain is exhausted. Summary of oop concepts Everything is an object. Computation is performed by objects communicating with each other. Objects communicate by sending and receiving messages. A message is a request for action bundled with whatever arguments may be necessary to complete the task. Each object has its own memory. Every object is an instance of a class. A class represents a group of similar objects. The class is the repository for behavior associated with an object. That is, all objects that are instances of the same class can perform the same actions. Classes are organized into a single rooted tree structure, called the inheritance hierarchy. Memory and behavior associated with instances of class are automatically available to any class associated with a descendant in that tree structure. coping with complexity Complexity of programming projects is nonlinear Interconnectedness, the dependence of one Two programmers cannot do in one month what one programmer can do in 2 months Adding more men/women to a project often lengthens, not shortens, the schedule. portion of code on another portion, is responsible for this phenomena. Abstraction mechanisms try to cope with this and object oriented techniques offer yet another step forward. Abstraction Abstraction mechanisms are techniques to deal with creating, understanding and managing complex systems Abstraction is the purposeful suppression or hiding of some details of a process in order to bring out more clearly other aspects, details or structures. Through abstraction one builds a model of the actual system. Abstraction layers in ObjectOriented Software Design At the highest level a program is viewed as a community of cooperating objects. At this highest level the important features to stress are the services provided by each object and the lines of communication between them. The next level of abstraction allows a group of objects working together to be grouped in a unit . Ex:packages Contd.. The next two levels of abstraction deal with the interaction between 2 individual objects where one (the client) uses services from the other (the server) The server advertises the services it can provide for the client as an interface, the client uses these services. The server provides a concrete implementation for its interface. The last level of abstraction considers a single task in isolation I.e. which steps implement a single method. Abstraction mechanisms in programming languages. Procedures and Functions (function centered view) Modules and Packages (data centered view) no information hiding for the detail of the data. no encapsulation. + information hiding + encapsulation Abstract Data Types + separates interface and implementation Contd.. Classes are as Abstract Data Types in a servicecentered view. Message Passing and Method Binding bring polymorphism leading to more readable code. Class Hierarchies and Inheritance results in increased functionality and reduction of code size. Programming in the Small versus Programming in the Large Code developed by single programmer or very small set of programmers. Every individual understands all aspects of the project. Major effort is the design and development of algorithms dealing with the problem at hand. Software system developed by a large team. No single individual is responsible for the entire project or understands all aspects of it Major effort is the management of details and the communication of information between different portions of the project. ...
View Full Document

Ask a homework question - tutors are online