Objects

Document Sample
Objects Powered By Docstoc
					INFT11-110/71-110
        Section 7

         Objects
                                          Object-orientation
•   Software is very complex          •    Model the real world as a collection
     (perhaps the most complex             of interacting entities, called objects
        thing ever built)                   – everything is an object!

•   Need organisation and structure   •    Objects have defined
    to build and understand                 – properties
                                            – behaviour

•   object-orientation is one
    mechanism addressing these
    aims
                                 Object-oriented Design
OO design quite similar to LEGO ™

•    use small blocks to build bigger objects
•    same kind of block used in different objects
      – some blocks very generic
      – some blocks become highly specialised
•    blocks fit together in specific ways




    OO problem-solving
    • determine all the blocks required
    • determine how to fit the blocks together
                                          What is an Object?
Objects are entities that can perform a   Objects communicate with each other using
   given (defined) set of activities         messages


E.g.,                                     •   each object specifies the actions it can
                                              perform

•   Clock                                 •   sender object sends a message
     – what time is it?                        – to clock: “tell me the time”
     – what make is it?                        – to book: “tell me your title”

•   Book                                  •   receiver object responds
     – what‟s the title?                       – from clock: “it‟s 12pm”
     – who‟s the author?                       – from book: “Zen and the Art of
                                                  Motorcycle Maintenance”
                             Object-orientation Example
Find highest card in poker hand:           Hand of card object: how does it work?

•   What are the objects?                  Hand of cards is a collection of card objects
     – hand of cards                           – what behaviour do card objects
                                                   have?
•   What behaviour do the objects              – card: compare yourself against
    display? (I.e., to what messages do            another card and say whether
    they respond?)                                 you‟re higher or not
      – hand of cards: tell me your
        highest card

•   Note: algorithms from tutorial 1 are
    not object-oriented
                            Object-orientation Example
OO algorithm for highest card in hand:

object: hand-of-cards
message: which-is-highest

answer: hand-of-cards . which-is-
   highest

                             OO algorithm for hand-of-cards.which-is-highest:

                                    highest-so-far is first card object in hand
                                    repeat for all card objects in hand:
                                       next-card-object . compare-to: highest-so-far
                                       (I.e., next-card-object, compare yourself against
                                       highest-so-far object)
                                          If larger: set highest-so-far as next-card-object
                                          If not: do nothing
                                         Classes and Objects
Card objects                              OO Design

                                          •   Define the classes in the system
K         J           9
                                               – what properties do objects of this
                                                 class have?
Each behaves in similar way
    – I.e., each responds to the               – what behaviour do objects of this
       message compare-yourself-to               class display?

Concept of how all cards behave,          •   Declare the actual objects in the system
   rather than specific cards: a class
                                               – introduce the objects

                                               – declare what class they belong to
                                   Messages
A Message has four components
• receiver: to whom is the message going?
• behaviour: what general task do you want the
  receiver to do?
• details: are there any specific details about
  how you want the receiver to do the task?
• return: what kind of response are you
  expecting?
                                         Messages

Examples

  “ clock , tell me the time ”                    ?

   receiver      behaviour       no details     return



  “ son , turn that damned radio off ”

 receiver        behaviour            details      no return
                                           Messages

Examples

  “ honey , buy me a diamond necklace ”

   receiver    behaviour             details   no return



  “ honey , buy me flowers ”

 receiver     behaviour    details
                                               no return
               Sending Messages in Java
Messages are sent in Java through method
 calls:

        receiver . behaviour ( details )

    return = receiver . behaviour ( details )

• The method‟s name identifies the behaviour
• The details are the arguments passed to the
  method‟s parameters
                          Messages in Java

E.g.,

        honey . buy_me ( “flowers”);

        time_now = clock . tell_time (    );



                               note! no
                                details
            Existentialism and Ontology

“What‟s in a name? A rose by any other
  name would smell just as sweet.”

Can an object (a thing) exist without a
 name?

Can a name exist without an object?
                     Existentialism Examples

   • People
       – same person can have several names
       – same name can be used for several people

Sir Donald Bradman       Donald           Donald Trump




     The Don
                Existentialism Examples

• Houses
  – the building is the object
  – the address is the object reference




 69 Main St
 Hometown Qld
                      Existentialism Examples
Names without objects
     “When I have a baby, I‟m going to call it „Rachel‟”
     • name exists
     • baby does not exist yet


Objects without names
     Doctor: “What are you going to call him?”
     Proud Father: “I don‟t know yet”
     • baby exists
     • name does not exist yet
                    Existentialism and Java

Introducing a name in Java:
  – names refer to objects
  – (in Java) must associate a class (data-
    type) with a name
    • i.e., the new name is going to refer to an object
      of that class
       Introducing Object References in
                                    Java
Java syntax for declaring an object
  reference:

           class-name object-reference ;


E.g.,
        Card highest-so-far ;
        Person honey;                 note syntax
        Clock myClock;
          Introducing New Objects in Java
Java syntax for creating a new object:

        new class-name ( details )

E.g.,
        new Person( )
        new Card (“ace of spades”)
        new Clock( )
          Attaching object references to
                          actual objects

To make a new, named object in Java:
1. make a new, unnamed object of some
   class
2. associate it with an object reference to
   an object of that class
                Attaching object references to
                        actual objects in Java
In Java:

class-name object-reference ;
object-reference = new class-name ( details ) ;


E.g.,
   Person honey;                        Card x;
   honey = new Person ( ) ;             x = new Card ( “ace_of_spades” );


   Card motörhead;
   motörhead = new Card ( “ace_of_spades” );
                                                                Strings
Strings are objects
                                        Object references           Objects

In Java:

String s = new String(“Batman”);
                                          s
String t = new String(“Robin”);
String u = new String(“Batman”);                            B   a   t       m   a   n


                                          t
Objects respond to messages:
                                                            R   o       b   i   n
In Java:

System.out.println(   s.length()   );         u
                                                            B   a       t   m   a   n
                     Objects and Object References
  Object references are not objects       Objects are not object references




String s = new String(“Batman”);      s
String t = new String(“Robin”);                       B    a   t       m   a   n
String u = s;
                                      u

System.out.println(s);     Batman

                                      t
System.out.println(t);     Robin
                                                       R   o       b   i   n
System.out.println(u);     Batman
                    Objects and Object References
Object references are not objects               Objects are not object references


                                                              B       a       t       m       a       n
s = “Robin”;
                                        u

System.out.println(s);     Robin

System.out.println(u);     Batman
                                                              R       o           b   i       n
t = u + t;
                                        s
                                                              R       o           b   i       n

                            t

                                    B       a     t   m   a       n       R       o       b       i       n
                   Objects and Object References
  Object references are not objects            Objects are not object references


String s = new String(“Batman”);          s
String t = new String(“Robin”);                            B    a   t       m   a   n
String u = new String(“Batman”);
                                          t
int x = 10;
int y = 5;                                                  R   o       b   i   n
int z = 5;
                                          u
                                                           B    a   t       m   a   n

x == y ?           false
y == z ?           true               x   10


s == t   ?         false
                                      y   5
s == u ?           false
s.equals(u) ?      true
                                      z   5
                                   Unnamed Objects
Objects can exist without names
E.g.,                                           a String object

            System.out.println( “Baron Greenback” );


                                                       a Point object


            jLabel1.setPosition( new Point(10,10) );



                                                   a Frame1 object


              Application.run( new Frame1( ) );
                       The Class Tour




http://java.sun.com/j2se/1.3/docs/api/

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:12/17/2011
language:English
pages:27