Objects by Sufian Idris


Published on

Slides on objects by Sufian Idris

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Refine
  • Picture showing someone ordering food at a restaurant
  • Objects by Sufian Idris

    1. 1. Objects
    2. 2. <ul><li>Object is a very important concept in object-orientation. </li></ul><ul><li>When an OO program is executed, it normally creates objects in memory. </li></ul><ul><li>These objects collaborate with each other to perform certain tasks. </li></ul><ul><li>This realizes the runtime behaviour of the program. </li></ul>
    3. 3. Object Space $$ $$ Kad ATMku Kad ATMku
    4. 4. Definition of Object <ul><li>An object is a thing that has state , behaviour and identity . </li></ul><ul><li>Examples: </li></ul><ul><ul><li>The LCD projector in the lecture hall at FTSM. </li></ul></ul><ul><ul><li>Dr Sufian’s Kenari </li></ul></ul><ul><ul><li>Dr Sufian’s Waja </li></ul></ul>
    5. 5. Object State <ul><li>Each object has attributes which collectively represent its state . </li></ul>BEFORE AFTER
    6. 6. <ul><li>In Java, the attributes of an object are implemented as variables that belong to that object. These variables are called instance variables . </li></ul>Example: A Car object Another Car object Car “ WHH7801” green “ Sufian Idris” colour plateNr owner Car “ WHY9531” blue “ Ali Bakar” colour plateNr owner
    7. 7. <ul><li>The atributes of an object normally do not change. </li></ul><ul><li>The values of the attributes of an object can change. However, there are attributes with fixed values. </li></ul>Fridge “ 342-0967” “ Ali Bakar” biru owner serialNr colour value is fixed values can be changed
    8. 8. <ul><li>An object may have links to other objects. These links represent relationships between those objects. </li></ul><ul><li>Example: </li></ul>The links are added to represent the fact that both cars are owned by the same person. : Car : Person : Car links
    9. 9. <ul><li>Example: </li></ul>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. : Car : Engine : Chassis links
    10. 10. <ul><li>In Java, links to objects are represented as object references . </li></ul><ul><li>Instance variables can be defined to store such references. </li></ul>: Computer datePurch processor memory 15032002 : Memory size 256 : Processor speed 900 object references
    11. 11. Object Behaviour <ul><li>Message sending is a mechanism which objects use to interact with other objects. </li></ul><ul><li>To request an object to perform a certain task, an appropriate message needs to be sent to that object. </li></ul>jump
    12. 12. <ul><li>An object only responds to messages that it understands. </li></ul>fly ??
    13. 13. <ul><li>Object behaviour refers to how an object reacts to messages it receives from its clients. </li></ul>The Scanner object responds by inputting a float value and returning that value to the TemperatureConverter object. : Scanner : Temperature Converter nextFloat()
    14. 14. <ul><li>An object’s response to a message can cause its state to change. </li></ul>: Rectangle 5 colour width height 10 green zoom_2x 10 20
    15. 15. <ul><li>An object may also send messages to other objects in its response to a message. </li></ul>switchChannel(5) openCommChannel() switchChannel(5) closeCommChannel() Saluran Ceria Anda
    16. 16. Object Identity <ul><li>Each object has its own identity which differentiates it from other objects. </li></ul>Triplets… but each of them has its own identity
    17. 17. <ul><li>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. </li></ul>class Computer { private int computerID; private Date datePurchased; private Processor processor; … } Redundant
    18. 18. Relationship between Object State and Object Behaviour <ul><li>Object behaviour can affect object state. </li></ul><ul><li>Example: </li></ul><ul><ul><li>An ATM machine dispensing money causes the amount of money in the machine to decrease. </li></ul></ul>
    19. 19. <ul><li>Object state can affect object behaviour. </li></ul>Number of tins: 20 What if there are no more tins in the machine? dispense() Koka Koola
    20. 20. Abstraction <ul><li>Supposing we need to define an object that represents a cat. What attributes and methods should we define? </li></ul>weight length heart size chew drink run … … … there are too many! Attributes Behaviour
    21. 21. <ul><li>Humans have the ability to isolate those aspects that are important for some purpose and suppress those aspects that are unimportant . </li></ul>
    22. 22. <ul><li>A program designer only needs to define the attributes and behaviour that are essential to the problem in hand. </li></ul><ul><li>In object-orientation, software objects are abstractions. </li></ul>: Bird 5 colour weight height 10 white 14 0.5
    23. 23. Encapsulation <ul><li>Objects should not reveal all of its information. There is certain information that should be hidden. </li></ul><ul><li>Example </li></ul><ul><ul><li>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. </li></ul></ul>
    24. 24. <ul><li>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. </li></ul>
    25. 25. <ul><li>Information normally hidden by objects: </li></ul><ul><ul><li>object attributes </li></ul></ul><ul><ul><li>the specifics of how they react to the messages received by them </li></ul></ul>40 area() attributes: width height … behaviour: area() return width * height; HIDDEN BY OBJECT
    26. 26. <ul><li>Some benefits of encapsulation: </li></ul><ul><ul><li>if done properly, modifications to object implementation should not affect clients </li></ul></ul><ul><ul><li>objects can control access to their attributes </li></ul></ul>
    27. 27. Object Interface <ul><li>Not all messages are understood by an object. </li></ul><ul><li>To obtain information on the messages which an object understands, we need to refer to its interface. </li></ul>
    28. 28. <ul><li>The interface of an object reveals public information about the object. It should not contain information hidden by the object. </li></ul><ul><li>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. </li></ul>
    29. 29. INTERFACE area() setWidth(int width) setHeight(int height) ?? yesss! But where can we find information about an object’s interface? Rectangle object
    30. 30. Messages and Methods <ul><li>Message sending is a mechanism used by objects to communicate with other objects. </li></ul><ul><li>To get an object to do something, you send an appropriate message to that object. </li></ul>jump
    31. 31. <ul><li>A message consists of its name (compulsory) and its parameters (if any). </li></ul><ul><li>The receiver object is the object receiving the message. The sender of the message is referred to as the client. </li></ul>switchChannel(5) openCommChannel() switchChannel(5) closeCommChannel() client receiver object Saluran Ceria Anda
    32. 32. <ul><li>An object implements its behaviour with methods . A method contains implementation details of how an object responds to a certain message. </li></ul>40 area() attributes: width height … behaviour: area() return width * height; message method
    33. 33. Object Collaboration <ul><li>Isolated objects are useless in object-oriented systems. </li></ul>“ No object is an island”
    34. 34. <ul><li>Object cooperation is very important in object-oriented software. In general, the behaviour of an OO software is realized through object collaboration. </li></ul>