Tuesday, 23 January 2018

Kotlin: Abstract classes

An abstract class can have methods without implementation. Abstract class is defined using ‘abstract’ keyword.

Ex
interface Arithmetic{
 fun sum(x : Int, y : Int) : Int
 fun sub(x : Int, y : Int) : Int
 fun mul(x : Int, y : Int) : Int
 fun div(x : Int, y : Int) : Int
}

abstract class ArithmeticImpl : Arithmetic{
 override fun sum(x : Int, y : Int) : Int{
  return x + y
 }
 
 override fun sub(x : Int, y : Int) : Int{
  return x - y
 }
}

You can’t define instance (object) to abstract class.

Abstract method
Just like how you defined abstract class, you can define abstract method (method without implementation) using the keyword ‘abstract’.

abstract class ArithmeticImpl : Arithmetic{
 override fun sum(x : Int, y : Int) : Int{
  return x + y
 }
 
 override fun sub(x : Int, y : Int) : Int{
  return x - y
 }
 
 abstract fun areaofCircle(radius : Int) : Int
}

In the above snippet, I declared ‘areaOfCircle’ as abstract method. Any concrete class that is extending this abstract class must provide implementation to all the abstract methods.


class ConcreteArithmetic : ArithmeticImpl() {
 override fun mul(x: Int, y: Int): Int {
  return x * y
 }

 override fun div(x: Int, y: Int): Int {
  return x / y
 }

 override fun areaofCircle(radius: Int): Double {
  return 3.14 * radius * radius
 }
}

Find the below working application.


HelloWorld.kt

interface Arithmetic {
 fun sum(x: Int, y: Int): Int
 fun sub(x: Int, y: Int): Int
 fun mul(x: Int, y: Int): Int
 fun div(x: Int, y: Int): Int
}

abstract class ArithmeticImpl : Arithmetic {
 override fun sum(x: Int, y: Int): Int {
  return x + y
 }

 override fun sub(x: Int, y: Int): Int {
  return x - y
 }

 abstract fun areaofCircle(radius: Int): Double
}

class ConcreteArithmetic : ArithmeticImpl() {
 override fun mul(x: Int, y: Int): Int {
  return x * y
 }

 override fun div(x: Int, y: Int): Int {
  return x / y
 }

 override fun areaofCircle(radius: Int): Double {
  return 3.14 * radius * radius
 }
}

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

 var sumOf10And20 = obj.sum(10, 20)
 var subOf10And20 = obj.sub(10, 20)
 var mulOf10And20 = obj.mul(10, 20)
 var divOf10And20 = obj.div(10, 20)
 var areaOfCircle = obj.areaofCircle(5)

 println("sumOf10And20 = $sumOf10And20")
 println("subOf10And20 = $subOf10And20")
 println("mulOf10And20 = $mulOf10And20")
 println("divOf10And20 = $divOf10And20")
 println("areaOfCircle = $areaOfCircle")

}


Output

sumOf10And20 = 30
subOf10And20 = -10
mulOf10And20 = 200
divOf10And20 = 0
areaOfCircle = 78.5






Previous                                                 Next                                                 Home

Monday, 22 January 2018

Kotlin: Access super class of outer class from inner class

By using the super keyword qualified with outer class name, we can access the super class of the outer class. Let’s me explain with an example.

Ex

Find below working application.

HelloWorld.kt
open class GrandParentClass {
 fun grandParentFunction() {
  println("I am in grand parent class")
 }
}

open class ParentClass : GrandParentClass() {
 fun parentClass() {
  println("I am in parent class")
 }

 inner class ChildInnerClass {
  fun printInfo() {
   parentClass()
   super@ParentClass.grandParentFunction()
  }
 }
}

fun main(args: Array<String>) {
 var innerObj = ParentClass().ChildInnerClass()

 innerObj.printInfo()
}

Output
I am in parent class
I am in grand parent class



Previous                                                 Next                                                 Home

Saturday, 20 January 2018

JavaDoc: @value: Display constant values

@value tag is used to display constant values.

The values of all the constants that are tagged with @value tag are displayed in 'Constant Field Values' section.

Arithmetic.java

package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithemtic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * 
  * @param radius
  *            Radius of the Circle
  * 
  * @return area of the circle.
  * 
  * @since 1.1
  */
 public double areaOfCircle(int radius);

}



Previous                                                 Next                                                 Home

Javadoc: @link: Used to create inline links

@link tag is used to create inline links that can refer to the documentation for the specified package, class, or member name of a referenced class.

Syntax
{@link package.class#member label}

Refer constant of same class
/**
 * Calculates the area of circle. It uses value of {@link #PI} in
 * calculation.
 *
 */
public double areaOfCircle(int radius);

Refer method of same class
/**
 * Prints the area of circle. It internally calls {@link #areaOfCircle(int)}
 * method
 *
 */
public void printAreaOfCircle(int radius);

Refer method of other class
/**
 * Calculate the squalre root of the parameter a. It internally calls
 * {@link java.lang.Math#sqrt(double)}
 *
 * @return square root of a
 */
public default double sqrt(double a) {
         return Math.sqrt(a);
}

Find the below example.


Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithemtic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * Calculates the area of circle. It uses value of {@link #PI} in
  * calculation.
  * 
  * @param radius
  *            Radius of the Circle
  * 
  * @return area of the circle.
  * 
  * @since 1.1
  */
 public double areaOfCircle(int radius);

 /**
  * Prints the area of circle. It internally calls {@link #areaOfCircle(int)}
  * method
  * 
  * @param radius
  *            Radius of the Circle.
  */
 public void printAreaOfCircle(int radius);

 /**
  * Calculate the squalre root of the parameter a. It internally calls
  * {@link java.lang.Math#sqrt(double)}
  * 
  * @param a
  *            Operand
  * @return square root of a
  */
 public default double sqrt(double a) {
  return Math.sqrt(a);
 }

}



Previous                                                 Next                                                 Home

Javadoc: @inheritDoc: Inherit the documentation

{@inheritDoc} tag is used to inherit the documentation from the nearest inheritable class or implementable interface into the current documentation comment at this tag's location.

You can use @inheritedDoc tag in description block, or in the text arguments of @return, @param, and @throws tags.

Usage of @inheritDoc tag in description block
When you use @inheritDoc tag in description block, the main description is copied from a class or interface up the hierarchy.

/**
 * {@inheritDoc}
 */
@Override
public void welcomeEmployee(String empName) {
         System.out.println("Hello " + empName);
}

ABCOrg.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public class ABCOrg {

 /**
  * Print welcome message to employee.
  * 
  * @param empName
  *            Employee Name
  */
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }

}

XYZOrg.java

package com.java.tags;

public class XYZOrg extends ABCOrg{

 /**
  * {@inheritDoc}
  */
 @Override
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }
}


Javadoc for the class XYZOrg generated like below.

Usage of @inheritDoc tag in tag descriptions of @return, @param, and @throws
When you use @inheritDoc tag in tag descriptions of @return, @param and @throws tags, the tag text is copied from the corresponding tag up the hierarchy.

ABCOrg.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public class ABCOrg {

 /**
  * Print welcome message to employee.
  * 
  * @param empName
  *            Employee Name
  */
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }

}


XYZOrg.java
package com.java.tags;

public class XYZOrg extends ABCOrg{

 /**
  * Welcome Employee.
  * 
  * @param empName {@inheritDoc}
  */
 @Override
 public void welcomeEmployee(String empName) {
  System.out.println("Hello " + empName);
 }
}


Javadoc for the class XYZOrg generated like below.



Previous                                                 Next                                                 Home

Javadoc: @version: Specify current version of the release

@version tag specify the current version of the release.

Syntax
@version version-text


/**
 * 
 * @author Hari Krishna
 * 
 * @version 1.5
 * 
 * @since 1.2
 *
 */
public class ABCOrg {

}


Javadoc is generated like below.

Previous                                                 Next                                                 Home

JavaDoc: @throws: specify the exception thrown by method

@throws tag is used to specify the exception thrown by a method (or) constructor.

Syntax
@throws class-name description

Can I use multiple throws tags to specify different exceptions?
Yes

Example
/**
 *
 * @throws IllegalArgumentException
 *             thrown if radius &lt; 0.
 */
public double areaOfCircle(int radius) throws IllegalArgumentException;

Arithmetic.java
package com.java.tags;

/**
 * 
 * @author Hari Krishna
 * 
 * @since 1.0
 */
public interface Arithmetic {

 /**
  * The value of PI is {@value}
  */
 public static final double PI = 3.14;

 /**
  * 
  * @param radius
  *            radius of the circle
  * 
  * @return area of the circle
  * 
  * @throws IllegalArgumentException
  *             thrown if radius &lt; 0.
  */
 public double areaOfCircle(int radius) throws IllegalArgumentException;

}

javadoc is generated like below.



Previous                                                 Next                                                 Home