Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Second Look at Classes and Objects - 2

Similar presentations


Presentation on theme: "A Second Look at Classes and Objects - 2"— Presentation transcript:

1 A Second Look at Classes and Objects - 2
Chapter 9 A Second Look at Classes and Objects - 2

2 Contents Passing Objects As Arguments to Methods
Returning Objects from Methods The toString Method Writing an equals Method Methods That Copy Objects

3 I. Passing Objects As Arguments to Methods
When a variable is passed as an argument to a method, it is said to be passed by value. This means that a copy of the variable's value is passed into the method's parameter. When the method changes the contents of the parameter variable, it does not affect the contents of the original variable that was passed as an argument.

4 I. Passing Objects As Arguments to Methods
To pass an object as a method argument, we pass an object reference. Reference variable holds the memory address of an object. The method has access to the object that the variable reference. When writing a method that receives the value of a reference as an argument, we must take care not to accidentally modify the contents of the object that is referenced by default.

5

6 I. Passing Objects As Arguments to Methods
Passing a reference as an argument A Rectangle object changeRectangle(box); public static void changeRectangle(Rectangle r) { r.setLength(0.0); r.setWidth(0.0); } length: 12.0 width: 5 address

7 I. Passing Objects As Arguments to Methods
Both box and r reference the same object A Rectangle object length: 12.0 width: 5 The box variable holds the address of a Rectangle object. address The r variable holds the address of a Rectangle object. address

8 II. Returning Objects from Methods
A method can return a reference to an object. The address of the object is then returned from the method.

9

10 II. Returning Objects from Methods
account = getAccount(); A BankAccount object balance: address public static BankAccount getAccount() { (Statements appear here) return new BankAccount(balance); } The getAccount method returns a reference to a BankAccount object.

11 III. The toString Method
Most classes can benefit from having a method named toString: Is implicitly called under certain circumstances Typically, the method toString returns a string that represents the state of an object. An object's state is simply the data that is stored in the object's fields at any given moment. For example, the value of the balance field represents the BankAccount object's state at that moment.

12 III. The toString Method
An example of a class, which holds data about a company's stock: symbol: holds the trading symbol for the company's stock sharePrice: holds the current price per share of the stock. UML diagram for Stock class Stock - symbol : String - sharePrice : double + Stock(sym : String, price : double) + getSymbol() : String + getSharePrice() : double + toString : String

13 III. The toString Method

14 III. The toString Method

15 III. The toString Method

16 III. The toString Method
Evey class automatically has a toString method that returns a string containing: the object's class name, and symbol, and an integer that is usually based on the object's memory address.

17 IV. Writing an equals Method
How to determine whether two objects contain the same data? We cannot compare them with == operator. Instead, the class must have a method such as equals for comparing the contents of objects. Stock com1 = new Stock(“XYZ”, 9.62); Stock com2 = new Stock(“XYZ”, 9.62); if(com1 == com2) System.out.println(“Both objects are the same.”); else System.out.println(“The objects are different.”); This is a mistake.

18 IV. Writing an equals Method
symbol: “XYZ” sharePrice: 9.62 A Stock object The com1 variable holds the address of a Stock address. address The if statement compares these two address. symbol: “XYZ” sharePrice: 9.62 A Stock object The com2 variable holds the address of a Stock address. address

19 IV. Writing an equals Method
The equals method accepts a Stock object as its argument. public boolean equals(Stock object2) { boolean status; //Determine whether this object's symbol and //sharePrice fields are equal to object2's //symbol and sharePrice fields if(symbol.equals(object2.symbol) && sharePrice == object2.sharePrice) status = true; else status = false; return status; }

20 IV. Writing an equals Method
This code uses the Stock class's equals method to compare two Stock object. Stock com1 = new Stock("XYZ", 9.62); Stock com2 = new Stock("XYZ", 9.62); if(com1.equals(com2)) System.out.println("Both objects are the same."); else System.out.println("The objects are different.");

21 V. Methods That Copy Objects
How to make a copy of an object? Stock com1 = new Stock(“XYZ”, 9.62); Stock com2 = com1; This is a mistake. - This does not make a copy of the object referenced by com1. - It makes a copy of address that is stored in com1 and stores that address in com2.

22 V. Methods That Copy Objects
Stock com1 = new Stock(“XYZ”, 9.62); Stock com2 = com1; symbol: “XYZ” sharePrice: 9.62 A Stock object The com1 variable holds the address of a Stock address. address The com2 variable holds the address of a Stock address. address Both variables reference the same object. This type of assignment operation is called a reference copy because only the address is copied, not the actual object itself.

23 V. Methods That Copy Objects
We should write a method in the class Stock named copy, which returns a copy of a Stock object: public Stock copy() { //Create a new Stock object and initialize it //with the same data held by the calling object Stock copyObject = new Stock(symbol, sharePrice); //Return a reference to the new object return copyObject; }

24 V. Methods That Copy Objects
Copy Constructors Another way to create a copy of an object is to use a copy constructor. A copy constructor is simply a constructor that accepts an object of the same class as an argument. It makes the object that is being created a copy of the object that was passed as an argument.

25 V. Methods That Copy Objects
The Copy Constructor of the Stock class: public Stock(Stock object2) { symbol = object2.symbol; sharePrice = object2.sharePrice; } How to use the copy constructor? Stock com1 = new Stock(“XYZ”, 9.62); //Create another Stock object that is //a copy of the com1 object Stock com2 = new Stock(com1);


Download ppt "A Second Look at Classes and Objects - 2"

Similar presentations


Ads by Google