03- OOP Kotlin Classes & Object
03- OOP Kotlin Classes & Object
OOP Kotlin
Classes & Object
Classes & Object
• Classes are a fundamental building block of OOP. In fact,
Kotlin classes are very similar to Java classes
2
Creating Object
• Unlike Java, Kotlin does not require the new
keyword to create a class instance:
Ex: val p = new Person();
4
Classes Example
This is an exact equivalent of the preceding Java class:
5
Primary Constructor
class Person constructor(name: String, age: Int) {
var name: String
var age: Int
init {
this.name = name
this.age = age
println("Person instance created")
}
}
6
Primary Constructor
class Person constructor(name: String, age: Int) {
var name: String
var age: Int
init {
this.name = name
this.age = age
println("Person instance created")
}
}
7
Primary Constructor
In Kotlin, the primary constructor, as opposed to the secondary constructor,
can't contain any code, so all initialization code must be placed inside the
initializer block (init). An initializer block will be executed during class
creation, so we can assign constructor parameters to fields inside it.
To simplify code, we can remove the initializer block and access constructor
parameters directly in property initializers. This allows us to assign
constructor parameters to a field:
class Person constructor(name: String, age: Int) {
var name: String = name
var age: Int = age
}
8
Secondary Constructor
Kotlin may have one or more secondary constructors. Secondary
constructors allow initialization of variables and allow to provide some logic
to the class as well. They are prefixed with the constructor keyword.
class Person {
var name: String
var age: Int
constructor(name: String, age: Int) {
this.name = name
this.age = age
}
}
9
Properties
Properties in Kotlin classes can be declared either as mutable, using
the var keyword, or as read-only, using the val keyword.
class Address {
var name: String = "Holmes, Sherlock"
var street: String = "Baker"
var city: String = "London"
var state: String? = null
var zip: String = "123456"
}
10
Properties
To use a property, simply refer to it by its name:
11
Read-write versus read-only property
class Person(
var name: String,
// Read-write property (generated getter and setter)
val age: Int // Read-only property (generated getter)
)
12
Read-write versus read-only property
\\usage
val person = Person("Eva", 25)
13
Getter & Setter
getters are used for getting value of the property. Similarly, setters are used
for setting value of the property.
In Kotlin, getters and setters are optional and are auto-generated if you do
not create them in your program.
// getter
get() = field
// setter
set(value) {
field = value
}
}
15
Getter & Setter
val p = Person()
p.name = "jack“
println("${p.name}")
16
Custom Setter and Getter
Custom Getter
class Rectangle(val width: Int, val height: Int) {
val area: Int
get() = width * height // Custom getter calculates area
}
fun main() {
val rectangle = Rectangle(5, 10)
println("Area: ${rectangle.area}") // Output: Area: 50
}
17
Custom Setter and Getter
Custom Setter
class Person {
var name: String = ""
set(value) {
if (value.isNotBlank()) {
field = value // Only set if the value is not blank
} else {
println("Invalid name.")
}
}
} 18
Custom Setter and Getter
Custom Setter
fun main() {
val person = Person()
person.name = "Alice" // Sets the name because it is not blank
println("Name: ${person.name}") // Output: Name: Alice
person.name = "" // Prints "Invalid name." and does not change the name
19
Late-initialized properties
class MainActivity : AppCompatActivity() {
Now, with the property marked as lateinit, we can access our application
instancewithout performing nullity checks.
20
Access Modifier in Kotlin
22
Example Access Modifier
open class Base() {
var a = 1 // public by default
private var b = 2 // private to Base class
protected open val c = 3 // visible to the Base and the Derived class
internal val d = 4 // visible inside the same module
protected fun e() { } // visible to the Base and the Derived class
}
23
class Derived: Base() {
// a, c, d, and e() of the Base class are visible
// b is not visible
25