├── .DS_Store ├── 1 Fundamentals ├── .DS_Store ├── 1 Hello World │ └── HelloWorld.kt ├── 2 Variables │ └── Variables.kt ├── 3 NullSafety │ └── NullSafety.kt ├── 4 If-Else │ └── ifelse.kt ├── 5 When │ ├── .DS_Store │ └── When.kt ├── 6 Array │ └── Array.kt ├── 7 Loops │ └── Loops.kt ├── 8 Functions │ └── Functions.kt └── 9 Higher Order Functions and Lambdas │ └── HigherOrder.kt └── 2 ObjectOrientedProgramming ├── .DS_Store ├── 1 Classes and Objects ├── Box.kt └── Main.kt ├── 10 Property Delegation └── Main.kt ├── 11 Standard Delegates └── Main.kt ├── 12 Singleton └── Main.kt ├── 13 Generics └── Main.kt ├── 14 Nested Classes └── Main.kt ├── 15 Data Class ├── Main.kt └── Person.java ├── 16 Enums └── Main.kt ├── 17 Sealed Classes └── Main.kt ├── 18 Scope Functions └── Main.kt ├── 19 Collections └── Main.kt ├── 2 Getters and Setters ├── Box.kt └── Main.kt ├── 20 Exception Handling └── Main.kt ├── 3 Visibility Modifiers ├── Box.kt └── Main.kt ├── 4 Constructors ├── Box.kt └── Main.kt ├── 5 Inheritance ├── BaseCoffeeMachine.kt ├── Main.kt └── PremiumCoffeeMachine.kt ├── 6 Abstract Class ├── BaseCoffeeMachine.kt ├── CoffeeMachine.kt ├── Main.kt └── PremiumCoffeeMachine.kt ├── 7 Interface ├── Audio.kt ├── Downloader.kt ├── Main.kt ├── Player.kt └── Video.kt ├── 8 Object Expression ├── DownloadListener.kt ├── Downloader.kt └── Main.kt └── 9 Delegation ├── Downloader.kt ├── Main.kt └── Player.kt /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/probelalkhan/kotlin-android-tutorial-for-beginners/bafd67dcd52eb5cc0433f6e1f034726fada7f561/.DS_Store -------------------------------------------------------------------------------- /1 Fundamentals/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/probelalkhan/kotlin-android-tutorial-for-beginners/bafd67dcd52eb5cc0433f6e1f034726fada7f561/1 Fundamentals/.DS_Store -------------------------------------------------------------------------------- /1 Fundamentals/1 Hello World/HelloWorld.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | println("Hello World") 3 | } -------------------------------------------------------------------------------- /1 Fundamentals/2 Variables/Variables.kt: -------------------------------------------------------------------------------- 1 | val firstName : String = "Belal" 2 | val lastname : String = "Khan" 3 | 4 | fun main(){ 5 | println("Hello $firstName $lastname") 6 | } -------------------------------------------------------------------------------- /1 Fundamentals/3 NullSafety/NullSafety.kt: -------------------------------------------------------------------------------- 1 | var firstName : String? = null 2 | val lastname : String = "Khan" 3 | 4 | fun main(){ 5 | firstName = " Belal " 6 | println("Hello ${firstName?.length}") 7 | } -------------------------------------------------------------------------------- /1 Fundamentals/4 If-Else/ifelse.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | val age = 19 3 | if(age >= 18){ 4 | print("Welcome friend...") 5 | }else{ 6 | print("You are not allowed to see this...") 7 | } 8 | } -------------------------------------------------------------------------------- /1 Fundamentals/5 When/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/probelalkhan/kotlin-android-tutorial-for-beginners/bafd67dcd52eb5cc0433f6e1f034726fada7f561/1 Fundamentals/5 When/.DS_Store -------------------------------------------------------------------------------- /1 Fundamentals/5 When/When.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | val userType = "" 3 | 4 | when(userType){ 5 | "admin" -> { 6 | println("Hey you are admin") 7 | } 8 | "editor" -> { 9 | println("Hey you are editor") 10 | } 11 | "author" -> { 12 | println("Hey you are author") 13 | } 14 | else -> { 15 | println("Hey you are subscriber") 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /1 Fundamentals/6 Array/Array.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val a: Array = arrayOf(2, 3, 4, 5) 3 | val b: IntArray = intArrayOf(2, 3, 4, 5) 4 | 5 | val x = Array(5){ 4 } 6 | 7 | println(a[2]) 8 | println(x[3]) 9 | println(x[2]) 10 | } -------------------------------------------------------------------------------- /1 Fundamentals/7 Loops/Loops.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val a: IntArray = intArrayOf(2, 3, 4, 5) 3 | var i = 1 4 | 5 | for(x in 1..10){ 6 | print("$x ") 7 | } 8 | 9 | for(y in a.indices){ 10 | println(a[y]) 11 | } 12 | 13 | 14 | // while(i <= 10){ 15 | // print("$i ") 16 | // i+=2 17 | // } 18 | 19 | // do { 20 | // print("$i ") 21 | // i += 2 22 | // } while (i <= 10) 23 | 24 | } -------------------------------------------------------------------------------- /1 Fundamentals/8 Functions/Functions.kt: -------------------------------------------------------------------------------- 1 | fun rollDice( 2 | times: Int = 1, 3 | range: IntRange = (1..6) 4 | ) { 5 | for (i in 0 until times) { 6 | val result = range.random() 7 | println(result) 8 | } 9 | } 10 | 11 | fun getSum(vararg nums: Int) = nums.sum() 12 | 13 | fun makeBurger( 14 | size: String, 15 | extraCheese: Boolean, 16 | makeItAMeal: Boolean, 17 | type: String 18 | ){ 19 | println("Size: $size") 20 | println("Extra Cheese: $extraCheese") 21 | println("Make it a Meal: $makeItAMeal") 22 | println("type: $type") 23 | } 24 | 25 | fun main() { 26 | // rollDice() 27 | // getSum(5, 5, 10, 20) 28 | // makeBurger( 29 | // type = "Chicken", 30 | // size = "XL", 31 | // makeItAMeal = true, 32 | // extraCheese = true 33 | // ) 34 | 35 | val sum = getSum(5, 5, 5) 36 | println(sum) 37 | } -------------------------------------------------------------------------------- /1 Fundamentals/9 Higher Order Functions and Lambdas/HigherOrder.kt: -------------------------------------------------------------------------------- 1 | import kotlin.concurrent.thread 2 | 3 | fun rollDice( 4 | range: IntRange, 5 | time: Int, 6 | callback: (result: Int) -> Unit 7 | ) { 8 | for (i in 0 until time) { 9 | val result = range.random() 10 | callback(result) 11 | } 12 | } 13 | 14 | fun rollDice( 15 | callback: ((result: Int) -> Unit)? = null 16 | ): String { 17 | 18 | thread { 19 | Thread.sleep(3000) 20 | callback?.invoke(4) 21 | } 22 | 23 | return "Dice Rolled" 24 | } 25 | 26 | fun main() { 27 | val result = rollDice{ 28 | println(it) 29 | } 30 | 31 | println(result) 32 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/probelalkhan/kotlin-android-tutorial-for-beginners/bafd67dcd52eb5cc0433f6e1f034726fada7f561/2 ObjectOrientedProgramming/.DS_Store -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/1 Classes and Objects/Box.kt: -------------------------------------------------------------------------------- 1 | class Box { 2 | 3 | var length: Int = 10 4 | var width: Int = 20 5 | var height: Int = 5 6 | 7 | fun fillContents(){ 8 | println("Box is Filled") 9 | } 10 | 11 | fun open(){ 12 | println("Box Opened") 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/1 Classes and Objects/Main.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | val box1 = Box() 3 | val box2 = Box() 4 | val box3 = Box() 5 | 6 | box1.height = 7 7 | println("Height: ${box1.height}") 8 | println("Length: ${box1.length}") 9 | println("Width: ${box1.width}") 10 | 11 | box1.open() 12 | box1.fillContents() 13 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/10 Property Delegation/Main.kt: -------------------------------------------------------------------------------- 1 | import kotlin.reflect.KProperty 2 | 3 | class Student { 4 | var firstName: String? by NameDelegate() 5 | var lastName: String? by NameDelegate() 6 | 7 | override fun toString(): String { 8 | return "$firstName $lastName" 9 | } 10 | } 11 | 12 | class NameDelegate { 13 | var formattedValue: String? = null 14 | 15 | operator fun setValue( 16 | thisRef: Any?, 17 | property: KProperty<*>, 18 | value: String? 19 | ) { 20 | if (value != null && value.length > 5) { 21 | formattedValue = value.trim().toUpperCase() 22 | } 23 | } 24 | 25 | operator fun getValue(thisRef: Any?, property: KProperty<*>): String? { 26 | return formattedValue 27 | } 28 | } 29 | 30 | fun main() { 31 | val student = Student() 32 | student.firstName = "be" 33 | student.lastName = " " 34 | println(student) 35 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/11 Standard Delegates/Main.kt: -------------------------------------------------------------------------------- 1 | import kotlin.properties.Delegates 2 | 3 | class StudentHeavy { 4 | init { 5 | println("Student Heavy Initialized") 6 | } 7 | } 8 | 9 | class Student { 10 | val heavy by lazy { StudentHeavy() } 11 | 12 | var marks: Int by Delegates.observable(50) { property, oldValue, newValue -> 13 | println("Old Value $oldValue") 14 | println("New Value $newValue") 15 | } 16 | 17 | var age: Int by Delegates.vetoable(14){ property, oldValue, newValue -> 18 | println("New Age $newValue") 19 | println("Old Age $oldValue") 20 | newValue >= 14 21 | } 22 | } 23 | 24 | fun main() { 25 | val student = Student() 26 | // student.marks = 70 27 | // student.marks = 80 28 | 29 | student.age = 13 30 | println(student.age) 31 | student.age = 12 32 | println(student.age) 33 | student.age = 15 34 | println(student.age) 35 | 36 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/12 Singleton/Main.kt: -------------------------------------------------------------------------------- 1 | object Manager { 2 | init { 3 | println("Manager Object Initialized") 4 | } 5 | } 6 | 7 | //Singleton the old way 8 | class Manager1 private constructor() { 9 | companion object { 10 | private var instance: Manager1? = null 11 | operator fun invoke(): Manager1 = synchronized(this) { 12 | if (instance == null) 13 | instance = Manager1() 14 | return instance!! 15 | } 16 | } 17 | } 18 | 19 | fun main() { 20 | println(Manager) 21 | println(Manager) 22 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/13 Generics/Main.kt: -------------------------------------------------------------------------------- 1 | //class ArrayUtil(private val array: Array){ 2 | // fun findElement(element: T, foundElement: (index: Int, element: T?) -> Unit){ 3 | // for(i in array.indices){ 4 | // if(array[i] == element){ 5 | // foundElement(i, array[i]) 6 | // return 7 | // } 8 | // } 9 | // foundElement(-1, null) 10 | // return 11 | // } 12 | //} 13 | 14 | fun findElement(array: Array, element: T, foundElement: (index: Int, element: T?) -> Unit){ 15 | for(i in array.indices){ 16 | if(array[i] == element){ 17 | foundElement(i, array[i]) 18 | return 19 | } 20 | } 21 | foundElement(-1, null) 22 | return 23 | } 24 | 25 | fun justForTesting(param1: X, param2: Y){ 26 | println("$param1 and $param2") 27 | } 28 | 29 | fun main() { 30 | 31 | justForTesting("3", 4) 32 | 33 | 34 | findElement(arrayOf(1,2,3,4,5), 3) {index, element -> 35 | println("Index $index") 36 | println("Element $element") 37 | } 38 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/14 Nested Classes/Main.kt: -------------------------------------------------------------------------------- 1 | class Box(val width: Int, val length: Int, val height: Int) { 2 | 3 | inner class Content(val content: String){ 4 | 5 | fun printBoxInfo(){ 6 | println("$width, $length, $height") 7 | } 8 | 9 | fun printContent(){ 10 | println(content) 11 | } 12 | 13 | } 14 | 15 | } 16 | 17 | fun main() { 18 | val box = Box(10,10,10) 19 | val content = box.Content("Some Content") 20 | content.printContent() 21 | content.printBoxInfo() 22 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/15 Data Class/Main.kt: -------------------------------------------------------------------------------- 1 | open class Address{ 2 | public val test = "Hello" 3 | } 4 | data class Person( 5 | val id: String, 6 | val fname: String, 7 | val lname: String, 8 | val country: String 9 | ) : Address(){ 10 | fun getName() = "$fname $lname" 11 | } 12 | 13 | fun main() { 14 | val person = Person("1", "Belal", "Khan", "India") 15 | val person1 = Person("1", "Belal", "Khan", "India") 16 | println(person == person1) 17 | println(person.getName()) 18 | println(person.test) 19 | //componentN 20 | // println(person.component1()) 21 | // println(person.component2()) 22 | // println(person.component3()) 23 | // println(person.component4()) 24 | 25 | val (id, fname, lname, country ) = person //destructuring delcaration 26 | 27 | println(id) 28 | println(fname) 29 | println(lname) 30 | println(country) 31 | 32 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/15 Data Class/Person.java: -------------------------------------------------------------------------------- 1 | //import java.util.Objects; 2 | // 3 | //public class Person { 4 | // 5 | // private String id; 6 | // private String name; 7 | // private String country; 8 | // 9 | // public Person(String id, String name, String country) { 10 | // this.id = id; 11 | // this.name = name; 12 | // this.country = country; 13 | // } 14 | // 15 | // public String getId() { 16 | // return id; 17 | // } 18 | // 19 | // public String getName() { 20 | // return name; 21 | // } 22 | // 23 | // public String getCountry() { 24 | // return country; 25 | // } 26 | // 27 | // @Override 28 | // public String toString() { 29 | // return "Person{" + 30 | // "id='" + id + '\'' + 31 | // ", name='" + name + '\'' + 32 | // ", country='" + country + '\'' + 33 | // '}'; 34 | // } 35 | // 36 | // @Override 37 | // public boolean equals(Object o) { 38 | // if (this == o) return true; 39 | // if (o == null || getClass() != o.getClass()) return false; 40 | // Person person = (Person) o; 41 | // return Objects.equals(id, person.id) && 42 | // Objects.equals(name, person.name) && 43 | // Objects.equals(country, person.country); 44 | // } 45 | // 46 | // @Override 47 | // public int hashCode() { 48 | // return Objects.hash(id, name, country); 49 | // } 50 | //} 51 | -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/16 Enums/Main.kt: -------------------------------------------------------------------------------- 1 | interface DoColor{ 2 | fun doColor() 3 | } 4 | 5 | enum class Color(val colorName: String, val colorValue: Int) : DoColor { 6 | RED("Red", 10) { 7 | override fun doColor() { 8 | println("Colored with Red") 9 | } 10 | }, 11 | GREEN("Green", 20){ 12 | override fun doColor() { 13 | println("Colored with Green") 14 | } 15 | }, 16 | BLUE("Blue", 30){ 17 | override fun doColor() { 18 | println("Colored with Blue") 19 | } 20 | } 21 | } 22 | 23 | fun main() { 24 | println(Color.RED.colorName) 25 | println(Color.RED.colorValue) 26 | Color.GREEN.doColor() 27 | // Color.values().forEach { println(it) } 28 | 29 | 30 | // val color = Color.RED 31 | // when (color) { 32 | // Color.RED -> println("Red") 33 | // Color.GREEN -> println("Green") 34 | // Color.BLUE -> println("Blue") 35 | // } 36 | // 37 | 38 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/17 Sealed Classes/Main.kt: -------------------------------------------------------------------------------- 1 | sealed class Data { 2 | data class Success(val data: String) : Data() 3 | data class Error(val error: String) : Data() 4 | object Loading : Data() 5 | } 6 | 7 | fun getData(): Data { 8 | return Data.Error((100..1000).random().toString()) 9 | } 10 | 11 | 12 | fun main() { 13 | val data = getData() 14 | when (data) { 15 | is Data.Success -> { 16 | println(data) 17 | } 18 | is Data.Error -> { 19 | println(data) 20 | } 21 | is Data.Loading -> { 22 | println(data) 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/18 Scope Functions/Main.kt: -------------------------------------------------------------------------------- 1 | class Square( 2 | private val width: Int, 3 | private val height: Int, 4 | private var color: String? = null, 5 | private var text: String? = null 6 | ) { 7 | fun fillColor(color: String) { 8 | this.color = color 9 | println("$color Color Filled") 10 | } 11 | 12 | fun addText(text: String) { 13 | this.text = text 14 | println("\"$text\" text added") 15 | } 16 | 17 | override fun toString(): String { 18 | return "Square(width=$width, height=$height, color=$color text=$text)" 19 | } 20 | } 21 | 22 | /* 23 | Kotlin Scope Functions 24 | +----------+----------------+----------------+ 25 | | Function | Context Object | Return Value | 26 | +----------+----------------+----------------+ 27 | | let | it | lambda result | 28 | | run | this | lambda result | 29 | | with | this | lambda result | 30 | | apply | this | context object | 31 | | also | it | context object | 32 | +----------+----------------+----------------+ 33 | */ 34 | 35 | lateinit var square: Square 36 | 37 | fun main() { 38 | val square = Square(10, 20).also { 39 | it.fillColor("Black") 40 | it.addText("Black Text") 41 | } 42 | println(square) 43 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/19 Collections/Main.kt: -------------------------------------------------------------------------------- 1 | /* * 2 | +------+--------------+---------------------+ 3 | | | Immutable | Mutable | 4 | +------+--------------+---------------------+ 5 | | List | listOf() | mutableListOf() | 6 | | Set | setOf() | mutableSetOf() | 7 | | Map | mapOf() | mutableMapOf() | 8 | +------+--------------+---------------------+ 9 | * */ 10 | 11 | fun main() { 12 | val students = mutableMapOf(1 to "Belal", 2 to "Ramiz", 3 to "Sunny") 13 | students.remove(1) 14 | students[7] = "new value" 15 | students.forEach { t, u -> 16 | println("$t and $u") 17 | } 18 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/2 Getters and Setters/Box.kt: -------------------------------------------------------------------------------- 1 | class Box { 2 | 3 | var length: Int = 10 4 | var width: Int = 20 5 | var height: Int = 5 6 | 7 | val volume 8 | get() = length * width * height 9 | 10 | var boxName: String = "Box Name" 11 | set(value) { 12 | if(value.length < 3){ 13 | println("Name cannot be less than 3 characters") 14 | }else{ 15 | field = value 16 | } 17 | } 18 | 19 | fun fillContents() { 20 | println("Box is Filled") 21 | } 22 | 23 | fun open() { 24 | println("Box Opened") 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/2 Getters and Setters/Main.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | val box1 = Box() 3 | val box2 = Box() 4 | val box3 = Box() 5 | 6 | box1.height = 7 7 | println("Height: ${box1.height}") 8 | println("Length: ${box1.length}") 9 | println("Width: ${box1.width}") 10 | println("Volume: ${box1.volume}") 11 | 12 | box1.boxName = "My" 13 | println("Name: ${box1.boxName}") 14 | 15 | box1.open() 16 | box1.fillContents() 17 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/20 Exception Handling/Main.kt: -------------------------------------------------------------------------------- 1 | import java.lang.ArithmeticException 2 | import java.lang.IllegalArgumentException 3 | 4 | /* 5 | * Throwable 6 | * Exception 7 | * all other exceptions that are predefined 8 | * */ 9 | 10 | class IllegalVoterException(message: String) : Exception(message) 11 | 12 | fun vote(name: String, age: Int){ 13 | if(age < 18) 14 | throw IllegalVoterException("Younger than 18 cannot vote") 15 | println("$name voted") 16 | } 17 | 18 | 19 | fun main(){ 20 | 21 | try { 22 | vote("Belal", 17) 23 | }catch(e: Exception){ 24 | e.printStackTrace() 25 | }catch(e: IllegalVoterException){ 26 | e.printStackTrace() 27 | }finally { 28 | println("finally block") 29 | } 30 | 31 | val a = 10 32 | val b = 0 33 | 34 | val result = try { 35 | println("Inside Try Block") 36 | a / b 37 | } catch (e: ArithmeticException){ 38 | println("Inside Catch Block") 39 | 0 40 | } 41 | println("Result is $result") 42 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/3 Visibility Modifiers/Box.kt: -------------------------------------------------------------------------------- 1 | class Box { 2 | 3 | /* 4 | * public: Default, can be accessed everywhere 5 | * internal: Available everywhere in the same module 6 | * private: Available only inside the containing file or class 7 | * protected: Same as private but available inside subclasses or child classes 8 | * */ 9 | 10 | var length: Int = 10 11 | var width: Int = 20 12 | var height: Int = 5 13 | 14 | val volume 15 | get() = length * width * height 16 | 17 | var boxName: String = "Box Name" 18 | private set(value) { 19 | if(value.length < 3){ 20 | println("Name cannot be less than 3 characters") 21 | }else{ 22 | field = value 23 | } 24 | } 25 | 26 | fun fillContents() { 27 | println("Box is Filled") 28 | } 29 | 30 | fun open() { 31 | println("Box Opened") 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/3 Visibility Modifiers/Main.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | val box1 = Box() 3 | val box2 = Box() 4 | val box3 = Box() 5 | 6 | box1.height = 7 7 | println("Height: ${box1.height}") 8 | println("Length: ${box1.length}") 9 | println("Width: ${box1.width}") 10 | println("Volume: ${box1.volume}") 11 | 12 | box1.boxName = "My" 13 | println("Name: ${box1.boxName}") 14 | 15 | box1.open() 16 | box1.fillContents() 17 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/4 Constructors/Box.kt: -------------------------------------------------------------------------------- 1 | class Box( 2 | var length: Int = 0, 3 | var width: Int = 0, 4 | var height: Int = 0 5 | ) { 6 | 7 | init { 8 | println("Init Block Called") 9 | } 10 | 11 | constructor() : this(0,0,0){ 12 | println("Secondary constructor called") 13 | } 14 | 15 | val volume 16 | get() = length * width * height 17 | 18 | fun fillContents() { 19 | println("Box is Filled") 20 | } 21 | 22 | fun open() { 23 | println("Box Opened") 24 | } 25 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/4 Constructors/Main.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val box1 = Box() 3 | val box2 = Box( 4 | length = 10, 5 | width = 10, 6 | height = 10 7 | ) 8 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/5 Inheritance/BaseCoffeeMachine.kt: -------------------------------------------------------------------------------- 1 | open class BaseCoffeeMachine( 2 | private val price: Double, 3 | private val color: String 4 | ) { 5 | 6 | fun makeCoffee(){ 7 | println("Here is your coffee ☕️") 8 | } 9 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/5 Inheritance/Main.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | val coffeeMachine = PremiumCoffeeMachine(10000.0, "BROWN") 3 | coffeeMachine.makeCappuccino() 4 | coffeeMachine.makeCoffee() 5 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/5 Inheritance/PremiumCoffeeMachine.kt: -------------------------------------------------------------------------------- 1 | 2 | class PremiumCoffeeMachine( 3 | private val price: Double, 4 | private val color: String 5 | ) : BaseCoffeeMachine(price, color) { 6 | 7 | 8 | fun makeCappuccino(){ 9 | println("Here is your cappuccino ☕️") 10 | } 11 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/6 Abstract Class/BaseCoffeeMachine.kt: -------------------------------------------------------------------------------- 1 | abstract class BaseCoffeeMachine( 2 | private val price: Double, 3 | private val color: String 4 | ) { 5 | abstract val brand: String 6 | 7 | abstract fun makeCoffee(type: String): String 8 | 9 | open fun machineInfo(): String { 10 | return "Coffee Machine Details:\n" + 11 | "Price: $price\n" + 12 | "Color: $color\n" 13 | } 14 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/6 Abstract Class/CoffeeMachine.kt: -------------------------------------------------------------------------------- 1 | class CoffeeMachine ( 2 | price: Double, 3 | color: String 4 | ) : BaseCoffeeMachine(price, color){ 5 | 6 | override val brand: String 7 | get() = "Brand Y" 8 | 9 | override fun makeCoffee(type: String): String { 10 | Thread.sleep(4000) 11 | return "Your $type is Ready ☕️" 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/6 Abstract Class/Main.kt: -------------------------------------------------------------------------------- 1 | fun main(){ 2 | val coffeeMachine = PremiumCoffeeMachine(10000.0, "BROWN") 3 | val info = coffeeMachine.machineInfo() 4 | val coffee = coffeeMachine.makeCoffee("CAPPUCCINO") 5 | println(coffee) 6 | println(info) 7 | 8 | val simpleCoffeeMachine = CoffeeMachine(2000.0, "BROWN") 9 | val coffee1 = simpleCoffeeMachine.makeCoffee("CAPPUCCINO") 10 | println(coffee1) 11 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/6 Abstract Class/PremiumCoffeeMachine.kt: -------------------------------------------------------------------------------- 1 | class PremiumCoffeeMachine( 2 | price: Double, color: String 3 | ) : BaseCoffeeMachine(price, color) { 4 | 5 | override val brand: String 6 | get() = "Brand X" 7 | 8 | override fun makeCoffee(type: String): String { 9 | return "Your $type is Ready ☕️" 10 | } 11 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/7 Interface/Audio.kt: -------------------------------------------------------------------------------- 1 | class Audio(private val name: String) : Downloader, Player { 2 | 3 | override fun download() { 4 | println("Downloading Audio $name") 5 | } 6 | 7 | override fun play() { 8 | println("Playing Audio $name") 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/7 Interface/Downloader.kt: -------------------------------------------------------------------------------- 1 | interface Downloader { 2 | 3 | fun downloaderInfo(){ 4 | println("Downloader V1") 5 | } 6 | 7 | fun download() 8 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/7 Interface/Main.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val audioDownloader = Audio("Audio1.mp3") 3 | val videoDownloader = Video("Video1.mkv") 4 | 5 | audioDownloader.download() 6 | audioDownloader.play() 7 | audioDownloader.playerInfo() 8 | audioDownloader.downloaderInfo() 9 | 10 | videoDownloader.download() 11 | videoDownloader.play() 12 | videoDownloader.playerInfo() 13 | videoDownloader.downloaderInfo() 14 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/7 Interface/Player.kt: -------------------------------------------------------------------------------- 1 | interface Player { 2 | 3 | fun playerInfo(){ 4 | println("Player V1") 5 | } 6 | 7 | fun play() 8 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/7 Interface/Video.kt: -------------------------------------------------------------------------------- 1 | class Video(private val name: String) : Downloader, Player { 2 | 3 | override fun download() { 4 | println("Downloading Video $name") 5 | } 6 | 7 | override fun play() { 8 | println("Playing Video $name") 9 | } 10 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/8 Object Expression/DownloadListener.kt: -------------------------------------------------------------------------------- 1 | interface DownloadListener { 2 | fun onDownloadStarted() 3 | fun onDownloadCompleted(file: String) 4 | fun onProgressUpdate(progress: Int) 5 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/8 Object Expression/Downloader.kt: -------------------------------------------------------------------------------- 1 | class Downloader { 2 | 3 | var downloadListener: DownloadListener? = null 4 | 5 | fun downloadFile(file: String){ 6 | downloadListener?.onDownloadStarted() 7 | for(i in 1..10){ 8 | Thread.sleep(600) 9 | downloadListener?.onProgressUpdate(i * 10) 10 | } 11 | downloadListener?.onDownloadCompleted(file) 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/8 Object Expression/Main.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val downloader = Downloader() 3 | downloader.downloadListener = object : DownloadListener { 4 | override fun onDownloadStarted() { 5 | println("Download Started...") 6 | } 7 | 8 | override fun onDownloadCompleted(file: String) { 9 | println("$file Downloaded.") 10 | } 11 | 12 | override fun onProgressUpdate(progress: Int) { 13 | println("$progress% Downloaded") 14 | } 15 | } 16 | downloader.downloadFile("FileA.mkv") 17 | } 18 | 19 | -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/9 Delegation/Downloader.kt: -------------------------------------------------------------------------------- 1 | interface Downloader { 2 | fun download() 3 | } -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/9 Delegation/Main.kt: -------------------------------------------------------------------------------- 1 | class FileDownloader(private val file: String) : Downloader { 2 | override fun download() { 3 | println("$file Downloaded") 4 | } 5 | } 6 | 7 | class FilePlayer(private val file: String) : Player { 8 | override fun play() { 9 | println("$file Playing") 10 | } 11 | } 12 | 13 | class MediaFile( 14 | private val downloader: Downloader, 15 | private val player: Player 16 | ) : Downloader by downloader, Player by player 17 | 18 | 19 | fun main() { 20 | val file: String = "File1.mkv" 21 | val mediaFile = MediaFile(FileDownloader(file), FilePlayer(file)) 22 | mediaFile.download() 23 | mediaFile.play() 24 | } 25 | -------------------------------------------------------------------------------- /2 ObjectOrientedProgramming/9 Delegation/Player.kt: -------------------------------------------------------------------------------- 1 | interface Player { 2 | fun play() 3 | } --------------------------------------------------------------------------------