Classes Workbook (Part 1)
Practice problems for A Kotlin Class Is Mostly Its Header. Each takes a minute or two. Write your own answer first, then click Show answer — nothing here is a trick question, just direct practice of the syntax from the lesson.
declarations and constructors
1. The smallest class
Declare an empty class Person and create an instance.
Show answer Hide answer
class Person
val p = Person()No new keyword — you call the class like a function.
2. Properties in the header
Declare Point with a read-only x and y of type Int in its primary constructor.
Show answer Hide answer
class Point(val x: Int, val y: Int) 3. A default property value
Declare User(name: String) with a var active property that defaults to true.
Show answer Hide answer
class User(val name: String, var active: Boolean = true) 4. An init block
Declare Account(balance: Int) that throws IllegalArgumentException at construction if balance is negative.
Show answer Hide answer
class Account(val balance: Int) {
init {
require(balance >= 0) { "balance must be non-negative" }
}
} 5. Construct with named arguments
Given class Server(val host: String, val port: Int = 80, val secure: Boolean = false), construct one for "example.com" that is secure, keeping the default port.
Show answer Hide answer
Server(host = "example.com", secure = true) 6. A secondary constructor
Add a secondary constructor to Point that takes a single Int and uses it for both x and y.
Show answer Hide answer
class Point(val x: Int, val y: Int) {
constructor(both: Int) : this(both, both)
} properties and encapsulation
7. A computed property
Add a read-only area property to Rectangle(val w: Int, val h: Int) computed from its sides.
Show answer Hide answer
class Rectangle(val w: Int, val h: Int) {
val area: Int
get() = w * h
} 8. Read-public, write-private
Declare Counter with an Int property value that anyone can read but only the class can change, plus an increment() method.
Show answer Hide answer
class Counter {
var value: Int = 0
private set
fun increment() { value++ }
} inheritance
9. Classes are final by default
Make Animal extendable and give it an open speak() returning "..."; then have Dog override it to return "woof".
Show answer Hide answer
open class Animal {
open fun speak() = "..."
}
class Dog : Animal() {
override fun speak() = "woof"
}A class and its members must be marked open before they can be extended or overridden.
10. An abstract class
Declare an abstract Shape with an abstract area(): Double, and a Circle(val r: Double) that implements it.
Show answer Hide answer
abstract class Shape {
abstract fun area(): Double
}
class Circle(val r: Double) : Shape() {
override fun area() = Math.PI * r * r
} 11. Make it extendable
This doesn’t compile. Fix the declaration so Derived can extend Base:
class Base
class Derived : Base()
Show answer Hide answer
open class Base
class Derived : Base()A class is final by default; open is what lets it be extended.
Back to the lesson, A Kotlin Class Is Mostly Its Header, or on to the next one: class types.
Comments