PK - Chapter 6 Flashcards

1
Q

What is a backing field?

A
Generated for a property in a class
if it uses the default implementation
of at least one of the accessors.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you access a backing field?

A

The backing field can be accessed
inside a custom accessor using the field keyword.

var firstName : String
get() = field
set(value) {field = value}

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

Why does a Property
need a Backing Field?

A

object.SomeProperty = “value”
is the same as
object.setSomeProperty(“value”)

so the following would be recursive
and generate a StackOverflow
———————-

var firstName : String
get() = firstName
set(value) {firstName = value}

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

How would you create a public property
with a private setter?

A

var firstName : String
get() = firstName
set(value) {firstName = value}

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

What is the purpose of lateinit?

A

Non-null properties have to be
initialized in the constructor, unless
they are defined with the lateinit keyword.

lateinit var delayedInitProperty : DelayedInstance

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

What are the restrictions for
using Late Initialization?

A

The property:

  • cannot be a primitive type
  • cannot have custom getters or setters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happes if you access a lateinit
property before it has been initialized ?

A

kotlin.UninitializedPropertyAccessException

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

Why use Late Initialization?

A

You do not initialize a non-null
variable in the constructor:

  • dependency injection
  • value not available during object construction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why use a delegated property?

A
  • to write the getters and setters only once (DRY) for a common type of property in the application domain
  • to guarantee that data constraints are applied uniformly across the application/library

var eventTimestamp : Long by
TimestampPropertyDelegate()

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

Write the code for TimestampPropertyDelegate.

A

class TimestampPropertyDelegate {
private var timestamp = 0L
operator fun getValue(
ref: Any?, // object reference
property: KProperty<*>): Long {
return timestamp;
}

operator fun setValue(
ref: Any?, // object reference
property: KProperty<*>,
value: Long) {
timestamp = value
}
}

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

How are property delegates
similar to database domain constraints?

A

You can create a data dictionary of
property delegates that enforce
data integrity for the logical data types
in your application domain.

A change to a property delegate in the
data dictionary will consistently
propogate across the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
What interface(s) should a
property delegate implement ?
A
  • ReadOnlyProperty
  • ReadWriteProperty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why use lazy initialization?

A

To improve performance or resource utilization:

  • you don’t want to incur the cost of initialization
    unless the object is actually needed
  • you may want to delay initialization until more
    critical processing is complete
  • you may want to delay initialization until
    adequate resources are available
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create a lazy initialization property.

A

val foo: Int by lazy {
getSomeInteger()
}

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

What are the
LazyThreadSafeteyMode enums?

A
  • SYNCHRONIZED
    uses locks
  • PUBLICATION
    initializer can be called several times,
    but only the first value will be used
  • NONE
    no locks are used.
    high performance when it can only
    be initialized by one thread
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Compare lateinit versus lazy.

A
  1. lazy can only be used for val
    lateinit
    can oly be used for var
  2. a lateinit cannot be immutable
  3. lateinit has a backing field
  4. lateinit cannot be used for nullable properties
  5. lateinit can be initialized from anywhere,
    the lazy property must define its initializer