Code:
// Parent class
open class Computer(val name: String,
val brand: String) {
}
// Child class (initializes the parent class)
class Laptop(name: String,
brand: String,
val batteryLife: Double) : Computer(name, brand) {
}
open enables subclasses
here in the class signatures is the primary c'tor.
Code:
class Laptop : Computer {
val batteryLife: Double
// Calls super() to initialize the Parent class
constructor(name: String, brand: String, batteryLife: Double): super(name, brand) {
this.batteryLife = batteryLife
}
// Calls another constructor (which calls super())
constructor(name: String, brand: String): this(name, brand, 0.0) {
}
}
If the child class doesn’t have a primary constructor, then all of its secondary constructors have to initialize : the{} of the constructors.
****************************************************************
Inheritance Example with Properties and Member Functions
Code:
/**
* BankAccount (Base Class)
* @property accountNumber - Account Number (read-only)
* @property accountName - Account Name (read-only)
* @property balance - Current Balance (Mutable)
*/
open class BankAccount(val accountNumber: String, val accountName: String) {
var balance : Double = 0.0
fun depositeMoney(amount: Double): Boolean {
if(amount > 0) {
balance += amount
return true
} else {
return false
}
}
fun withdrawMoney(amount: Double): Boolean {
if(amount > balance) {
return false
} else {
balance -= amount
return true
}
}
}
its subclass :
Code:
/**
* SavingsAccount (Derived Class)
* @property interestRate - Interest Rate for SavingsAccount (read-only)
* @constructor - Primary constructor for creating a Savings Account
* @param accountNumber - Account Number (used to initialize BankAccount)
* @param accountName - Account Name (used to initialize BankAccount)
*/
class SavingsAccount (accountNumber: String, accountName: String, val interestRate: Double) :
BankAccount(accountNumber, accountName) {
fun depositInterest() {
val interest = balance * interestRate / 100
this.depositeMoney(interest);
}
}
Properties - accountNumber, accountName, balance
Methods - depositMoney, withdrawMoney
using it in a main function
Code:
fun main(args: Array<String>) {
// Create a Savings Account with 6% interest rate
val savingsAccount = SavingsAccount("64524627", "Rajeev Kumar Singh", 6.0)
savingsAccount.depositeMoney(1000.0)
savingsAccount.depositInterest()
println("Current Balance = ${savingsAccount.balance}")
}
*********************************************************************
Overriding Member Functions :
Code:
open class Teacher {
// Must use "open" modifier to allow child classes to override it
open fun teach() {
println("Teaching...")
}
}
class MathsTeacher : Teacher() {
// Must use "override" modifier to override a base class function
override fun teach() {
println("Teaching Maths...")
}
}
main :
Code:
fun main(args: Array<String>) {
val teacher = Teacher()
val mathsTeacher = MathsTeacher()
teacher.teach() // Teaching...
mathsTeacher.teach() // Teaching Maths..
}
********************************************
Overriding Properties
Just like functions, you can override the properties of a super class as well. To allow child classes to override a property of a parent class, you must annotate it with the open modifier.
Moreover, The child class must use override keyword for overriding a property of a parent class
Code:
open class Employee {
// Use "open" modifier to allow child classes to override this property
open val baseSalary: Double = 30000.0
}
class Programmer : Employee() {
// Use "override" modifier to override the property of base class
override val baseSalary: Double = 50000.0
}
fun main(args: Array<String>) {
val employee = Employee()
println(employee.baseSalary) // 30000.0
val programmer = Programmer()
println(programmer.baseSalary) // 50000.0
}
*********************************
override using setter :
Code:
open class Person {
open var age: Int = 1
}
class CheckedPerson: Person() {
override var age: Int = 1
set(value) {
field = if(value > 0) value else throw IllegalArgumentException("Age can not be negative")
}
}
fun main(args: Array<String>) {
val person = Person()
person.age = -5 // Works
val checkedPerson = CheckedPerson()
checkedPerson.age = -5 // Throws IllegalArgumentException : Age can not be negative
}
Calling properties and functions of Super class
Code:
open class Employee {
open val baseSalary: Double = 10000.0
open fun displayDetails() {
println("I am an Employee")
}
}
class Developer: Employee() {
override var baseSalary: Double = super.baseSalary + 10000.0
override fun displayDetails() {
super.displayDetails()
println("I am a Developer")
}
}
:FP: