Saturday, 20 January 2018

Javadoc: @author tag

@author tag is used to specify the author of the module.

Syntax
@author author1, author2…author

You can specify multiple author names in one @author tag (or) you can use multiple author tags to specify multiple author names.

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * @author Keerthi
 * 
 * @since Version 1.1
 *
 */
public interface Arithemtic {

 /**
  * 
  * @param a
  *            First Operand
  * @param b
  *            Second Operand
  * @return sum of two operands a and b
  * 
  * @since Version 1.1
  * 
  */
 int sum(int a, int b);
}


When you generate Javadoc for above interface, it looks like below.

As you above image, author names are part of Author section in the java documentation.

Previous                                                 Next                                                 Home

Javadoc tags

I am going to explain frequently used tags provided by java. Javadoc tool parses these tags, when they are embedded in Javadoc comments.

Tags starts with @ sign.

Ex:

Is tags are case sensitive?
Yes

Tags are categorized into two types
Block tags: Place block tags only in the tag section that follows the description. Block tags have the form: @tag.

Inline tags: Place inline tags anywhere in the main description or in the comments for block tags. Inline tags are enclosed within braces: {@tag}.

Following are the block tags that I used frequently

Following are the inline tags that I used frequently

Reference


Previous                                                 Next                                                 Home

Friday, 19 January 2018

Kotlin: Anonymous Inner class

Anonymous inner classes can be created using object expressions.

Ex
         printMessage(object : PrinterUtil() {
                 override fun welcomeMessage() {
                          println("Good Morning customer")
                 }
         })

Find the below working application.

HelloWorld.kt
open class PrinterUtil {
 open fun welcomeMessage() {
  println("Welcome user")
 }
}

fun printMessage(printUtl: PrinterUtil) {
 printUtl.welcomeMessage()
}

fun main(args: Array<String>) {
 printMessage(object : PrinterUtil() {
  override fun welcomeMessage() {
   println("Good Morning customer")
  }
 })
}

Output
Good Morning customer



Previous                                                 Next                                                 Home

Kotlin: Companion Objects

By using companion object, you can access the members using the class name (no need to define the object).

HelloWorld.kt
class TestClass {
 companion object {
  fun sayHello() {
   println("Hello")
  }
 }
}

fun main(args: Array<String>) {
 TestClass.sayHello()
}

Output
Hello

As you see above code snippet, I called sayHello function of companion object using TestClass (not created any object to TestClass).

You can give name to companion object.

         companion object FunModules {
                 fun sayHello() {
                          println("Hello")
                 }
         }


HelloWorld.kt
class TestClass {
 companion object FunModules {
  fun sayHello() {
   println("Hello")
  }
 }
}

fun main(args: Array<String>) {
 TestClass.sayHello()
}

Output
Hello


By adding @JvmStatic annotation on top of companion object, you can use it as static member in any java class.


HelloWorld.kt
import kotlin.jvm.JvmStatic

class TestClass {
 companion object {
  @JvmStatic
  fun sayHello() {
   println("Hello")
  }
 }
}

CompanionDemo.java

public class CompanionDemo {
 public static void main(String args[]){
  TestClass.sayHello();
 }
}


Output

Hello


Previous                                                 Next                                                 Home

Thursday, 18 January 2018

Kotlin: Define singleton objects

By using 'object' keyword, you can define singleton objects.

Syntax
object ObjectName{

}

Ex
object mySingleTonObj {
     var x: Int = 10
     var y: Int = 20
 }

You can access the member variables and functions of singleton object using the object name directly.

Ex
mySingleTonObj.x
mySingleTonObj.y

Find the below working application.

HelloWorld.kt
class MyClass {
 object mySingleTonObj {
  var x: Int = 10
  var y: Int = 20
 }

 fun printSingletonObj() {
  println("Value of x is  : ${mySingleTonObj.x}")
  println("Value of y is  : ${mySingleTonObj.y}")
 }
}

fun main(args: Array<String>) {
 var obj = MyClass()

 obj.printSingletonObj()
}

Output
Value of x is  : 10
Value of y is  : 20

Can a singleton object has super type?
Yes, a singleton has super types.

Ex
         object mySingleTonObj : Arithmetic {
                 override fun sum(x: Int, y: Int): Int {
                          return x + y
                 }
         }

Find the below working application.


HelloWorld.kt
interface Arithmetic {
 fun sum(x: Int, y: Int): Int
}

class MyClass {
 object mySingleTonObj : Arithmetic {
  override fun sum(x: Int, y: Int): Int {
   return x + y
  }
 }

 fun getSum(x: Int, y: Int): Int {
  return mySingleTonObj.sum(x, y)
 }

}

fun main(args: Array<String>) {
 var obj = MyClass()
 var result = obj.getSum(10, 20)

 println("Sum of 10 and 20 is : $result")
}

Output
Sum of 10 and 20 is : 30

Can object declaration be local?
Object declarations can't be local. For ex, you can't define them inside a function.


HelloWorld.kt

fun main(args: Array<String>) {
 object mySingleTonObj {
  fun sum(x: Int, y: Int): Int {
   return x + y
  }
 }

}

When you try to compile above program, you will end up in below error.


Named object 'mySingleTonObj' is a singleton and cannot be local. Try to use anonymous object instead




Previous                                                 Next                                                 Home

Kotlin: Object Expressions

If you want to create an object that is slight modification to the existing class, you can use anonymous class.

By using Object expressions, you can create anonymous class.

For example,
open class PrintUtil {
         public open fun welcomeMessage(name: String) {
                 println("Hello $name")
         }
}

Now we can define anonymous class like below.

         /* Define object using anonymous class */
         var anonymousObj: PrintUtil = object : PrintUtil() {

                 override fun welcomeMessage(name: String) {
                          println("Welcome $name")
                 }
         }

As you observe above snippet, I defined the object 'anonymousObj' using object expression. Object expressions are defined using the keyword 'object'.

HelloWorld.kt
open class PrintUtil {
 public open fun welcomeMessage(name: String) {
  println("Hello $name")
 }
}

fun main(args: Array<String>) {
 var obj = PrintUtil()

 obj.welcomeMessage("Krishna")

 /* Define object using anonymous class */
 var anonymousObj: PrintUtil = object : PrintUtil() {

  override fun welcomeMessage(name: String) {
   println("Welcome $name")
  }
 }

 anonymousObj.welcomeMessage("Krishna")
}

Output

Hello Krishna
Welcome Krishna

You can also pass parameterized constructor while defining anonymous class

open class PrintUtil(var greeting : String) {
         public open fun welcomeMessage(name: String) {
                 println("Hello $name")
         }
}

We can define anonymous class like below.

         /* Define object using anonymous class */
         var anonymousObj: PrintUtil = object : PrintUtil("Good Morning") {

                 override fun welcomeMessage(name: String) {
                          println("Welcome $name")
                 }
         }

Find the below working application.


HelloWorld.kt

open class PrintUtil(var greeting: String) {
 public open fun welcomeMessage(name: String) {
  println("Hello $name")
 }
}

fun main(args: Array<String>) {
 var obj = PrintUtil("Good Morning")

 obj.welcomeMessage("Krishna")

 /* Define object using anonymous class */
 var anonymousObj: PrintUtil = object : PrintUtil("Good Morning") {

  override fun welcomeMessage(name: String) {
   println("Welcome $name")
  }
 }

 anonymousObj.welcomeMessage("Krishna")
}

Can I define an anonymous object without any super type?
Yes, you can define anonymous object, without any super type.

Ex
         val newAnonymousObj = object {
                 var x: Int = 1
                 var y: Int = 2
                 var z: Int = 3

                 fun welcomeUser(name: String) {
                          println("Hello $name")
                 }
         }

Find below working application.


HelloWorld.kt
fun main(args: Array<String>) {
 val newAnonymousObj = object {
  var x: Int = 1
  var y: Int = 2
  var z: Int = 3

  fun welcomeUser(name: String) {
   println("Hello $name")
  }
 }

 newAnonymousObj.welcomeUser("Krishna")

 println("Value of x is : ${newAnonymousObj.x}")
 println("Value of y is : ${newAnonymousObj.y}")
 println("Value of z is : ${newAnonymousObj.z}")
}


Output
Hello Krishna
Value of x is : 1
Value of y is : 2
Value of z is : 3

Can an object expression access the code of enclosing block?
Yes, an object expression can access the code of enclosing block.


HelloWorld.kt

fun main(args: Array<String>) {
 var message = "Hello, Good Morning"
 
 val newAnonymousObj = object {

  fun welcomeUser(name: String) {
   println("$message $name")
  }
 }

 newAnonymousObj.welcomeUser("Krishna")

}

Output

Hello, Good Morning Krishna



Previous                                                 Next                                                 Home

Thursday, 11 January 2018

Kotlin: Overriding conflict while implementing interfaces

If you are implementing more than one interface and those interfaces have same method signatures, then you will end up in conflict scenario.

HelloWorld.kt
interface Interface1{
 fun welcomeMessage(name : String){
  println("Welcome $name")
 }
}

interface Interface2{
 fun welcomeMessage(name : String){
  println("Good Morning $name")
 }
}

class MyClass : Interface1, Interface2{

 
}

When you try to compile above program, you will end up in below error.

ERROR: Class 'MyClass' must override public open fun welcomeMessage(name: String): Unit defined in Interface1 because it inherits multiple interface methods of it (13, 1)

How to resolve above error?
By overriding the function welcomeMessage in MyClass, you can get rid of above error.

class MyClass : Interface1, Interface2 {
 override fun welcomeMessage(name: String) {
  println("Hello $name")
 }

}

Find the below working application.


HelloWorld.kt
interface Interface1 {
 fun welcomeMessage(name: String) {
  println("Welcome $name")
 }
}

interface Interface2 {
 fun welcomeMessage(name: String) {
  println("Good Morning $name")
 }
}

class MyClass : Interface1, Interface2 {
 override fun welcomeMessage(name: String) {
  println("Hello $name")
 }

}

fun main(args: Array<String>) {
 var obj = MyClass()
 obj.welcomeMessage("krishna")
}

Output
Hello Krishna

How can I call interface method implementations from class?
By using super keyword, you can call interface method implementations from a class.

Ex
class MyClass : Interface1, Interface2 {
         override fun welcomeMessage(name: String) {
                 super<Interface1>.welcomeMessage(name)
                 super<Interface2>.welcomeMessage(name)
         }
}

Find the below working application.

HelloWorld.kt

interface Interface1 {
 fun welcomeMessage(name: String) {
  println("Welcome $name")
 }
}

interface Interface2 {
 fun welcomeMessage(name: String) {
  println("Good Morning $name")
 }
}

class MyClass : Interface1, Interface2 {
 override fun welcomeMessage(name: String) {
  super<Interface1>.welcomeMessage(name)
  super<Interface2>.welcomeMessage(name)
 }
}

fun main(args: Array<String>) {
 var obj = MyClass()
 obj.welcomeMessage("krishna")
}

Output

Welcome krishna
Good Morning Krishna


Previous                                                 Next                                                 Home