privacy -ch5_2 Flashcards

1
Q

what is null

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is the class invariant?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

in a class you can have different equals methods for different classes of objects - how does java know which .equals() to use?

A

using the type of object its calling

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a copy constructor

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

copy constructor for a class with primitive type instance variables

A
public Date(Date aDate)
    month = aDate.month;
    day = aDate.day;
    year = aDate.year;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

copy constructor for a class with class type instance variables

A

e.g.) born = new Date(originalobject.born)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

why can the copy constructor not be the same for class type and prim type instance variables

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

in order to define a correct copy constructor for a class that has type instance variables…

A

copy constructors must already be defined for the instance variables’ classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how should we define accessor methods to prevent privacy leaks?

A
public Date getDate(){
      return new Date(born);
} --> this returns a copy and not the original instance variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

why dont we use the return a copy method for the string class or copy constructors for the string class?

A

the string class is an immutable class -i.e. it contains no mutator methods that can change any data in a String object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

define an immutable class

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

define a mutable class

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

deep vs shallow copy

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly