Tags

Although Kotlin is statically typed language, it supports object-oriented programming like Java. Before explaining further let us dive into the basic syntax of the Kotlin class.

class Vehicle { // class definition
	// class body
}

So, defining a basic class is as simple as this and similar to Java.

Before diving further into the class syntax let us first discuss about the four access modifier available in Kotlin.

Access Modifier

Public : A publicly defined entity is accessible from anywhere in or outside the class. It is similar to what public access modifier was like in Java. If no access modifier is define then by default it is public.

Private : If something is private then it can be accessed within the enclosing scope. For example if class in private then only the same file can access it. Next, if variable within class is private then it can be accessed only from the class.

Protected : If something is protected in the main class then it can be accessed by the sub-classes as well.

Internal : This is newly introduced in Kotlin which is not available in Java. If anything is define Internal then it limits the visibility within the same module. So, this creates the visibility in the package/module level.

More on Kotlin Class

Let us start by seeing more complete class with a function/method and attribute.

class Dog { 
	private var numberOfLegs = 4

	fun startBarking() {
		println("Barking!")
	}
}

fun main(args : Array<String&gt;) {
	val d = Dog() // Creating an object
	d.startBarking() // calling the class function
}

Nested Class

The class in the kotlin can be written inside the class, which is called the nested class. This is similar to what we had done in functions like defining the function inside the function. Let us see one example of nested class:

fun main(args: Array<String&gt;) {
   OuterClass.NestedClass().sayHello() 
}
class OuterClass {
   private var name: String = "Sudeep"
   class NestedClass {
      fun sayHello() {
          print("Hello! Sudeep")
      }
   }
}

Here, if you try to access the variable name from the NestedClass then it will throw the error. Next, we have inner class where you can access the variables of Outer Class inside it.

Inner Class

fun main(args: Array<String&gt;) {
   OuterClass().NestedClass().sayHello() 
}
class OuterClass {
   private var name: String = "Sudeep"
   inner class NestedClass {
      fun sayHello() {
          print("Hello! " + name)
      }
   }
}

So, we can now access the variable of the outer class inside the inner class as well and notice the difference in the calling the Nested Class function in line 2.

Anonymous Inner Class

Anonymous inner class are useful while creating and instantiating the object. For example let us see one example:

fun main(args: Array<String&gt;) {
   var animal : Dog = object: Dog { // creating an instance of the interface Dog
      override fun bark() { // overriding the bark method
         print("Bark! Bark! bark!")
      }
   }
   animal.bark()
}
interface Dog {
   fun bark()
}

Here we can create the object of interface by overriding the function at the same time.

That’s all for this post! Cheers!

Advertisements