13.07.2015 Views

4. Object Structures, Aliasing, and Encapsulation

4. Object Structures, Aliasing, and Encapsulation

4. Object Structures, Aliasing, and Encapsulation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example:(Role confusion)• Array is internal representation of the list• Method addElems confuses alias modesclass ArrayList {private /* rep */ int[ ] array;private int next;}public void addElems( int[ ] ia ) {array = ia;next = ia.length;}…Clean solution requires array copy:public void addElems( int[ ] ia ) {array = new /* rep */ int[ ia.length ];System.arraycopy (ia, 0, array, 0, ia.length );next = ia.length;}13.05.13© A. Poetzsch-Heffter, TU Kaiserslautern28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!