Age age cloneweight weight return clone invoked as

This preview shows page 49 - 58 out of 61 pages.

clone.age = age; clone.weight = weight; return clone; } Invoked as tom = mary.deepCopy(); //copy mary into tom
Image of page 49

Subscribe to view the full document.

50 Arrays of Objects Three step declaration for n objects 1- Declare a variable to store the array address 2- Declare an array of n reference variables 3- Declare the n objects and store their addresses in the array elements Example Person[ ] employee; // Step 1 employee = new Person[3]; // Step 2 for ( int i = 0; i < 3, i++) // Step 3 employee[i] = new Person(25, 187.6);
Image of page 50
51 The Memory Model of an Array of Objects Three Person objects location 100 null 100 25 187.6 age weight location 200 null 300 25 187.6 age weight null 200 25 187.6 age weight location 300 null 50 location 50 employee Person[ ] employee; employee = new Person[3]; for ( int i=0; i< 3, i++); employee[i] = new Person(25, 187.6);
Image of page 51

Subscribe to view the full document.

52 Containership Objects that reference other objects The class definition declares reference variables The class’ constructor usually allocates the objects public class WeightLossClass { private Person person1, person2, person3; public WeightLossClass( int a1, double w1, int a2, double w2, int a2, double w3) { person1 = new Person(a1, w1); // allocate three Persons person2 = new Person(a2, w2); person3 = new Person(a3, w3); } // end of constructor } // end of WeightLossClass class
Image of page 52
53 Containership Memory Model 21 age weight 300 54 age weight 250 age weight person1 person 2 person 3 class1 A WeightLossClass Object Containing Three Person Object References 3 Person objects a WeightLossClass object 32 400
Image of page 53

Subscribe to view the full document.

54 Inheritance The concept of a new class (child class) that expands an existing class (parent class) The child class Has all the data and methods of the parent class Adds data or methods Contains the keyword extends in its heading Can write its own version of the parent’s method(s) Saves work and protects parent source code Do not need the source code of the parent, only the byte code (. class ) file
Image of page 54
55 Syntax of Inheritance To define a child class WeightLossClient that inherits from the parent class Person we write: public class WeightLossClient extends Person { // additional member data are coded here private double goalWeight; // additional member methods are coded here public double getGoalWeight() { return goalWeight; } // end of getGoalWeight method } // end of WeightLossClient class
Image of page 55

Subscribe to view the full document.

56 Parent and Child Reference Variables These variables follow a family axiom: “parents can point to children, but children can never point to parents” A parent reference variable can store the address of a parent or a child object A child reference variable can store the address of a child object, but not a parent object Child object addresses stored in parent reference variables can be coerced into child reference variables: childRefVar = (ChildClass) parentRefVar;
Image of page 56
57 Generics A language feature for generalizing the type of data a method or class will process The data types are specified by the code That invokes of the method That declares an object in the class Method parameter types or class data types can be generic types
Image of page 57

Subscribe to view the full document.