PK - Chapter 3 Flashcards

1
Q

Declare a class named Foo.

A

class Foo {

}

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

What is the default access level of a class ?

A

Public

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

Explain class constructors.

A
  • as in Java, an empty constructor is generated by default
  • a primary constructor can be specified in the class declaration
  • parameters in the primary constructor are _properties
    of the instance
    _
  • secondary constructors can be created in the body of
    the class using the constructor keyword
  • constructors can reference other constructors
  • secondary constructors must directly/indirectly invoke the
    primary constructor; primary must always be invoked
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create a primary constructor for class Foo, with data validation.

A
class Foo (firstArg: String?, secondArg: Int?) {
 init {
 require(! firstArg.isNullOrEmpty()) { "First argument cannot be null or empty." }
 require(! secondArgument.isNull()) { "Second argument cannot be null or empty." }
 }
}

Note:
require will throw an IllegalArgumentException with the
message provided, if the condition is false.

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

Explain initializer blocks.

A
* syntax is
 init {
 doSomething()
 }
* initializer blocks are executed in the same order as they appear in the class body
* initializer blocks can be interleaved with the property initializers
* parameters of the primary constructor can be used in initializer blocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain primary constructors.

A
  • a primary constructor is specified in the class declaration
  • any code run as part of the primary constructor needs to be in an init block
  • parameters in the primary constructor are properties of the instance,
    if they include the “val” or “var” keyword
  • primary constructor does not require the constructor keyword, if it
    does not have any annotations or visibility modifiers
class Foo private constructor(firstArg: Int, secondArg: String) {
 init { ifNeeded(firstArg }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain secondary constructors.

A
  • secondary constructors can be created in the body of a class using the constructor keyword
  • constructors can reference other constructors
  • secondary constructors must directly or indirectly call the primary constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given the class

class Person constructor(val firstName: String, val lastName: String) {}

val fooPerson = Person(“John”, “Doe”)

Print the person’s first and last name for instance fooPerson.

A

println(“${fooPerson.getFirstName} ${fooPerson.getLastName}”)

Note that the getters (and setters for a var) will not be generated for
primary constructor parameters if val and var are not specified. This
will make them private to the class.

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

What is the recommended visibility
for the constructors or an abstract class ?

A

protected

They would only be visible to derived classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
# Define a *_primary constructor_* for class Foo
that can *_only be instantiated inside the package_*.
A

class Foo internal constructor(firstArg: String, secondArg: Int) {

}

Note that there are no “val” or “var” keywords, so the
constructor parameters are not properties of the object.

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

Summarize visibility levels.

A

4 visibility levels:

  • public
    Visible to everyone everywhere
  • private
    Only visible in the file defining it
  • protected
    Only visible to subclasses, extensions
    and implementers
  • internal
    Visible from anywhere within the module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the access to a private nested class ?

A

Can create/access an instance from the outer class.

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

What is the access to an internal nested class ?

A

Can be created/accessed from anywhere in the module.

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

What is the access to an protected nested class ?

A
Can be created/accessed from any class
that is derived from the containing class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between static and non-static nested classes ?

A

Static nested class:

  • can only access the public members
    of the enclosing class
  • not defined with the inner keyword

Inner class (non-static class):

  • has access to the enclosing class members,
    even if declared private
  • can only be created from an instance of an
    enclosing class
  • defined with the inner keyword
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would you access the “memberOne” variable
in outer class “Alpha”
from inner class “Bravo” ?

A

this@Alpha.memberOne

Since we are referencing it from an inner class,
the visibility of memberOne does not matter.

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

How would you access the “memberOne” variable
in outer class “Alpha”
from nested static class “Bravo” ?

A

this@Alpha.memberOne

The nested static class can only access the
member variable if it is public.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Add a MouseListener to a button,
using an anonymous inner class.

A

button.addMouseListener(adapter : MouseAdapter() {
override fun mouseClicked(event: MouseEvent) { this@OuterClass.doSomething() }
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
# Define a data class named "Person"
with attributes "firstName", "lastName".
A

data class Person(val firstName: String, val lastName: String)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
# Define an enum named "DayOfWeek"
with attribute "properCaseName".
A

enum class DayOfWeek(val properCaseName: String) {
MONDAY(“Monday”),
TUESDAY(“Tuesday”),
WEDNESDAY(“Wednesday”),
THURSDAY(“Thursday”),
FRIDAY(“Friday”),
SATURDAY(“Saturday”),
SUNDAY(“Sunday”)
}

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

What are the built-in properties of an enum instance ?

A

name: String
ordinal: Int

22
Q

Create an interface named “Printable” and
an enum named “Word” that implements Printable.

A
interface Printable {
 fun print(): Unit
}
enum class Word : Printable {
 HELLO {
 override fun print() {
 println("Word is Hello")
 }
 },
 BYE {
 override fun print() {
 println("Word is Bye")
 }
 }
}

val word = Word.HELLO
word.print()

23
Q

Create a class with static methods.

A

Kotlin doesn’t directly support static methods for a class.

Methods accessed statically must be defined
in a companion object.

24
Q

What is a companion object ?

A

“class” is used to define classes
that have multiple instances.

"companion object" is used to define a singleton
object instantiated at runtime.
It is a "class object" defined within a class body.
25
``` Create an *_anonymous companion object_* inside class Foo. ```
``` class Foo { companion object { fun returnOne() : Int = 1 } } ``` This can be referenced as: Foo.a()
26
``` Create a *_named companion object_* inside class Foo. ```
``` class Foo { companion object Barr { fun returnOne() : Int = 1 } } ``` This can be referenced as: Foo.a()
27
What are the characteristics of a *_companion object_* ?
* is a singleton created at runtime for the class * has access to the private methods ​and properties of the containing class's objects * is only useful if it needs access to the private members of the containing class; else just declare "top level" properties and functions in the file * can implement interfaces and extend other classes * can be anonymous or named * can only have one anonymouse companion object
28
Define a static method Foo.bar() that can be called from Java.
``` class Foo { companion object { @JvmStatic fun bar() {} } } ```
29
Create a *_singleton_*.
``` // factory pattern class ServiceManager private constructor(val pName: String) { companion object { fun create(pNewName: String): ServiceManager { return ServiceManager(pNewName) } } } ``` val myServiceManager = ServiceManager.create("SomeName")
30
How would you access method bar() in Foo's companion object explicitly ?
Foo.Companion.bar()
31
What are the *_characteristics of an Interface_* ?
An interface can contain: * abstract methods * method implementations * properties, but not state (no assignments) Implementing or overriding any of these in a class definition
32
What is the difference between an *_interface_* and an *_abstract class_* ?
* an interface cannot contain state, a class can * a class can implement multiple interfaces, but only inherit from one abstract class
33
What are the *_characteristics of Any_* ?
* Any is the base class for Kotlin * if a class does not extend a super class, it extends Any * Any implements a subset of methods found in the Java Object class * common methods: equals(arg list): Boolean hashCode(): Int toString(): String
34
When can you *_extend a class_* ?
``` A class cannot be extended unless it is declared with the *_open_* keyword. ```
35
``` Given the following base class, define a subclass named "CardPayment" with the additional constructor parameters of "description" and "accountNumber". ``` open class Payment(val payment: BigDecimal) { }
``` class CardPayment( val pDescription: String, cardNumber: String, val amount: BigDecimal): Payment(amount) { } ```
36
What is a *_sealed class_* ?
A sealed class cannot be extended. Classes are sealed by default. ``` The *_open_* keyword must be used when defining a class that is *_not sealed_*. ```
37
Define a *_sealed class_* "Foo".
class Foo { }
38
Define a class "Foo" that can be *_extended_*.
open class Foo { }
39
``` # Define class "Foo". Extend class "Bar". Implement (empty/marker) interfaces "Learn" and "More". ```
class Foo : Bar, Learn, More { } or class Foo : Learn, More, Bar { } or class Foo : Learn, Bar, More { } Any ordering of superclass and interface(s) is fine.
40
Can the *_scope of a protected member_* be expanded to public ?
Yes, if it is defined "open". Redefining the field will NOT replace the existing one, when it comes to object allocation. WHY DO THIS ??? ``` open class Container { protected open val fieldA: String = "Some value" } ``` ``` class DerivedContainer : Container() { public override val fieldA: String = "Something else" } ```
41
Describe an *_abstract class_*.
* specified with the *_abstract_* keyword in the class definition * abstract methods must also include the *_abstract_* keyword * cannot create an instance of an abstract class * concrete extensions of the class must implement all abstract methods * abstract class can extend a concrete class, and override parent method as abstract, if the parent method is *_open_*. Parent method will no longer be accessible
42
When should you create *_an interface versus an abstract class_* ?
* *_is-a_* versus *_can-do_* * versioning - if you add a method to an interface, all classes that implement that interface must be updated to implement the new interface * interfaces cannot initialize state * interfaces do not have a constructor or factory * subclasses can only extend one superclass/abstract
43
Explain *_late-binding polymorphism_*.
Also known as dynamic binding or runtime binding. Base classes define and implement virtual methods. Subclasses can override virtual methods that are *_open_*. When a virtual method is called, the type of the *_current receiver_* is determined at runtime and the implementation of that type is executed.
44
I there a runtime performance penalty for creating deep inheritance hierarchies ?
Yes. Virtual method calls are expensive, because they have to search through the entire class hierarchy at runtime to determine which implementation to execute.
45
What are the *_overriding rules_* ?
* both methods and properties (val) can be overridden * by default, methods and properties are closed and cannot be overridden * a method or property definition must include *_open_* to make it available to be overridden in a subclass * the *_override_* keyword must be used when overriding a method or property in a subclass * you can override a val with a var, but not the other way around * if a superclass and an interface implement the same method signature, the subclass must implement the method. The method may call the others: super.method()
super.method()
46
What are the _2 types of composition_ ?
* aggregation - "assocated-with" The object lifecycle is independent from the container lifecycle. Container can be destroyed, but the aggregated objects can live on. * composition - "part-of" The object lifecycle is tied to the container lifecycle. If the container is destroyed, so are the composed objects. These objects are constructed by the container and are frequently private.
47
Why should you *_favor composition over inheritance_* ?
* deep class hierarchies are less efficient at runtime * single responsibility * simplicity of design * reduce type coupling * greater flexibility * easy to delegate to a different type
48
What is a *_sealed class_* ?
* defined with the *_sealed_* keyword * abstract class * can be extended by subclassed nested in the sealed class * a powerful enumeration option that supports multiple instances of each subclass * powerful when used in a collection with a *_when_* statement
49
50
Given the following inheritance, how would you access a method implementation foo()" in class "A" from class "C", if it has been overridden in class "B" ? A \<-- B \<-- C
super.foo()