Chapter IV
Inheritance & Data Class
in Kotlin
Inheritance
In real life, inheritance means leaving one’s belongings to
someone else. In object- oriented computer languages like
Kotlin, the idea is similar. Given a class A, writing class B : A
indicates that we give all the assets from class A to class B.
What is this good for, beyond having some kind of renamed
copy of A?
The magic is that class B can overrule or override parts of the
assets it inherits from class A. This can be used to alter
some aspects of the class it inherits from to introduce new
behavior.
2
Inheritance
Syntax:
if A has an empty default
open class A { ... } constructor, and
class B : A() { open class A([constructor
parameters]) { ... }
[overriding
class B : A([constructor
assets]
parameters]) {
[own assets] [overriding assets]
[own assets]
} }
3
Inheritance
• In contrast to Java, every class and every method in Kotlin is
final by default.
• This means that a class cannot be extended and a method
cannot be overridden until it's explicitly declared as open
using the open keyword. This is the exact opposite of the
Java final keyword.
• Let's say we want to declare a base class Plant and subclass
Tree:
4
Inheritance
• The preceding code will not compile, because the class Plant is final by
default. Let's make it open:
Notice that we define inheritance in Kotlin simply by using the colon
character (:). There is no extends or implements keywords known from
Java
5
Example: Inheritance
open class Plant {
open var height: Int = 0
open fun grow(height: Int) {
6
Example: Inheritance
class Tree : Plant() {
override var height: Int = super.height
get() = super.height
set(value) {
field = value
}
override fun grow(height: Int) {
this.height += height
}
}
7
Constructor Inheritance
• In Kotlin, subclasses can steal properties from the
superclass’s constructor. To do so, the val or var needs to be
prepended with open:
open class A(open val a:Int) {
}
class B(override val a:Int) : A(42) {
...
}
8
Primary Constructor Inheritance
If the parent class has a primary constructor, then
the child class must call the primary constructor of
parent class.
9
Example: Primary Constructor
Inheritance
open class Person(age: Int, name: String) {
init {
println("My name is $name.")
println("My age is $age")
}
10
Example: Constructor Inheritance
class Teacher(age: Int, name: String): Person(age, name)
fun teachMobile() {
println("I teach Native Mobile App in AUB.")
}
11
Example: Constructor Inheritance
fun main(args: Array<String>) {
val t = Teacher(35, “John")
t.teachMobile()
Output:
My name is John.
} My age is 35
I teach Native Mobile App in AUB.
12
Secondary Constructor
Inheritance
If the base class does not have a primary constructor
but has a secondary constructor then the derived
class should call the secondary constructor of base
class using the super keyword.
13
Example: Secondary Constructor
Inheritance
open class Vehicle{
constructor(carryPassenger:Boolean,tyres:Int){
println(“Carry passenger:$carryPassenger”)
println(“Tyres:$tyres”)
}
}
class Truck:Vehicle{
constructor(carryPassenger:Boolean,tyres:Int,airbag:Int):super(carryPassenger,
tyres){
println(“Air Bag:$airbag”)
}
}
fun main (){
val truck = Truck(false,4,4)
}
14
Overriding Functions
To override functions of a superclass, in the subclass you have to use the
override modifier and write
open class A {
open fun function1() {
...
}
}
class B : A() {
override fun function1() {
...
}
}
15
Overriding Properties
Kotlin has a special feature not found in other object-oriented languages. Not
only is it possible to override functions, but properties can also be overridden.
For this to work,
such properties need to be marked open in the superclass, as in
open class A {
open var a:Int = 0
}
A class that inherits from this superclass can then override the property by
declaring
class B : A() {
override var a:Int = 0
}
16
Example: Overriding Properties
class Girl: Person() {
open class Person() { override var age: Int = 0
open var age: Int = 0 get() = field
get() = field
set(value) {
set(value) { field = value - 5
field = value }
} }
}
fun main(args: Array<String>) {
val girl = Girl()
girl.age = 31
println("My fake age is ${girl.age}.")
} 17
Data Classes
Classes that only contain properties and no or very few functions
most likely are data classes, the purpose of which is to provision a
bracket around a couple of properties. They thus serve as a kind of
container gathering a range of properties. Think of a Person class
which for a person gathers the name, birthday, place of birth, and
so on.
data class ClassName([constructor])
18
Example: Data Classes
class Product(var name: String, var price: Double)
// normal class
data class Product(var name: String, var price: Double)
// data class
19
Example: Data Classes
data class Point(val x:Double, val y:Double)
fun movePoint(pt:Point, dx:Double, dy:Double):Point =
Point(pt.x + dx, pt.y + dy)
// somewhere in a function ...
val pt = Point(0.0, 1.0)
val pt2 = movePoint(pt, 0.5, 0.5)
20
Exercise-Inheritance
Person
id
name
sex
Student Employee
score salary
input() input()
output() output()
21
Thank You Calligraphy Transparent PNG | PNG Mart
22