chapter7_nn - Chapter 7 ObjectOriented Programming...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing References as Arguments Method-Call Chaining Overloaded Methods Constructors Overloaded Constructors 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Object Creation - a Detailed Analysis Let's start the chapter with a behind-the-scenes detailed look at what  happens when a program instantiates an object and stores its address in  a reference variable. Code fragment: 1. Car car; // reference variable declaration 2. car = new Car(); // object creation 3. car.year = 1998; // assign 1998 to car's year instance variable 1. Space is allocated in memory for the  car  reference variable. The  car   reference variable will hold the address of an object, but since there's no  object created for it yet, it doesn't yet hold a legitimate address. 2. Space is allocated in memory for a new  Car  object. The address of the  allocated space is assigned to  car . 3. The  car  variable's value (the address of a  Car  object) is used to find the  Car  object in memory, and then 1998 can be stored in the  Car  object.  Note that for this assignment to work, we're making the simplifying  assumption that  year  is a  public  instance variable. 2
Background image of page 2
car memory Object Creation - a Detailed Analysis Code fragment: 1. Car car; 2. car = new Car(); 3. car.year = 1998; 1062 1062 1998 ? 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Assigning a Reference The result of assigning one reference variable to  another is that both reference variables then point to  the same object. With both reference variables pointing to the same  object, if the object is updated by one of the  reference variables, then the other reference variable  will notice that change when it attempts to access the  object. That can be disconcerting! 4
Background image of page 4
Assigning a Reference Suppose you want to create two  Car  objects that  are the same except for their color. Your plan is to  create the first car, copy the first car to the second  car, and then update the second car's  color   instance variable. Will this code accomplish that? Car stacyCar; Car johnCar = new Car(); johnCar.setMake("Honda"); johnCar.setYear(2003); johnCar.setColor("silver"); stacyCar = johnCar; stacyCar.setColor("peach"); 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The problem with the previous slide's code is that the  stacyCar = johnCar;  statement causes the two references  to point to the same single  Car  object. Thus,  johnCar 's color  becomes "peach" (and that was not intended). johnCar = new Car();
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

chapter7_nn - Chapter 7 ObjectOriented Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online