Presentation is loading. Please wait.

Presentation is loading. Please wait.

H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.

Similar presentations


Presentation on theme: "H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation."— Presentation transcript:

1 H OW O BJECTS B EHAVE

2 O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation Using references in an Array

3 M ETHODS USE OBJECT STATE In a non-OOP language, functions need to have lots of parameters Alternatively, you can have lots of functions Using methods, many objects contain instance variables that can be used to change methods.

4 E XAMPLE if (dog_a_weight < 14){ small_bark(); } else { large_bark(); } We had to have more functions for each ‘state’ of dog. We would also need to do this for each dog!

5 J AVA E XAMPLE The Java example on the webpage shows how a dog’s barks changes with size

6 A RGUMENTS AND RETURN TYPES IN METHODS Just like in C functions, Java methods can take in arguments and return values The syntax is identical to C E.g. int somefunction(int somevalue){ //Some code that uses the variable somevalue return 45; // Returns 45 }

7 A RGUMENTS AND RETURN TYPES IN METHODS You can send multiple arguments as well The order of the arguments matters void someotherfunction(int x, int y)

8 J AVA PASSES BY VALUE When calling a method, a copy of each argument is made for the function In C, you can pass pointers where the function manipulates the original value If you want a function to manipulate a value in Java, the value needs to be returned, or be an instance variable

9 G ETTERS AND S ETTERS Getters = Accessors Setters = Mutators Getters and Setters are simple methods that let programs manipulate an objects instance variables Why does this matter? Cant we access these values already with the dot operator?

10 E NCAPSULATION This is the concept of keeping our data safe so that only valid value can be put in instance variables We set instance variables to private We set setters and getters to public

11 E XAMPLE Lets modify our previous example to use the concept of encapsulation We will even add some simple error checking

12 I NSTANCE V ARIABLES VS. L OCAL V ARIABLES Just like in C, where variables are defined sets their ‘scope’ Instance variables are accessible by all methods and are declared in the class outside of any method Local variables only apply to one method and are defined at the top of the method they are used in

13 C OMPARING O BJECTS Simple comparison of primitives can be done with ‘==‘ How can we do this with objects? To see if two object reference variables are the same, still use ‘==‘ To see of the instance values in two objects are the same, write an ‘equals()’ method and use it


Download ppt "H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation."

Similar presentations


Ads by Google