05 object behavior


Published on

  • 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
  • http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=QUE&q=the+answer+to+life+the+universe+and+everything&btnG=Search&aq=f&oq=&aqi=g10
  • 05 object behavior

    1. 1. How Objects Behavemethods use instance variables<br />LIS4930 © PIC<br />You already know that each instance of a class can have its own unique values for instance variables. Dog A can have a name “Rufus” and a weight of 70lbs. Dog B is “Killer” and weighs 9 pounds. And if the Dog class has a method makeNoise(), well, don’t you think a 70lb dog barks a bit deeper than the little 9-pounder?<br />Rufus<br />Killer<br />Fortunately that is the whole point of an object – it has behavior that acts on its state. In other words, methods use instance variable values. Like, “if dog is less than 14lbs, make yippy sound, else…” or “increase weight by 5”.<br />
    2. 2. How Objects Behave<br />LIS4930 © PIC<br />You already know that objects of one type can have different instance variable values. But what about the methods?<br />Can every object of that type have different method behavior?<br />Song<br />title<br />artist<br />knows<br />setTitle()<br />setArtist()<br />play()<br />Sing<br />Travis<br />My Way<br />Sex Pistols<br />My Way<br />Sinatra<br />Sister<br />DMB<br />Politik<br />Coldplay<br />does<br />t2<br />s3<br />Calling play() on this instance will cause “Sing” to play.<br />Calling play() on this instance will cause “My Way” to play. But, not the Sinatra one!<br />Song<br />Song<br />
    3. 3. The Size Affects the Bark!<br />LIS4930 © PIC<br />Dog<br />name<br />bark()<br />
    4. 4. You Can Send Things To A Method<br />LIS4930 © PIC<br />Just as you may have done in other programming languages, you can pass values into your methods. <br />A method uses parameters. A caller passes arguments.<br />Arguments are the things you pass into the methods. An argument lands face-down into a parameter. And a parameter is nothing more than a local variable. A variable with a type and a name, that can be used inside the body of the method.<br />But here’s the important part:<br />If a method takes a parameter, you MUST pass it something<br />
    5. 5. LIS4930 © PIC<br />Call the jump method on the Dog reference, and pass in the value 3 (as the argument to the method).<br />2<br />1<br />The bits representing the int value 3 are delivered into the bark method.<br />argument<br />parameter<br />The bits land in the numOfJumps parameter (an int-sized variable).<br />Use the numOfJumps parameter as a variable in the method code.<br />3<br />4<br />void jump (intnumOfJumps){<br /> while (numOfJumps > 0) {<br />//volunteer jumps once<br />numOfJumps = numOfJumps – 1;<br /> } <br />}<br />Dog d = new Dog()<br />d.jump(3);<br />
    6. 6. You can get things back from a method<br />LIS4930 © PIC<br />Methods can return values. Every method is declared with a return type, but until now we’ve made all of our methods with a void return type, which means they don’t give anything back.<br />void go() {<br />}<br />intgiveNumber () {<br /> return 42;<br />}<br />But, we can declare a method to give a specific type of value back to the caller, such as:<br />If you declare a method to return a value, you must return a value of the declared type! (Or something that is compatible with the declared type!)<br />Whatever you say you’ll give back, you better give back!<br />
    7. 7. LIS4930 © PIC<br />You can get things back from a method<br />00101010<br />theSecret<br />These types MUST match<br />The bits representing 42 are returned from the giveSecret() method, and land in the variable named theSecret.<br />int<br />intgiveSecret ( ){<br />intsecretNum = 42; <br /> return secretNum; <br />}<br />inttheSecret = life.giveSecret( );<br />
    8. 8. LIS4930 © PIC<br />You Can Send MORE Than One Thing To A Method<br />Methods can have multiple parameters. Separate them with commas ( , ) when you declare them, and separate the arguments with commas when you pass them. <br />Most importantly:<br />The arguments you pass land in the same order you passed them. First argument lands in the first parameter, second argument in the second parameter, and so no.<br />Calling a two-parameter method, and sending it two arguments:<br />If a method has parameters, you must pass arguments of the right type and order.<br />void go( ) {<br />TestStufft = new TestStuff( );<br />t.takeTwo (12, 34);<br />}<br />void takeTwo( intx, inty ) {<br />intz = x + y;<br />System.out.println(“Total is “ + z);<br />}<br />
    9. 9. LIS4930 © PIC<br />You Can Send MORE Than One Thing To A Method<br />You can pass variables into a method, as long as the variable type matches the parameter type.<br />What’s the value of z? It’s the same result you’d get if you divided snicker by bar at the time you passed them into the takeTwo method.<br />The values of snicker and bar land in the x and y parameters. So now the bits in x are identical to the bits in foo (the bit pattern for the integer ‘7’) and the bits in y are identical to the bits in bar.<br />void go( ) {<br />int snicker = 8;<br />int bar = 4;<br />t.takeTwo(snicker, bar);<br />}<br />void takeTwo( intx, inty ) {<br />x = 13;<br />intz =x / y;<br />System.out.println(“Resultis “ + z);<br />}<br />
    10. 10. Java is Pass-by-Value. That Means Pass-by-copy.<br />LIS4930 © PIC<br />1<br />2<br />3<br />4<br />Declare an int variable and assign it the value ‘7’. The bit pattern for 7 goes into the variable named x.<br />00000111<br />intx = 7;<br />X<br />int<br />Declare a method with an int parameter named z.<br />Z<br />void go(intz) { }<br />int<br />00000111<br />00000111<br />Call the go() method, passing the variable x as the argument. The bits in x are copied, and the copy lands in z.<br />Z<br />copy of x<br />X<br />int<br />int<br />foo.go(x);<br />void go (intz);<br />x and z aren’t connected<br />Change the value of z inside the method. The value of x doesn’t change! The argument passed to the z parameter was only a copy of x.<br />The method can’t change the bits that were in the calling variable x. <br />00000111<br />0000000<br />X<br />Z<br />int<br />int<br />void go (intz) { z = 0; }<br />
    11. 11. There are no Dumb Questions!<br />LIS4930 © PIC<br />Q: What happens if the argument you want to pass is an object instead of a primitive?<br />A: Java passes everything by value. Everything. But value means bits inside the variable. And remember, you don’t stuff objects into variables; the variable is a remote control – a reference to an object. So if you pass a reference to an object into a method, you’re passing a copy of the remote control.<br />Q: Do I have to return the exact type I declared?<br />A: You can return anything that can be implicitly promoted to that type. But, you must use an explicit cast when the declared type is smaller than what you’re trying to return.<br />
    12. 12. Getters & Setters<br />LIS4930 © PIC<br />Now that we’ve seen how parameters and return types work, it’s time to put them to good use: Getters and Setters.<br />Getters and Setters are methods that get and set instance variables.<br />A Getter’s sole purpose in life is to send back, as a return value, the value of whatever it is supposed to be Getting.<br />ElectricGuitar<br />brand<br />numOfPickups<br />rockStarUsesIt<br />getBrand()<br />setBrand()<br />getNumOfPickups()<br />setNumOfPickups()<br />getRockStarUsesIt()<br />setRockStarUsesIt()<br />A Setter’s sole purpose is to take an argument value and use it to set the value of an instance variable.<br />
    13. 13. ENCAPSULATIONCover Your Exposed Parts!<br />LIS4930 © PIC<br />Here we have been humming along without a care in the world leaving our data out there for anyone to see and even touch.<br />Exposed means reachable with the dot operator, as in:<br />theCat.height = 7;<br />Think about this idea of using our remote control to control the cat, and the remote getting into the hands of the wrong person. A reference variable (the remote) could be quite dangerous. Because what is to prevent:<br />theCat.height = 40;<br />
    14. 14. LIS4930 © PIC<br />ENCAPSULATIONCover Your Exposed Parts!<br />By forcing everybody to call a setter method, we can protect the cat from unacceptable size changes.<br />We put in checks to guarantee a minimum cat height<br />Cat<br />name<br />height<br />color<br />setName()<br />getName()<br />setHeight()<br />getHeight()<br />setColor()getColor()<br />Encapsulation puts a fig leaf over the instance variables, so nobody can set them to something inappropriate.<br />public void setHeight (int ht) {<br /> if (ht > 9) {<br /> height = ht;<br /> }<br />}<br />
    15. 15. Hide the Data<br />LIS4930 © PIC<br />OK so you now know how to protect your data (instance variables) but how do you hide them?<br />You are already familiar with the public key word seen in every main() method; the public key word is called an access modifier and it allows anyone to access (call) that method. Well, there is another access modifier called private. Private only allows members of that very object to access its contents. So for instance, if the instance variables of an object are se to private only that object’s methods can access its instance variables!<br />Here’s an encapsulation starter rule-of-thumb:<br />Mark instance variables private.<br />Mark getters and setters public.<br />
    16. 16. Encapsulating the GoodDog Class<br />LIS4930 © PIC<br />Make the instance variable private<br />Make the getter and setter methods public<br />Even though the methods can’t really add new functionality, the cool thing is that you can change your mind later, You can come back and make a method safer, faster, better.<br />
    17. 17. SUMMARY<br />LIS4930 © PIC<br />ALL variables must have:<br />Access Modifier (optional)<br />Type<br />Name <br />Value (optional)<br />private int kyle = 10;<br />ALL methods must have:<br />Access Modifier (optional)<br />Return type<br />Name <br />Parameters (optional)<br />Curly brackets<br />public void bark(int x) { }<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.