Inheritance in kotlin

package com.example.my_kotlin_practice

fun main(args: Array<String>)
{
// open class Vehicle
//
// class Car : Vehicle()
//
// Here is the relationship:
//
// Term Class
// Base Class => Vehicle
// Derived Class => Car
// Parent Class => Vehicle
// Child Class => Car
// Super Class (Superclass) => Vehicle
// Sub Class (Subclass) => Car


val obj = B1()
obj.add()
obj.sub()
obj.mul()

var derived = DerivedClass()
println("The name of BaseClass is : ${derived.name}")
derived.method1()
derived.method2()
}
open class A1
{
var no1 = 10
var no2 = 20
init{
println("Constructor of class 'A1' is calling..")
}
fun add()
{
println("Addition of $no1 and $no2 is : ${no1 + no2}")
}
}
class B1 : A1()
{
init{
println("Constructor of class 'B1' is calling..")
}
fun sub()
{
println("Subtraction of $no1 and $no2 is : ${no1 - no2}")
}
fun mul()
{
println("Multiplication of $no1 and $no2 is : ${no1 * no2}")
}
}
open class BaseClass {
var name: String = "BaseClass"
fun method1() {
println("I am BaseClass")
}
}
class DerivedClass:BaseClass() {
var name2:String = "DerivedClass"
fun method2(){
println("I am DerivedClass")
}
}

OUTPUT :

Constructor of class 'A1' is calling.. Constructor of class 'B1' is calling.. Addition of 10 and 20 is : 30 Subtraction of 10 and 20 is : -10 Multiplication of 10 and 20 is : 200 The name of BaseClass is : BaseClass I am BaseClass I am DerivedClass Process finished with exit code 0

Comments

Popular posts from this blog

Maps (Collections) in kotlin

Functions in Kotlin

Arrays in Kotlin