PK - Chapter 3 Flashcards
Declare a class named Foo.
class Foo {
}
What is the default access level of a class ?
Public
Explain class constructors.
- 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
Create a primary constructor for class Foo, with data validation.
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.
Explain initializer blocks.
* 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
Explain primary constructors.
- 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 } }
Explain secondary constructors.
- 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
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.
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.
What is the recommended visibility
for the constructors or an abstract class ?
protected
They would only be visible to derived classes.
# Define a *_primary constructor_* for class Foo that can *_only be instantiated inside the package_*.
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.
Summarize visibility levels.
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
What is the access to a private nested class ?
Can create/access an instance from the outer class.
What is the access to an internal nested class ?
Can be created/accessed from anywhere in the module.
What is the access to an protected nested class ?
Can be created/accessed from any class that is derived from the containing class.
What is the difference between static and non-static nested classes ?
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 would you access the “memberOne” variable
in outer class “Alpha”
from inner class “Bravo” ?
this@Alpha.memberOne
Since we are referencing it from an inner class,
the visibility of memberOne does not matter.
How would you access the “memberOne” variable
in outer class “Alpha”
from nested static class “Bravo” ?
this@Alpha.memberOne
The nested static class can only access the member variable if it is public.
Add a MouseListener to a button,
using an anonymous inner class.
button.addMouseListener(adapter : MouseAdapter() {
override fun mouseClicked(event: MouseEvent) { this@OuterClass.doSomething() }
})
# Define a data class named "Person" with attributes "firstName", "lastName".
data class Person(val firstName: String, val lastName: String)
# Define an enum named "DayOfWeek" with attribute "properCaseName".
enum class DayOfWeek(val properCaseName: String) {
MONDAY(“Monday”),
TUESDAY(“Tuesday”),
WEDNESDAY(“Wednesday”),
THURSDAY(“Thursday”),
FRIDAY(“Friday”),
SATURDAY(“Saturday”),
SUNDAY(“Sunday”)
}