├── src ├── data │ ├── Action.kt │ ├── MinMax.kt │ ├── Login.kt │ ├── Token.kt │ ├── Product.kt │ ├── Location.kt │ ├── Laptop.kt │ ├── Television.kt │ ├── Game.kt │ ├── Operation.kt │ ├── Utilities.kt │ ├── User.kt │ ├── Gender.kt │ ├── Application.kt │ ├── Boss.kt │ ├── Fruit.kt │ ├── Student.kt │ ├── CreateProductRequest.kt │ ├── Company.kt │ ├── Teacher.kt │ ├── Base.kt │ ├── Account.kt │ ├── Animal.kt │ ├── Address.kt │ ├── Shape.kt │ ├── Car.kt │ ├── Customer.kt │ ├── Note.kt │ ├── Person.kt │ ├── Interaction.kt │ └── Employee.kt ├── data2 │ └── Application.kt ├── exception │ └── ValidationException.kt ├── annotations │ ├── NotBlank.kt │ ├── Fancy.kt │ └── Beta.kt ├── app │ ├── Annotation.kt │ ├── InlineClass.kt │ ├── Interface.kt │ ├── ThisKeyword.kt │ ├── AbstractPropertiesAndFunction.kt │ ├── LazyProperties.kt │ ├── LateInitProperties.kt │ ├── ToString.kt │ ├── FunctionOverloading.kt │ ├── AnyClass.kt │ ├── AbstractClass.kt │ ├── DataClass.kt │ ├── VisibilityModifier.kt │ ├── ObservableProperties.kt │ ├── ExtensionFunction.kt │ ├── OperatorOverloading.kt │ ├── SuperKeyword.kt │ ├── Equals.kt │ ├── Inheritance.kt │ ├── secondaryconstructor.kt │ ├── HashCode.kt │ ├── Delegation.kt │ ├── constructor.kt │ ├── Function.kt │ ├── PropertiesOverriding.kt │ ├── GetterSetter.kt │ ├── object.kt │ ├── Polymorphism.kt │ ├── SuperConstructor.kt │ ├── PropertiesConstructor.kt │ ├── InnerClass.kt │ ├── SingletonObject.kt │ ├── EnumClass.kt │ ├── TypeAlias.kt │ ├── SealedClass.kt │ ├── AnonymousClass.kt │ ├── NullSafety.kt │ ├── Exception.kt │ ├── ScopeFunction.kt │ ├── DestructuringDeclaration.kt │ ├── Reflection.kt │ └── TypeCheck.kt └── program │ └── MyApplication.kt ├── belajar-kotlin-oop.iml └── .gitignore /src/data/Action.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | interface Action { 4 | fun action() 5 | } -------------------------------------------------------------------------------- /src/data/MinMax.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | data class MinMax(val min: Int, val max:Int) -------------------------------------------------------------------------------- /src/data2/Application.kt: -------------------------------------------------------------------------------- 1 | package data2 2 | 3 | class Application(val name: String) { 4 | } -------------------------------------------------------------------------------- /src/data/Login.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | data class Login(val username: String, val password: String) -------------------------------------------------------------------------------- /src/data/Token.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | inline class Token(val value: String) { 4 | fun toUpper(): String = value.toUpperCase() 5 | } -------------------------------------------------------------------------------- /src/exception/ValidationException.kt: -------------------------------------------------------------------------------- 1 | package exception 2 | 3 | class ValidationException(message: String) : Throwable(message) { 4 | } -------------------------------------------------------------------------------- /src/data/Product.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | data class Product( 4 | val name: String, 5 | val price: Int, 6 | val cateogry: String 7 | ) -------------------------------------------------------------------------------- /src/annotations/NotBlank.kt: -------------------------------------------------------------------------------- 1 | package annotations 2 | 3 | @Target(AnnotationTarget.PROPERTY) 4 | @Retention(AnnotationRetention.RUNTIME) 5 | @MustBeDocumented 6 | annotation class NotBlank -------------------------------------------------------------------------------- /src/data/Location.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | abstract class Location(val name: String) 4 | 5 | class City(name: String) : Location(name) 6 | 7 | class Country(name: String) : Location(name) -------------------------------------------------------------------------------- /src/app/Annotation.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import program.MyApplication 4 | 5 | fun main() { 6 | val myApplication = MyApplication("Kotlin", 1) 7 | println(myApplication.info()) 8 | } -------------------------------------------------------------------------------- /src/app/InlineClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Token 4 | 5 | fun main() { 6 | val token = Token("ini token") 7 | println(token.value) 8 | println(token.toUpper()) 9 | } -------------------------------------------------------------------------------- /src/data/Laptop.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Laptop(val name: String) 4 | 5 | open class HandPhone(val name: String) 6 | 7 | class SmartPhone(name: String, val os: String) : HandPhone(name) -------------------------------------------------------------------------------- /src/data/Television.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Television { 4 | lateinit var brand: String 5 | 6 | fun initTelevision(brand: String){ 7 | this.brand = brand 8 | } 9 | } -------------------------------------------------------------------------------- /src/annotations/Fancy.kt: -------------------------------------------------------------------------------- 1 | package annotations 2 | 3 | @Target(AnnotationTarget.CLASS) 4 | @Retention(AnnotationRetention.RUNTIME) 5 | @MustBeDocumented 6 | annotation class Fancy(val author: String) -------------------------------------------------------------------------------- /src/data/Game.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | data class Game(val name:String, val price: Int) { 4 | // operator fun component1(): String = name 5 | // operator fun component2(): Int = price 6 | } -------------------------------------------------------------------------------- /src/data/Operation.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | sealed class Operation(val name: String) 4 | class Plus : Operation("Plus") 5 | class Minus : Operation("Minus") 6 | class Modulo : Operation("Modulo") -------------------------------------------------------------------------------- /src/app/Interface.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Human 4 | 5 | fun main() { 6 | val human = Human("Eko") 7 | human.sayHello("Budi") 8 | human.go() 9 | human.move() 10 | } -------------------------------------------------------------------------------- /src/app/ThisKeyword.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Person 4 | 5 | fun main() { 6 | val eko = Person() 7 | eko.firstName = "Eko" 8 | 9 | eko.sayHello("Budi", "Nugraha") 10 | 11 | } -------------------------------------------------------------------------------- /src/data/Utilities.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | object Utilities { 4 | var name: String = "My Utilities" 5 | 6 | fun toUpper(value: String): String { 7 | return value.toUpperCase() 8 | } 9 | } -------------------------------------------------------------------------------- /src/data/User.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class User(var username: String, var password: String) { 4 | 5 | override fun toString(): String { 6 | return "User with username=$username" 7 | } 8 | 9 | } -------------------------------------------------------------------------------- /src/app/AbstractPropertiesAndFunction.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Cat 4 | import data.Dog 5 | 6 | fun main() { 7 | val cat = Cat() 8 | cat.run() 9 | 10 | val dog = Dog(); 11 | dog.run() 12 | } -------------------------------------------------------------------------------- /src/app/LazyProperties.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Account 4 | 5 | fun main() { 6 | val account = Account() 7 | println(account.name) 8 | println(account.name) 9 | println(account.name) 10 | } -------------------------------------------------------------------------------- /src/data/Gender.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | enum class Gender(val description: String) { 4 | MALE("Male"), 5 | FEMALE("Female"); 6 | 7 | fun showDescription() { 8 | println(description) 9 | } 10 | } -------------------------------------------------------------------------------- /src/app/LateInitProperties.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Television 4 | 5 | fun main() { 6 | val tv = Television() 7 | tv.brand = "Samsung" 8 | tv.initTelevision("Samsung") 9 | println(tv.brand) 10 | } -------------------------------------------------------------------------------- /src/app/ToString.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.User 4 | 5 | fun main() { 6 | val user1 = User("eko", "rahasia") 7 | val user2 = User("eko", "rahasia") 8 | 9 | println(user1) 10 | println(user2) 11 | } -------------------------------------------------------------------------------- /src/app/FunctionOverloading.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Person 4 | 5 | fun main() { 6 | val eko = Person() 7 | eko.firstName = "Eko" 8 | 9 | eko.sayHello("Budi") 10 | eko.sayHello("Joko", "Nugroho") 11 | } -------------------------------------------------------------------------------- /src/app/AnyClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.SmartPhone 4 | 5 | fun main() { 6 | val smartPhone = SmartPhone("Samsung S10", "Android") 7 | 8 | println(smartPhone.toString()) 9 | println(smartPhone.hashCode()) 10 | } -------------------------------------------------------------------------------- /src/data/Application.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Application(val name: String) { 4 | 5 | companion object { 6 | fun toUpper(name: String): String { 7 | return name.toUpperCase() 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /src/program/MyApplication.kt: -------------------------------------------------------------------------------- 1 | package program 2 | 3 | import annotations.Fancy 4 | 5 | @Fancy(author = "Eko") 6 | class MyApplication(val name: String, val version: Int) { 7 | 8 | fun info(): String = "Application $name-$version" 9 | 10 | } -------------------------------------------------------------------------------- /src/app/AbstractClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.City 4 | import data.Country 5 | 6 | fun main() { 7 | val city = City("Subang") 8 | val country = Country("Subang") 9 | 10 | println(city.name) 11 | println(country.name) 12 | } -------------------------------------------------------------------------------- /src/app/DataClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Product 4 | 5 | fun main() { 6 | val product = Product("Indomie", 2500, "Food") 7 | val product2 = product.copy(name = "Supermie") 8 | 9 | println(product) 10 | println(product2) 11 | } -------------------------------------------------------------------------------- /src/app/VisibilityModifier.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.SuperTeacher 4 | import data.Teacher 5 | 6 | fun main() { 7 | val teacher = SuperTeacher("Eko") 8 | println(teacher.name) 9 | // teacher.teach() // error 10 | teacher.test() 11 | } -------------------------------------------------------------------------------- /src/app/ObservableProperties.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Account 4 | 5 | fun main() { 6 | val account = Account() 7 | 8 | account.description = "Contoh" 9 | account.description = "Diubah" 10 | account.description = "Hello World" 11 | } -------------------------------------------------------------------------------- /src/app/ExtensionFunction.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Student 4 | import data.sayHello 5 | import data.upperName 6 | 7 | fun main() { 8 | val student: Student? = Student("Eko", 15) 9 | student.sayHello("Budi") 10 | println(student?.upperName) 11 | } -------------------------------------------------------------------------------- /src/app/OperatorOverloading.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Fruit 4 | 5 | fun main() { 6 | val fruit1 = Fruit(100) 7 | val fruit2 = Fruit(100) 8 | val fruit3 = fruit1 + fruit2 9 | println(fruit3) 10 | 11 | println(fruit3 - Fruit(10)) 12 | } -------------------------------------------------------------------------------- /src/data/Boss.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Boss(val name: String) { 4 | 5 | inner class Employee(val name: String){ 6 | 7 | fun hi(){ 8 | println("Hi, my name is $name, my boss name is ${this@Boss.name}") 9 | } 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /src/app/SuperKeyword.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Rectangle 4 | 5 | fun main() { 6 | val rectangle = Rectangle() 7 | println("Corner ${rectangle.corner}") 8 | println("Parent Corner ${rectangle.parentCorner}") 9 | 10 | rectangle.printName() 11 | } -------------------------------------------------------------------------------- /src/app/Equals.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Company 4 | 5 | fun main() { 6 | val company1 = Company("Eko") 7 | val company2 = Company("Eko") 8 | 9 | println(company1 == company2) 10 | println(company1 == company1) 11 | println(company2 == company2) 12 | } -------------------------------------------------------------------------------- /src/app/Inheritance.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Manager 4 | import data.VicePresident 5 | 6 | fun main() { 7 | val manager = Manager("Eko") 8 | manager.sayHello("Budi") 9 | 10 | val vicePresident = VicePresident("Budi") 11 | vicePresident.sayHello("Joko") 12 | } -------------------------------------------------------------------------------- /src/app/secondaryconstructor.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Address 4 | 5 | fun main() { 6 | val address1 = Address("Jalan A", "Jakarta") 7 | val address2 = Address("Jalan A", "Jakarta", "Indonesia") 8 | 9 | println(address1.street) 10 | println(address2.street) 11 | } -------------------------------------------------------------------------------- /src/app/HashCode.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Company 4 | 5 | fun main() { 6 | val company1 = Company("Eko") 7 | val company2 = Company("Eko") 8 | 9 | println(company1.hashCode()) 10 | println(company2.hashCode()) 11 | println(company1.hashCode() == company2.hashCode()) 12 | } -------------------------------------------------------------------------------- /src/app/Delegation.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.MyBase 4 | import data.MyBaseDelegate 5 | 6 | fun main() { 7 | val base = MyBase() 8 | base.sayHello("Eko") 9 | 10 | val baseDelegate = MyBaseDelegate(base) 11 | baseDelegate.sayHello("Joko") 12 | baseDelegate.sayGoodbye("Budi") 13 | } -------------------------------------------------------------------------------- /src/app/constructor.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Car 4 | 5 | fun main() { 6 | val avanza = Car("Toyota") 7 | avanza.year = 2015 8 | 9 | val almaz = Car("Wuling", "Almaz") 10 | 11 | println(avanza.brand) 12 | println(avanza.year) 13 | println(almaz.brand) 14 | println(almaz.year) 15 | } -------------------------------------------------------------------------------- /src/data/Fruit.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | data class Fruit(val quantity: Int){ 4 | operator fun plus(fruit: Fruit): Fruit { 5 | return Fruit(this.quantity + fruit.quantity) 6 | } 7 | 8 | operator fun minus(fruit: Fruit): Fruit { 9 | return Fruit(this.quantity - fruit.quantity) 10 | } 11 | } -------------------------------------------------------------------------------- /src/app/Function.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Person 4 | 5 | fun main() { 6 | val eko = Person() 7 | eko.firstName = "Eko" 8 | eko.middleName = "Kurniawan" 9 | eko.lastName = "Khannedy" 10 | 11 | eko.sayHello("Budi") 12 | eko.run() 13 | 14 | val fullName = eko.getFullName() 15 | println(fullName) 16 | } -------------------------------------------------------------------------------- /src/data/Student.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Student(val name: String, val age: Int) 4 | 5 | fun Student?.sayHello(name: String) { 6 | if (this != null) { 7 | println("Hello $name, my name is ${this.name}, and my age is ${this.age}") 8 | } 9 | } 10 | 11 | val Student.upperName : String 12 | get() = this.name.toUpperCase() -------------------------------------------------------------------------------- /src/app/PropertiesOverriding.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Rectangle 4 | import data.Shape 5 | import data.Triangle 6 | 7 | fun main() { 8 | val shape = Shape() 9 | println(shape.corner) 10 | 11 | val shape2 = Rectangle() 12 | println(shape2.corner) 13 | 14 | val shape3 = Triangle() 15 | println(shape3.corner) 16 | } -------------------------------------------------------------------------------- /src/data/CreateProductRequest.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import annotations.NotBlank 4 | 5 | data class CreateProductRequest( 6 | @NotBlank val id: String, 7 | @NotBlank val name: String, 8 | @NotBlank val price: Long 9 | ) 10 | 11 | data class CreateCategoryRequest( 12 | @NotBlank val id: String, 13 | @NotBlank val name: String 14 | ) -------------------------------------------------------------------------------- /src/app/GetterSetter.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.BigNote 4 | import data.Note 5 | 6 | fun main() { 7 | val note = Note("Belajar Kotlin") 8 | 9 | println(note.title) 10 | 11 | note.title = "" 12 | println(note.title) 13 | 14 | val bigNote = BigNote("Belajar kotlin") 15 | println(bigNote.title) 16 | println(bigNote.bigTitle) 17 | } -------------------------------------------------------------------------------- /src/app/object.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Person 4 | 5 | fun main() { 6 | val eko = Person() 7 | eko.firstName = "Eko" 8 | 9 | val joko = Person() 10 | joko.firstName = "Joko" 11 | 12 | val budi = Person() 13 | budi.firstName = "Budi" 14 | 15 | println(eko.firstName) 16 | println(joko.firstName) 17 | println(budi.firstName) 18 | } -------------------------------------------------------------------------------- /src/data/Company.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Company(val name: String) { 4 | 5 | override fun hashCode(): Int { 6 | return name.hashCode() 7 | } 8 | 9 | override fun equals(other: Any?): Boolean { 10 | return when (other) { 11 | is Company -> other.name == this.name 12 | else -> false 13 | } 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /src/app/Polymorphism.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Employee 4 | import data.Manager 5 | import data.VicePresident 6 | 7 | fun main() { 8 | var employee: Employee = Employee("Eko") 9 | employee.sayHello("Budi") 10 | 11 | employee = Manager("Eko") 12 | employee.sayHello("Budi") 13 | 14 | employee = VicePresident("Eko") 15 | employee.sayHello("Budi") 16 | } -------------------------------------------------------------------------------- /src/app/SuperConstructor.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.ExecutiveCustomer 4 | import data.PremiumCustomer 5 | 6 | fun main() { 7 | val premiumCustomer = PremiumCustomer("Eko") 8 | println(premiumCustomer.name) 9 | 10 | val executiveCustomer = ExecutiveCustomer("Eko", 1000000000) 11 | println(executiveCustomer.name) 12 | println(executiveCustomer.balance) 13 | } -------------------------------------------------------------------------------- /src/data/Teacher.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | open class Teacher(val name:String) { 4 | private fun teach(){ 5 | println("Teach!") 6 | } 7 | 8 | open protected fun test(){ 9 | println("Test") 10 | } 11 | } 12 | 13 | class SuperTeacher(name:String) : Teacher(name){ 14 | 15 | override public fun test(){ 16 | super.test() 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /src/app/PropertiesConstructor.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.User 4 | 5 | fun main() { 6 | 7 | val user1 = User("eko", "rahasia") 8 | val user2 = User("joko", "rahasia123") 9 | 10 | user1.username = "budi" 11 | user1.password = "sangatrahasia" 12 | 13 | println(user1.username) 14 | println(user1.password) 15 | 16 | println(user2.username) 17 | println(user2.password) 18 | 19 | } -------------------------------------------------------------------------------- /src/data/Base.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | interface Base { 4 | fun sayHello(name: String) 5 | fun sayGoodbye(name: String) 6 | } 7 | 8 | class MyBase : Base { 9 | override fun sayHello(name: String) { 10 | println("Hello $name") 11 | } 12 | 13 | override fun sayGoodbye(name: String) { 14 | println("Good bye $name") 15 | } 16 | } 17 | 18 | class MyBaseDelegate(val base: Base) : Base by base -------------------------------------------------------------------------------- /src/data/Account.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import kotlin.properties.Delegates 4 | 5 | class Account(description: String = "") { 6 | val name: String by lazy { 7 | println("Name is called") 8 | "Eko" 9 | } 10 | 11 | var description: String by Delegates.observable(description) { property, oldValue, newValue -> 12 | println("${property.name} is changed from $oldValue to $newValue") 13 | } 14 | } -------------------------------------------------------------------------------- /src/data/Animal.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | abstract class Animal { 4 | abstract val name: String 5 | abstract fun run() 6 | } 7 | 8 | class Cat: Animal() { 9 | override val name: String = "Cat" 10 | override fun run(){ 11 | println("Cat run") 12 | } 13 | } 14 | 15 | class Dog: Animal() { 16 | override val name: String = "Dog" 17 | override fun run() { 18 | println("Dog run") 19 | } 20 | } -------------------------------------------------------------------------------- /src/annotations/Beta.kt: -------------------------------------------------------------------------------- 1 | package annotations 2 | 3 | @Target( 4 | AnnotationTarget.VALUE_PARAMETER, 5 | AnnotationTarget.PROPERTY_GETTER, 6 | AnnotationTarget.FIELD 7 | ) 8 | @Retention(AnnotationRetention.RUNTIME) 9 | @MustBeDocumented 10 | annotation class Beta() 11 | 12 | class ExampleTarget( 13 | @field:Beta val firstName: String, 14 | @get:Beta val middleName: String, 15 | @param:Beta val lastName: String 16 | ) -------------------------------------------------------------------------------- /src/app/InnerClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Boss 4 | 5 | fun main() { 6 | val boss1 = Boss("Eko") 7 | val employee1 = boss1.Employee("Budi") 8 | val employee2 = boss1.Employee("Joko") 9 | 10 | val boss2 = Boss("Rudi") 11 | val employee3 = boss2.Employee("Budi") 12 | val employee4 = boss2.Employee("Joko") 13 | 14 | employee1.hi() 15 | employee2.hi() 16 | employee3.hi() 17 | employee4.hi() 18 | } -------------------------------------------------------------------------------- /src/app/SingletonObject.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Application 4 | import data.Utilities 5 | 6 | fun main() { 7 | Utilities.name = "Dirubah" 8 | println(Utilities.toUpper("Eko")) 9 | a() 10 | b() 11 | 12 | println(Application.Companion.toUpper("Eko")) 13 | println(Application.toUpper("Eko")) 14 | } 15 | 16 | fun a(){ 17 | println(Utilities.name) 18 | } 19 | 20 | fun b(){ 21 | println(Utilities.name) 22 | } -------------------------------------------------------------------------------- /src/app/EnumClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Gender 4 | 5 | fun main() { 6 | val man = Gender.MALE 7 | val woman = Gender.FEMALE 8 | val allGenders: Array = Gender.values() 9 | 10 | val manFromString = Gender.valueOf("MALE") 11 | val womanFromString = Gender.valueOf("FEMALE") 12 | 13 | println(man) 14 | println(woman) 15 | println(allGenders.toList()) 16 | 17 | man.showDescription() 18 | woman.showDescription() 19 | } -------------------------------------------------------------------------------- /src/data/Address.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Address { 4 | var street: String = "" 5 | var city: String = "" 6 | var country: String = "Indonesia" 7 | 8 | constructor(paramStreet: String, paramCity: String) { 9 | street = paramStreet 10 | city = paramCity 11 | } 12 | 13 | constructor(paramStreet: String, paramCity: String, paramCountry: String) 14 | : this(paramStreet, paramCity) { 15 | country = paramCountry 16 | } 17 | } -------------------------------------------------------------------------------- /src/app/TypeAlias.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Application 4 | 5 | typealias App = Application 6 | typealias Aplikasi = App 7 | typealias App2 = data2.Application 8 | 9 | typealias StringSupplier = () -> String 10 | 11 | fun sayHello(supplier: StringSupplier){ 12 | println("Hello ${supplier()}") 13 | } 14 | 15 | fun main() { 16 | val app = App("Kotlin App") 17 | val aplikasi = Aplikasi("Kotlin App") 18 | val app2 = App2("Kotlin App v2") 19 | 20 | sayHello { "Eko" } 21 | } -------------------------------------------------------------------------------- /src/data/Shape.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | open class Shape { 4 | open val corner: Int = -1 5 | open fun printName(){ 6 | println("This is Shape") 7 | } 8 | } 9 | 10 | class Rectangle : Shape() { 11 | override val corner: Int = 4 12 | val parentCorner:Int = super.corner 13 | override fun printName() { 14 | println("This is Rectangle") 15 | super.printName() 16 | } 17 | } 18 | 19 | open class Triangle : Shape() { 20 | override val corner: Int = 3 21 | } -------------------------------------------------------------------------------- /src/app/SealedClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Minus 4 | import data.Modulo 5 | import data.Operation 6 | import data.Plus 7 | 8 | fun operation(value1: Int, value2: Int, operation: Operation): Int { 9 | return when (operation) { 10 | is Plus -> value1 + value2 11 | is Minus -> value1 - value2 12 | is Modulo -> value1 % value2 13 | } 14 | } 15 | 16 | fun main() { 17 | println(operation(10, 10, Plus())) 18 | println(operation(10, 5, Minus())) 19 | println(operation(10, 3, Modulo())) 20 | } -------------------------------------------------------------------------------- /src/app/AnonymousClass.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Action 4 | 5 | fun fireAction(action: Action) { 6 | action.action() 7 | } 8 | 9 | class SampleAction : Action { 10 | override fun action() { 11 | println("This is sample action") 12 | } 13 | } 14 | 15 | fun main() { 16 | fireAction(SampleAction()) 17 | fireAction(object : Action { 18 | override fun action() = println("Action one") 19 | }) 20 | fireAction(object : Action { 21 | override fun action() = println("Action two") 22 | }) 23 | } -------------------------------------------------------------------------------- /src/app/NullSafety.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | data class Friend(val name: String) 4 | 5 | fun sayHello(friend: Friend?) { 6 | // if(friend != null){ 7 | // println("Hello ${friend.name}") 8 | // } 9 | // println("Hello ${friend?.name}") 10 | 11 | // val name = friend?.name ?: "Friend" 12 | // println("Hello $name") 13 | 14 | val notNullFriend = friend!! 15 | val name = notNullFriend.name 16 | println("Hello $name") 17 | } 18 | 19 | fun main() { 20 | sayHello(Friend("Eko")) 21 | sayHello(null) // Error 22 | } -------------------------------------------------------------------------------- /belajar-kotlin-oop.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/data/Car.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Car(paramBrand: String, paramName: String, paramYear: Int) { 4 | 5 | init { 6 | println("Car $paramBrand dibuat") 7 | } 8 | 9 | constructor(paramBrand: String, paramName: String) : this(paramBrand, paramName, 2020) { 10 | println("Secondary Constructor 1") 11 | } 12 | 13 | constructor(paramBrand: String) : this(paramBrand, "") { 14 | println("Secondary Constructor 2") 15 | } 16 | 17 | var brand: String = paramBrand 18 | var name: String = paramName 19 | var year: Int = paramYear 20 | } -------------------------------------------------------------------------------- /src/data/Customer.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | open class Customer(val name: String, val type: String, val balance: Long) { 4 | constructor(name: String, type: String) : this(name, type, 0) 5 | constructor(name: String) : this(name, "Standard") 6 | } 7 | 8 | class PremiumCustomer : Customer { 9 | constructor(name: String) : super(name, "Premium") 10 | constructor(name: String, balance: Long) : super(name, "Premium", balance) 11 | } 12 | 13 | class ExecutiveCustomer(name: String, balance: Long) : Customer(name, "Executive", balance) { 14 | constructor(name: String) : this(name, 0) 15 | } -------------------------------------------------------------------------------- /src/data/Note.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Note(title: String) { 4 | 5 | var title: String = title 6 | get() { 7 | println("Call getter function") 8 | return field 9 | } 10 | set(value) { 11 | println("Call setter function") 12 | if (value.isNotBlank()) { 13 | field = value 14 | } else { 15 | println("Invalid title") 16 | } 17 | } 18 | 19 | } 20 | 21 | class BigNote(val title: String) { 22 | val bigTitle: String 23 | get() = title.toUpperCase() 24 | } -------------------------------------------------------------------------------- /src/data/Person.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | class Person { 4 | var firstName: String = "" 5 | var middleName: String? = null 6 | var lastName: String = "" 7 | 8 | fun sayHello(name: String) { 9 | println("Hello $name, My Name is ${this.firstName}") 10 | } 11 | 12 | fun sayHello(firstName: String, lastName: String) { 13 | println("Hello $firstName $lastName, My Name is ${this.firstName}") 14 | } 15 | 16 | fun run(){ 17 | println("I'm Run") 18 | } 19 | 20 | fun getFullName(): String { 21 | return "$firstName $middleName $lastName" 22 | } 23 | } -------------------------------------------------------------------------------- /src/data/Interaction.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | interface Interaction { 4 | val name: String 5 | fun sayHello(name: String) { 6 | println("Hello $name, my name is ${this.name}") 7 | } 8 | } 9 | 10 | interface Go : Interaction { 11 | fun go(){ 12 | println("Go!") 13 | } 14 | } 15 | 16 | interface MoveA { 17 | fun move() = println("Move A") 18 | } 19 | 20 | interface MoveB { 21 | fun move() = println("Move B") 22 | } 23 | 24 | class Human(override val name:String): Go, MoveA, MoveB{ 25 | override fun move() { 26 | super.move() 27 | super.move() 28 | println("Move Human") 29 | } 30 | } -------------------------------------------------------------------------------- /src/app/Exception.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import exception.ValidationException 4 | 5 | fun validateAndSayHello(name: String) { 6 | if (name.isBlank()) { 7 | throw ValidationException("Name is blank") 8 | } else { 9 | println("Hello $name") 10 | } 11 | } 12 | 13 | fun main() { 14 | try { 15 | validateAndSayHello("Eko") 16 | // validateAndSayHello("") 17 | println("Program") 18 | } catch (error: ValidationException) { 19 | println("Terjadi Error ${error.message}") 20 | } catch (error: Throwable) { 21 | println("Terjadi Error ${error.message}") 22 | } finally { 23 | println("Program Selesai") 24 | } 25 | } -------------------------------------------------------------------------------- /src/app/ScopeFunction.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Student 4 | 5 | fun main() { 6 | val student = Student("Eko", 15) 7 | val result: String = student.let { 8 | "Name ${it.name}, Age ${it.age}" 9 | } 10 | println(result) 11 | 12 | val result2: String = student.run { 13 | "Name ${this.name}, Age ${this.age}" 14 | } 15 | println(result2) 16 | 17 | val result3: Student = student.apply { 18 | "Name ${this.name}, Age ${this.age}" 19 | } 20 | println(result3) 21 | 22 | val result4: Student = student.also { 23 | "Name ${it.name}, Age ${it.age}" 24 | } 25 | println(result4) 26 | 27 | val result5: String = with(student) { 28 | "Name ${this.name}, Age ${this.age}" 29 | } 30 | println(result5) 31 | } -------------------------------------------------------------------------------- /src/data/Employee.kt: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | open class Employee(val name: String) { 4 | open fun sayHello(name: String) { 5 | println("Hello $name, My Name is ${this.name}") 6 | } 7 | } 8 | 9 | open class Manager(name: String) : Employee(name) { 10 | final override fun sayHello(name: String) { 11 | println("Hello $name, My Name is Manager ${this.name}") 12 | } 13 | } 14 | 15 | class SuperManager(name: String) : Manager(name){ 16 | // ERROR 17 | // override fun sayHello(name: String) { 18 | // println("Hello $name, My Name is Super Manager ${this.name}") 19 | // } 20 | } 21 | 22 | class VicePresident(name: String) : Employee(name) { 23 | override fun sayHello(name: String) { 24 | println("Hello $name, My Name is Vice President ${this.name}") 25 | } 26 | } -------------------------------------------------------------------------------- /src/app/DestructuringDeclaration.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.Game 4 | import data.Login 5 | import data.MinMax 6 | 7 | fun minmax(value1: Int, value2: Int): MinMax { 8 | return when { 9 | value1 > value2 -> MinMax(value2, value2) 10 | else -> MinMax(value1, value2) 11 | } 12 | } 13 | 14 | fun login(login: Login, callback: (Login) -> Boolean): Boolean { 15 | return callback(login) 16 | } 17 | 18 | fun main() { 19 | val game = Game("Game Kotlin", 1000000) 20 | // val name = game.name 21 | // val price = game.price 22 | val (name, price) = game 23 | println(name) 24 | println(price) 25 | 26 | // val result = minmax(10, 100) 27 | // val min = result.min 28 | // val max = result.max 29 | val (min, max) = minmax(10, 100) 30 | println(min) 31 | println(max) 32 | 33 | val login = Login("eko", "rahasia") 34 | // login(login){ login -> 35 | // login.username == "eko" && login.password == "rahasia" 36 | // } 37 | login(login) { (username, password) -> 38 | username == "eko" && password == "rahasia" 39 | } 40 | } -------------------------------------------------------------------------------- /src/app/Reflection.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import annotations.NotBlank 4 | import data.CreateCategoryRequest 5 | import data.CreateProductRequest 6 | import exception.ValidationException 7 | import kotlin.reflect.full.findAnnotation 8 | import kotlin.reflect.full.memberProperties 9 | 10 | fun validateRequest(request: Any) { 11 | val clazz = request::class 12 | val properties = clazz.memberProperties 13 | 14 | // iterate satu satu, lalu cek kalo ada annotation @NotBlank 15 | for (property in properties) { 16 | if (property.findAnnotation() != null) { 17 | val value = property.call(request) 18 | when (value) { 19 | is String -> { 20 | if ("" == value) { 21 | throw ValidationException("${property.name} is blank") 22 | } 23 | } 24 | } 25 | } 26 | } 27 | } 28 | 29 | fun main() { 30 | val request = CreateProductRequest("1", "Indomie", 2000) 31 | validateRequest(request) 32 | 33 | val request2 = CreateCategoryRequest("F", "") 34 | validateRequest(request2) 35 | } -------------------------------------------------------------------------------- /src/app/TypeCheck.kt: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import data.HandPhone 4 | import data.Laptop 5 | 6 | fun printObject(any: Any) { 7 | if (any is Laptop) { 8 | println("Laptop with name ${any.name}") 9 | } else if (any is HandPhone) { 10 | println("HandPhone with name ${any.name}") 11 | } else { 12 | println(any) 13 | } 14 | } 15 | 16 | fun printObjectWithWhen(any: Any) { 17 | when (any) { 18 | is Laptop -> println("Laptop with name ${any.name}") 19 | is HandPhone -> println("HandPhone with name ${any.name}") 20 | else -> println(any) 21 | } 22 | } 23 | 24 | fun printString(any: Any){ 25 | val value = any as String 26 | println(value) 27 | } 28 | 29 | fun printStringSafe(any : Any){ 30 | val value: String? = any as? String 31 | println(value) 32 | } 33 | 34 | fun main() { 35 | printString("Eko") 36 | // printString(1) // ERROR 37 | 38 | printStringSafe("Eko") 39 | printStringSafe(1) // null 40 | 41 | printObjectWithWhen("Eko") 42 | printObjectWithWhen(1) 43 | printObjectWithWhen(Laptop("Acer")) 44 | printObjectWithWhen(HandPhone("Samsung")) 45 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### JetBrains template 3 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 4 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 5 | 6 | .idea 7 | # User-specific stuff 8 | .idea/**/workspace.xml 9 | .idea/**/tasks.xml 10 | .idea/**/usage.statistics.xml 11 | .idea/**/dictionaries 12 | .idea/**/shelf 13 | 14 | # Generated files 15 | .idea/**/contentModel.xml 16 | 17 | # Sensitive or high-churn files 18 | .idea/**/dataSources/ 19 | .idea/**/dataSources.ids 20 | .idea/**/dataSources.local.xml 21 | .idea/**/sqlDataSources.xml 22 | .idea/**/dynamic.xml 23 | .idea/**/uiDesigner.xml 24 | .idea/**/dbnavigator.xml 25 | 26 | # Gradle 27 | .idea/**/gradle.xml 28 | .idea/**/libraries 29 | 30 | # Gradle and Maven with auto-import 31 | # When using Gradle or Maven with auto-import, you should exclude module files, 32 | # since they will be recreated, and may cause churn. Uncomment if using 33 | # auto-import. 34 | # .idea/artifacts 35 | # .idea/compiler.xml 36 | # .idea/jarRepositories.xml 37 | # .idea/modules.xml 38 | # .idea/*.iml 39 | # .idea/modules 40 | # *.iml 41 | # *.ipr 42 | 43 | # CMake 44 | cmake-build-*/ 45 | 46 | # Mongo Explorer plugin 47 | .idea/**/mongoSettings.xml 48 | 49 | # File-based project format 50 | *.iws 51 | 52 | # IntelliJ 53 | out/ 54 | 55 | # mpeltonen/sbt-idea plugin 56 | .idea_modules/ 57 | 58 | # JIRA plugin 59 | atlassian-ide-plugin.xml 60 | 61 | # Cursive Clojure plugin 62 | .idea/replstate.xml 63 | 64 | # Crashlytics plugin (for Android Studio and IntelliJ) 65 | com_crashlytics_export_strings.xml 66 | crashlytics.properties 67 | crashlytics-build.properties 68 | fabric.properties 69 | 70 | # Editor-based Rest Client 71 | .idea/httpRequests 72 | 73 | # Android studio 3.1+ serialized cache file 74 | .idea/caches/build_file_checksums.ser 75 | 76 | ### Kotlin template 77 | # Compiled class file 78 | *.class 79 | 80 | # Log file 81 | *.log 82 | 83 | # BlueJ files 84 | *.ctxt 85 | 86 | # Mobile Tools for Java (J2ME) 87 | .mtj.tmp/ 88 | 89 | # Package Files # 90 | *.jar 91 | *.war 92 | *.nar 93 | *.ear 94 | *.zip 95 | *.tar.gz 96 | *.rar 97 | 98 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 99 | hs_err_pid* 100 | 101 | --------------------------------------------------------------------------------