WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Class & Object

In this chapter we will learn the basics of Object-Oriented Programming (OOP) using Kotlin.

By definition of OOP, a class is a blueprint of a runtime entity and object is its state, which includes both its behavior and state


Class myClass { // class Header 

   // class Body
}

we will create one class and its object through which we will access different data members of that class.
class myClass {
   // property (data member)
   private var name: String = "welookups.tutorial"
   
   // member function
   fun printMe() {
      print("You are at the best Learning website Named-"+name)
   }
}
fun main(args: Array<String>) {
   val obj = myClass() // create obj object of myClass class
   obj.printMe()
}


output
You are at the best Learning website Named- welookups.tutorial&

Nested Class

nested class is by default static, it can be accessed without creating any object of that class.
fun main(args: Array) {
   val demo = Outer.Nested().foo() // calling nested class method
   print(demo)
}
class Outer {
   class Nested {
      fun foo() = "Welcome to The Welookups.com"
   }
}
Output
Welcome to The Welookups.com

Anonymous Inner Class

Anonymous inner class is a pretty good concept that makes the life of a programmer very easy.The concept of creating an object of interface using runtime object reference is known as anonymous class
fun main(args: Array) {
   var programmer :Human = object:Human // creating an instance of the interface {
      override fun think() { // overriding the think method
         print("I am an example of Anonymous Inner Class welookups.com")
      }
   }
   programmer.think()
}
interface Human {
   fun think()
}
Output
I am an example of Anonymous Inner Class  welookups.com

Type Aliases

Type aliases is really helpful for complex type

Kotlin revoked the support for type aliases, however, if you are using an old version of Kotlin you may have use it like the following
typealias NodeSet = Set<Network.Node>
typealias FileTable<K> = MutableMap<K, MutableList<File>>