Posts

Nested class in kotlin (inner class in kotlin)

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val InObj = OuterClass().InnerClass() InObj.innerFunction() } class OuterClass { private var name = "OuterClass" inner class InnerClass { fun innerFunction () { println ( "Inside inner class : $ name " ) } } } OUTPUT : Inside inner class : OuterClass Process finished with exit code 0

Encapsulation in kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val instance_a:A2 = A2() instance_a. a } var a = 0 private fun abc () { println ( "This is 'abc' function" ) } open class A2 { var a = 10 private var b = 12 protected fun xyz () {} } internal class B2: A2() { fun abc () { super .xyz() } } Above file is Encapsulation.kt file Below file is Encapsulation2.kt file package com.example.my_kotlin_practice fun main (args: Array<String>) { val b2 = B2() } OUTPUT : Process finished with exit code 0

Abstraction using interfaces in kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val array = arrayOf (Rectangles2(),Circles2(),Squares2(),Picture()) for (shape in array) { shape.name() shape.xyz() } } //In kotlin, we can use multiple inheritance functionality with the help of interface abstract class Shapes2: Name,Xyz { abstract val i :Int abstract fun draw () fun type () {} } interface Name { fun name () //By default, functions inside interface are "abstract" // fun xyz() {} } interface Xyz { fun xyz () } class Rectangles2:Shapes2() { override val i = 10 override fun draw () { println ( "Drawing a Rectangle" ) } override fun name () { println ( "Name is Rectangle" ) } override fun xyz () { println ( "Xyz is inside Rectangles2" ) } } class Circles2:Shapes2() { override val i = 20 override fun draw () { println ( "Drawing a C...

Abstraction in kotlin (abstract class and abstract methods in kotlin)

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val array = arrayOf (Rectangles(),Circles(),Squares()) for (shape in array) { shape.draw() shape.name() } } abstract class Shapes { abstract val i : Int abstract fun draw () abstract fun name () fun type () { println ( "This is non-abstract method" ) } } class Rectangles:Shapes() { override val i :Int = 10 override fun draw (){ println ( "Drawing a Rectangle" ) } override fun name () { println ( "Name is Rectangle" ) } } class Circles:Shapes() { override val i :Int = 20 override fun draw () { println ( "Drawing a Circle" ) } override fun name () { println ( "Name is Circle" ) } } class Squares:Shapes() { override val i :Int = 40 override fun draw () { println ( "Drawing a Square" ) } override fun na...

Polymorphism in kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val rect:Shape = Rectangle() rect.draw() val array = arrayOf (Rectangle(),Circle(),Square(),Triangle()) for (shape in array){ shape.draw() } } open class Shape { open fun draw () { println ( "Drawing a shape" ) } } class Rectangle:Shape() { override fun draw () { println ( "Drawing a rectangle" ) } } class Circle:Shape() { override fun draw () { println ( "Drawing a circle" ) } } class Square:Shape() { override fun draw () { println ( "Drawing a square" ) } } class Triangle:Shape() { override fun draw () { println ( "Drawing a triangle" ) } } OUTPUT : Drawing a rectangle Drawing a rectangle Drawing a circle Drawing a square Drawing a triangle Process finished with exit code 0

Inheritance with "super" and "override" keywords in kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val car = Car( "Yellow" ) println (car. name ) car.drive() } open class Vehicle( val color : String) { init { println ( "Vehicle class created" ) } open var name :String = "Vehicle" open fun drive () { println ( "Vehicle is driving" ) } } class Car( val carColor : String):Vehicle(carColor) { init { println ( "Car class created" ) } override var name :String = "Car" override fun drive () { super .drive() println ( "Car is driving" ) } } OUTPUT : Vehicle class created Car class created Car Vehicle is driving Car is driving Process finished with exit code 0

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()...