Download presentation
Presentation is loading. Please wait.
1
How Objects Behave Chapter 4
2
Methods Up until this point, our methods haven’t done much – displaying methods We know how to instantiate objects and how to call them The power is in Passing the method’s data = giving those methods data and they can react based on the information.
3
Page 73 Dog Class and Dog Test Drive
Bark Method –decision-making we’re familiar with – conditional stmts based on size Dog TestDrive – new Object – one.size = 70 Two.size = 8; three.size = 35 Call the method Bark – result is different based on size Each object has a bark method Instance variables for each method are different
4
You can pass values (send things) to a Method
Page 74 – Very Important Concept Up until this point a method name has had blank parentheses: Example d.bark() Specify the type of data you pass it in () A method uses parameters - what is req’d A caller passes arguments d.bark(3) Look at example – bottom of page 74
5
You can get things back from a method
Bark method Example on p. 74 doesn’t return anything Void means the method does not return a value; it just does something. Methods can return values Must put the data type of the value you are sending back
6
Example int theSecret = life.giveSecret() int giveSecret () { return 42; } Data type that you are sending back must declare a data type; what you say you’ll give back you better give back
7
Returning Numbers Sometimes methods need to do calculations;
Sometimes need to figure something out or come up with an answer The method then needs to “send it back”
8
Page 76 – Sending things back
You can send more than one thing to a method Look at example – page 76 Methods can have multiple parameters Separate with commas Separate the arguments with commas You can pass variables into a method, as long as the variable type matches the parameter type. Example page 76 at bottom t. takeTwo(12, 34) 12 and 34 are passed into x and y Value of foo and value of bar become x and y
9
Page 77 Java is pass-by-value That means pass-by-copy
Big idea – you can pass one, two …. Values separated by commas Most of the time we will be passing variables – the value of the variable to the method Also, you are passing a copy of the method; original stays the same.
10
Page 82 Encapsulating the GoodDog class
Want to make sure that the user is forced to use an accurate value for the class Instance variable called size of type integer GetSize and SetSize When we create new objects and we want to assign our object a size, we have to call the SetSize method.
11
Encapsulation Getter and Setter Methods
Allows you to protect methods so that numbers that are out of range, etc. don’t get entered in. You accomplish this by making the instance variable private, rather than public Variable is only available within the class Getter and setter methods are public
12
New object One.setSize(70)
one based on GoodDog class One.setSize(70) 70 passed into the setSize method We can put checks and balances into the program to be sure that variables are assigned correctly
13
GetSize Method GetSize – must be changed to the data type that we are returning. Way to make sure other programmers use the classes appropriately. Responsible for returning a data type that we’re returning One.GetSize A failsafe to be sure that other programmers use your methods appropriately.
14
Private methods - encapsulating
Private means that this variable size is private to this class Things inside this class – methods – are the only ones that have access to the variable within the class Getter and setter methods will be used to interact You can only access using the getter & setter
15
Instance Variables & Local Variables
Important to know what they’re set to and how they react Page 84-85 Variable declarations need a name and a type You can initialize and assign a value. What is the value of the variable before you assign a value? Explanation page 84 Number primitives value = 0; boolean’s value =false; object reference variables value null.
16
Page 85 Difference between instance variables and local variables
Instance variables – declared inside a class, but NOT within a method Local variables – declared within a method Local variables must be initialized before you use them. Example – page 85
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.