Download Understanding Objects and Classes in Object-Oriented Programming (OOP) by Dr. Jamil Ahmed and more Slides Object Oriented Programming in PDF only on Docsity! Dr. Jamil Ahmed OBJECT ORIENTATION Lecture 5 2 Lecture Overview n What is an object? n How do objects communicate? n How is an object’s interface defined n What are classes? n What is the purpose of classes? 3 An object can be: n a thing in the 'real world’ n a model of reality n a tangible or visible thing n a thing to which action or thought can be directed (intangible) n passive - doing nothing until activated e.g. like a switch n active - continually monitoring until conditions change e.g. like a thermostat docsity.com Dr. Jamil Ahmed 4 World of Objects n An Object is a thing one can interact with. n Interaction with an object can be done by sending it messages. n Object will react on receiving message(s). 5 Behavior, State and Identity n The behavior of the object depends upon the current internal state of the object. n The internal state may change in reaction to receiving the message. n In order to send messages, an object is usually addressed by its name which is the identity of the object. 6 Object Defined n Object is a thing that has behavior, state and identity docsity.com Dr. Jamil Ahmed 13 Messages n Objects accept messages advertised in their interfaces n Objects send messages to other objects 14 Messages (2) n Messages may contain arguments (parameters) to clarify the request e.g. n myCat catch: aMouse n myCat moveTo: saucerOfMilk at: highSpeed n myCustomer.changePhoneNo (01705844032) n myCustomer.changeAddress (“PO Nilore”, “Islamabad”, “Pakistan”, “45650) 15 Messages (3) n When a message is sent to an object, the sender will not know what code will be executed by the receiver because the information is encapsulated. n That’s why we say that sender is making “assumptions”. n Encapsulation allows the objects to offer services independent of implementation. docsity.com Dr. Jamil Ahmed 16 Interfaces n Interface defines the collection of services that the object offers n Interface records selectors, arguments and return type 17 Public and Private Interfaces n Public interfaces n messages an object accepts regardless of where they come from n attributes in public interface (allowing inspection and alteration) n Private interfaces n An object is capable of understanding messages that are not in the public interface n An object can always send any messages to itself which it is capable of understanding 18 Classes n A class describes a set of objects with an equivalent role or roles in a system n Every object belongs to a class and the class of object determines its attributes and behavior (hence, interface) n Therefore, all objects of the same class have the same interface. docsity.com Dr. Jamil Ahmed 19 Classes (Instantiating) n Process of creating a new object of a class is called Instantiating the class, and the resulting object an instance of the class. n That’s why the attributes (variables) belonging to an object are called instance variables. n The values of attributes are set during the object creation process 20 The Object Factory n As class has a direct role in the creation of a new object n Object Template + Creation Process n Therefore a class is often seen as The Object Factory 21 Classes as Objects n Most languages allow classes to act like objects – having attributes, behavior and identity n Example: n Customer class may have an attribute numberOfInstances, which is incremented whenever a new customer is created n Customer class may send itself a message (to) getNumberOfCustomers() docsity.com