Object Oriented Programming (CS304) Lecture Notes PDF
Document Details
Uploaded by FastGrowingRing5358
Virtual University of Pakistan
2009
Tags
Summary
These are lecture notes from a course on Object-Oriented Programming (OOP). The notes cover topics such as information hiding, encapsulation, interface, and implementation. The notes also include examples and real-life scenarios to illustrate these concepts. The notes are relevant to undergraduate-level computer science students.
Full Transcript
Object Oriented Programming (CS304) VU Lecture No.02 Lecture Contents 1. Information Hiding 2. Encapsulation 3. Interface 4. Implementation 5. Separation of Interface & Implementation 6. Messages 02.1. Information Hiding:...
Object Oriented Programming (CS304) VU Lecture No.02 Lecture Contents 1. Information Hiding 2. Encapsulation 3. Interface 4. Implementation 5. Separation of Interface & Implementation 6. Messages 02.1. Information Hiding: Information hiding is one of the most important principles of OOP inspired from real life which says that all information should not be accessible to all persons. Private information should only be accessible to its owner. By Information Hiding we mean “Showing only those details to the outside world which are necessary for the outside world and hiding all other details from the outside world.” Real Life Examples of Information Hiding 1. Ali’s name and other personal information is stored in his brain we can’t access this information directly. For getting this information we need to ask Ali about it and it will be up to Ali how much details he would like to share with us. 2. An email server may have account information of millions of people but it will share only our account information with us if we request it to send anyone else accounts information our request will be refused. 3. A phone SIM card may store several phone numbers but we can’t read the numbers directly from the SIM card rather phone-set reads this information for us and if the owner of this phone has not allowed others to see the numbers saved in this phone we will not be able to see those phone numbers using phone. In object oriented programming approach we have objects with their attributes and behaviors that are hidden from other classes, so we can say that object oriented programming follows the principle of information hiding. In the perspective of Object Oriented Programming Information Hiding is, “Hiding the object details (state and behavior) from the users” Here by users we mean “an object” of another class that is calling functions of this class using the reference of this class object or it may be some other program in which we are using this class. Information Hiding is achieved in Object Oriented Programming using the following principles, © Virtual University of Pakistan 15 Object Oriented Programming (CS304) VU All information related to an object is stored within the object It is hidden from the outside world It can only be manipulated by the object itself Advantages of Information Hiding Following are two major advantages of information hiding, It simplifies our Object Oriented Model: As we saw earlier that our object oriented model only had objects and their interactions hiding implementation details so it makes it easier for everyone to understand our object oriented model. It is a barrier against change propagation As implementation of functions is limited to our class and we have only given the name of functions to user along with description of parameters so if we change implementation of function it doesn’t affect the object oriented model. We can achieve information hiding using Encapsulation and Abstraction, so we see these two concepts in detail now, 02.2. Encapsulation Encapsulation means “we have enclosed all the characteristics of an object in the object itself” Encapsulation and information hiding are much related concepts (information hiding is achieved using Encapsulation) We have seen in previous lecture that object characteristics include data members and behavior of the object in the form of functions. So we can say that Data and Behavior are tightly coupled inside an object and both the information structure and implementation details of its operations are hidden from the outer world. Examples of Encapsulation Consider the same example of object Ali of previous lecture we described it as follows, 16 © Virtual University of Pakistan Object Oriented Programming (CS304) VU Ali Characteristics (attributes) Name Age Behavior (operations) Walks Eats You can see that Ali stores his personal information in itself and its behavior is also implemented in it. Now it is up to object Ali whether he wants to share that information with outside world or not. Same thing stands for its behavior if some other object in real life wants to use his behavior of walking it can not use it without the permission of Ali. So we say that attributes and behavior of Ali are encapsulated in it. Any other object don’t know about these things unless Ali share this information with that object through an interface, Same concept also applies to phone which has some data and behavior of showing that data to user we can only access the information stored in the phone if phone interface allow us to do so. Advantages of Encapsulation The following are the main advantages of Encapsulation, a. Simplicity and clarity As all data and functions are stored in the objects so there is no data or function around in program that is not part of any object and is this way it becomes very easy to understand the purpose of each data member and function in an object. b. Low complexity As data members and functions are hidden in objects and each object has a specific behavior so there is less complexity in code there will be no such situations that a functions is using some other function and that functions is using some other function. c. Better understanding Everyone will be able to understand whole scenario by simple looking into object diagrams without any issue as each object has specific role and specific relation with other objects. 02.3. Interface © Virtual University of Pakistan 17 Object Oriented Programming (CS304) VU Interface is a set of functions of an object that he wants to expose to other objects. As we discussed previously that data and behavior of each object is hidden in that object it self so we have to use the concept of interface of the object to expose its behavior to outer word objects. Different objects may need different functions of an object so interface of an object may be different for different objects. Interfaces are necessary for object communication. Each object provides interface/s (operations) to other objects through these interfaces other objects communicate with this object. Example – Interface of a Car Steer Wheels Accelerate Change Gear Apply Brakes Turn Lights On/Off Example – Interface of a Phone Input Number Place Call Disconnect Call Add number to address book Remove number Update number 02.4. Implementation It is actual implementation of the behavior of the object in any Object Oriented language. It has two parts, Internal data structures to hold an object state that will be hidden from us it will store values for an object data members. Functionality in the form of member functions to provide required behavior. Examples of Implementation a. Gear Box in car system Consider object Gear Box in car system it has a certain structure and functionality. When this object will be implemented it will have two things, Physical structure of the gear box Functionality implemented in this structure to change gear. Both these things are part of implementation. 18 © Virtual University of Pakistan Object Oriented Programming (CS304) VU So it has, Data Structure in the form of Mechanical structure of gear box Functionality mechanism to change gear b. Address Book in a Phone Similarly take the example of contact details saved in the SIM of a phone, In that case we can say physical structure of SIM card as Data Structure And Read/write operations provided by the phone as Functionality. 02.5. Separation of Interface & Implementation As discussed earlier we only show interface of an object to outside world and hide actual implementation from outside world. The benefit of using this approach is that our object interface to outside word becomes independent from inside implementation of that interface. This is achieved through the concepts of encapsulation and information hiding. Real Life example of separation of interface and implementations Driver has a standard interface to drive a car and using that interface he drive can drive any car regardless of its model or type whatever engine type it has or whatever type of fuel it is using. 02.6. Messages Objects communicate through messages they send messages (stimuli) by invoking appropriate operations on the target object. The number and kind of messages that can be sent to an object depends upon its interface Examples – Messages A Person sends message (stimulus) “stop” to a Car by applying brakes A Person sends message “place call” to a Phone by pressing appropriate button 02.7. Summary Information hiding is achieved through encapsulation. Encapsulation and Information Hiding are related to each other. Interface of an object provides us the list of available functions. An object may have more than one interface. Interface and implementation are separated from each other to achieve Information Hiding. Objects communicate with each other using messages. © Virtual University of Pakistan 19 Object Oriented Programming (CS304) VU Useful Links: http://www.alice.org/ A Graphical Programming Environment to teach Computer Programming. 20 © Virtual University of Pakistan