privacy -ch5_2 Flashcards
what is null
constant that may be assigned to any variable of class type indicates that the variable has no real value - often used in constructors place holder
what is the class invariant?
can help to define in a class in a consistent and organized way for example - in person class each person has a birth and death date, the death date must be the same or later than the birth date - a method can be used to check this - this method can be static
in a class you can have different equals methods for different classes of objects - how does java know which .equals() to use?
using the type of object its calling
what is a copy constructor
- constructor with a single argument of the same type as the class
- copy constructor should create an object that is a separate, independent object but has the same instance variable set so that it is an exact copy of the argument object
We then create a copy constructor - allows you to create a copy of the object – instead of returning the reference to the actual date object - we create another object that has the same values as the actual object
copy constructor for a class with primitive type instance variables
public Date(Date aDate) month = aDate.month; day = aDate.day; year = aDate.year;
copy constructor for a class with class type instance variables
e.g.) born = new Date(originalobject.born)
why can the copy constructor not be the same for class type and prim type instance variables
if they were just copied then they would simply rename the instance variables from the original object and not make an independent copy of it
this way of doing things is safe - it creates completely new and independent copies of the instance variables and a new and independent copy of the original object
in order to define a correct copy constructor for a class that has type instance variables…
copy constructors must already be defined for the instance variables’ classes
how should we define accessor methods to prevent privacy leaks?
public Date getDate(){ return new Date(born); } --> this returns a copy and not the original instance variable
why dont we use the return a copy method for the string class or copy constructors for the string class?
the string class is an immutable class -i.e. it contains no mutator methods that can change any data in a String object
define an immutable class
- A class that contains no methods (other than constructors) that change any of the data in an object of the class is called an immutable class
- Objects of such a class are called immutable objects
- It is perfectly safe to return a reference to an immutable object because the object cannot be changed in any way
- The Stringclass is an immutable clas
define a mutable class
A class that contains public mutator methods or other public methods that can change the data in its objects is called a mutable class, and its objects are called mutable objects •Never write a method that returns a mutable object •Instead, use a copy constructor to return a reference to a completely independent copy of the mutable object
deep vs shallow copy
Deep copy example - a copy where there is no reference in common to the original: Person b = new Person (x), b and x are copies of each other but they don’t refer to the same object
Shallow copy - where two references to the same object: Person a = x; a and x refer to the same object
Deep copies are also prefered