Declare a class named Foo.
class Foo {
}
What is the default access level of a class ?
Public
Explain class constructors.
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 blocksExplain primary constructors.
class Foo private constructor(firstArg: Int, secondArg: String) {
init { ifNeeded(firstArg }
}Explain secondary constructors.
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:
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:
Inner class (non-static class):
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”)
}
What are the built-in properties of an enum instance ?
name: String
ordinal: Int
Create an interface named “Printable” and
an enum named “Word” that implements Printable.
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()
Create a class with static methods.
Kotlin doesn’t directly support static methods for a class.
Methods accessed statically must be defined
in a companion object.
What is a companion object ?
“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.