1 / 34

Objects

Objects. Object is a very important concept in object-orientation. When an OO program is executed, it normally creates objects in memory. These objects collaborate with each other to perform certain tasks. This realizes the runtime behaviour of the program. Kad ATMku. $$. $$.

Download Presentation

Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects

  2. Object is a very important concept in object-orientation. • When an OO program is executed, it normally creates objects in memory. • These objects collaborate with each other to perform certain tasks. • This realizes the runtime behaviour of the program.

  3. Kad ATMku $$ $$ Kad ATMku Object Space

  4. Definition of Object • An object is a thing that has state, behaviour and identity. • Examples: • The LCD projector in the lecture hall at FTSM. • Dr Sufian’s Kenari • Dr Sufian’s Waja

  5. Object State • Each object has attributes which collectively represent its state. BEFORE AFTER

  6. Car Car colour colour green blue plateNr plateNr “WHH7801” “WHY9531” owner owner “Sufian Idris” “Ali Bakar” • In Java, the attributes of an object are implemented as variables that belong to that object. These variables are called instance variables. Example: A Car object Another Car object

  7. Fridge owner “Ali Bakar” values can be changed colour biru serialNr “342-0967” value is fixed • The values of the attributes of an object can change. However, there are attributes with fixed values. • The atributes of an object normally do not change.

  8. : Person : Car links : Car • An object may have links to other objects. These links represent relationships between those objects. • Example: The links are added to represent the fact that both cars are owned by the same person.

  9. links : Engine : Chassis : Car • Example: The links are added to represent the fact that the engine and the chassis are part of the car. This relationship is also called aggregation.

  10. : Processor : Computer speed 900 datePurch 15032002 processor memory : Memory object references size 256 • In Java, links to objects are represented as object references. • Instance variables can be defined to store such references.

  11. jump Object Behaviour • Message sending is a mechanism which objects use to interact with other objects. • To request an object to perform a certain task, an appropriate message needs to be sent to that object.

  12. An object only responds to messages that it understands. ?? fly

  13. nextFloat() : Temperature Converter : Scanner • Object behaviour refers to how an object reacts to messages it receives from its clients. The Scanner object responds by inputting a float value and returning that value to the TemperatureConverter object.

  14. 10 20 : Rectangle zoom_2x colour green width 5 height 10 • An object’s response to a message can cause its state to change.

  15. Saluran Ceria Anda • An object may also send messages to other objects in its response to a message. openCommChannel() switchChannel(5) closeCommChannel() switchChannel(5)

  16. Object Identity • Each object has its own identity which differentiates it from other objects. Triplets… but each of them has its own identity

  17. The identity of an object is built-in. There is no need to explicitly define an instance variable to represent the identity of an object. class Computer { private int computerID; private Date datePurchased; private Processor processor; … } Redundant

  18. Relationship between Object State and Object Behaviour • Object behaviour can affect object state. • Example: An ATM machine dispensing money causes the amount of money in the machine to decrease.

  19. Koka Koola • Object state can affect object behaviour. Number of tins: dispense() 20 What if there are no more tins in the machine?

  20. Attributes Behaviour Abstraction • Supposing we need to define an object that represents a cat. What attributes and methods should we define? weight chew length drink run heart size … … …there are too many!

  21. Humans have the ability to isolate those aspects that are important for some purpose and suppress those aspects that are unimportant.

  22. 14 0.5 : Bird colour white weight 5 height 10 • A program designer only needs to define the attributes and behaviour that are essential to the problem in hand. • In object-orientation, software objects are abstractions.

  23. Encapsulation • Objects should not reveal all of its information. There is certain information that should be hidden. • Example A restaurant may not want to reveal the specifics of how the dishes it serves are prepared. On the other hand, it needs to reveal the price of each type of dish.

  24. Encapsulation is the technique for packaging information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

  25. HIDDEN BY OBJECT • Information normally hidden by objects: • object attributes • the specifics of how they react to the messages received by them attributes: width height … behaviour: area() 40 area() return width * height;

  26. Some benefits of encapsulation: • if done properly, modifications to object implementation should not affect clients • objects can control access to their attributes

  27. Object Interface • Not all messages are understood by an object. • To obtain information on the messages which an object understands, we need to refer to its interface.

  28. The interface of an object reveals public information about the object. It should not contain information hidden by the object. • An example of information that could be in an object’s interface is a list of its public methods; only their signature, not their implementation.

  29. Rectangle object INTERFACE area() setWidth(int width) setHeight(int height) ?? yesss! But where can we find information about an object’s interface?

  30. jump Messages and Methods • Message sending is a mechanism used by objects to communicate with other objects. • To get an object to do something, you send an appropriate message to that object.

  31. Saluran Ceria Anda • A message consists of its name (compulsory) and its parameters (if any). • The receiver object is the object receiving the message. The sender of the message is referred to as the client. openCommChannel() closeCommChannel() switchChannel(5) switchChannel(5) receiver object client

  32. An object implements its behaviour with methods. A method contains implementation details of how an object responds to a certain message. message attributes: width height … behaviour: area() 40 area() return width * height; method

  33. Object Collaboration • Isolated objects are useless in object-oriented systems. “No object is an island”

  34. Object cooperation is very important in object-oriented software. In general, the behaviour of an OO software is realized through object collaboration.

More Related