


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document from the ohio state university's computer science and engineering department covers the concepts of aliasing, immutability, accessors, and mutators in java. It explains the importance of defensive copying to prevent aliasing and maintain class invariants. The document also discusses the differences between mutable and immutable classes and provides examples of immutable classes like string and wrapper classes.
What you will learn
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
Computer Science and Engineering College of Engineering The Ohio State University
Computer Science and Engineering The Ohio State University
A method that reads, but never changes, the (abstract) state of an object Concrete representation may change, so long as change is not visible to client eg Lazy initialization Examples: getter methodsExamples: getter methods, toString toString Formally: restores “this”
A method that may change the (abstract) state of an object Examples: setter methods Formally: updates “this” Constructors not considered mutators
Computer Science and Engineering The Ohio State University
// Interface cover story goes here // Mathematical modeling … // constructor // requires start date < end date // initializes start and end dates // operations have specifications based on the model // exercises …
public interface FixedEpoch { public Date getStart(); public Date getEnd(); }
Computer Science and Engineering The Ohio State University
public class Period implements FixedEpoch { private Date start; private Date end;
public Period(Date start, Date end) { this.start = start; this.end =t s.e d end;e d; }
public Date getStart() { return start; }
public Date getEnd() { return end; } }
Computer Science and Engineering The Ohio State University
Client and component both have references to the same Date object
Date t1 = new Date(300); Date t2 = new Date (500); Period p = new Period (t1, t2); t2.setTime(100); // modifies p’s rep
Constructor creates a copy of the arguments Copy is used to initialize the private fields Metaphor: ownership
Computer Science and Engineering The Ohio State University
public class Period implements FixedEpoch { private Date start; private Date end;
public Period(Date start, Date end) { this.start = new Date(start.getTime()); this.end =t s.e d new Date(end.getTime());e ate(e d.get e()); }
public Date getStart() { return start; }
public Date getEnd() { return end; } }
Computer Science and Engineering The Ohio State University
Client can still obtain a reference to the class’s internal representation (the private fields) aka “privacy leak”, but really just an alias problem
Date t1 = new Date(300); Date t2 = new Date (500); Period p = new Period (t1, t2); p.getEnd().setTime(100); // modifies p’s rep
Accessors create a copy of internal fields Copy is returned to the client
Computer Science and Engineering The Ohio State University
public class Period implements FixedEpoch { private Date start; private Date end;
public Period(Date start, Date end) { this.start = new Date(start.getTime()); this.end =t s.e d new Date(end.getTime());e ate(e d.get e()); }
public Date getStart() { return new Date(start.getTime()); }
public Date getEnd() { return new Date(end.getTime()); } }
Computer Science and Engineering The Ohio State University
Problem: Aliases undermine the privacy of a field Solution: Prevent aliases to fields
Parameters in constructors and mutators Return value from any method
Fields that are primitive (eg int, float) Fields that are enumerations (eg Suit, Colors) Fields that are… (next slide)
Computer Science and Engineering The Ohio State University
Constructor allows initialization to different values No mutator methods
Having multiple references to the same immutable is indistinguishable from having multiple references to different immutables that have the same value Defensive copies of immutables are not required!
Computer Science and Engineering The Ohio State University
Computer Science and Engineering The Ohio State University
Result: no mutators You do that anyway, right?
You do that anyway, right?
Rule: If the class has fields that refer to mutable objects
Computer Science and Engineering The Ohio State University
Copy constructor arguments (reference types) Return only copies of fields (reference types)
Each instance represents a distinct valueEach instance represents a distinct value No mutators: no methods alter “this” Methods can return a new instance Defensive copying of mutable fields
String Wrapper classes (Integer, Long, Float…)