Kotlin A Programming Language Part I
Kotlin is a new open source programming language, it is like other programming language with some new powerful features. Kotlin is statically typed programming language that combines functional and technical part at the same place.
Before moving further let's understand what is statically typed programming languages, statically typed programming languages are those where data types are checked at compile time. For example java is a Statically typed programming language.
Advantages and Disadvantages
Kotlin is a brand new programming language. In 2017 google announce it a official language for Android app development. We can not says it is a alternate of Java but it has more feature then java.
Advantages of kotlin are Easy to understand,Concise, Run-time Performance,Inter-operaterbility, Band-New.
Interoperaterbility
Interoperaterbility is a phenomena of communication between two or more then two system. Kotlin programming language is well design to develop software in Interoperative way.
let's understand it by a picture
Disadvantages
Name space declaration and No static deceleration
Name space declaration koltin allow to develop the function at the top level if the same function is declared at many places then it is hard to find which function is called.
No Static Declaration − Kotlin does not have usual static handling modifier like Java, which can cause some problem to the conventional Java developer.
Classes in Kotlin
Class myClass { // class Header
// class Body
}
class myClass {
// property (data member)private var name: String = "rajendra verma"
// member functionfun printMe() {
print("Nice person his name is "+name)
}
}
fun main(args: Array<String>) {
val obj = myClass() // create obj object of myClass class
obj.printMe()
}
Nested class
When a class is inside another class , then it is called as Nested class.
Inner class
It is same as Nested class but the class which is inside is marked as inner.
fun main(args: Array<String>) {
val demo = Outer().Nested().foo() // calling nested class method
print(demo)
}
class Outer {
private val welcomeMessage: String = "rajendra verma"
inner class Nested {
fun foo() = welcomeMessage
}
}
Anonymous Inner Class
fun main(args: Array<String>) {
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 ")
}
}
programmer.think()
}
interface Human {
fun think()
}
Constructor in Kotlin
Kotlin have one primary constructor and it is as the header of the class but class may have more then once secondary constructor.
primary constructor --initialize class
secondary constructor -- some extra logic.
Primary Constructor
class Person(val firstName: String, var age: Int) {
// class body
}
fun main(args: Array<String>) {
val person1 = Person("rajendra ", 32)
println("First Name = ${person1.firstName}")
println("Age = ${person1.age}")
}
class Person(val firstName: String, var age: Int) {
}
Output of code
First Name = rajendra Age = 32
Secondary Constructor
It is same as primary constructor but it start with keyword constructor
fun main(args: Array<String>) {
val HUman = HUman("TutorialsPoint.com", 25)
print("${HUman.message}"+"${HUman.firstName}"+
"Welcome to the example of Secondary constructor, Your Age is-${HUman.age}")
}
class HUman(val firstName: String, var age: Int) {
val message:String = "Hey!!!"
constructor(name : String , age :Int ,message :String):this(name,age) {
}
}
Inheritance
Parent to child relationship is called as inheritance in any OOP programming language. It exist in Koltin also with the keyworkd :
But in kotlin everything is final by default sothat to inherit the class we have to use keyword open to which class we need to extand.
import java.util.Arrays
open class ABC {
fun think () {
print("Hey!! i am thiking ")
}
}
class BCD: ABC(){ // inheritence happend using default constructor
}
fun main(args: Array<String>) {
var a = BCD()
a.think()
}
Let’s see the function overriding in kotlin
Function override is same a other programming language
import java.util.Arrays
open class ABC {
open fun think () {
print("Hey!! i am thinking ")
}
}
class BCD: ABC() { // inheritance happens using default constructor override fun think() {
print("I Am from Child")
}
}
fun main(args: Array<String>) {
var a = BCD()
a.think()
}
Interfaces in kotlin
Kotlin interface is same a java 8 it have abstract methods , variables and one body method
interface ExampleInterface {
var myVar: String // abstract propertyfun absMethod() // abstract method
fun sayHello() = "Hello there" // method with default implementation
}
Let's see how we are using it.
interface ExampleInterface {
var myVar: Int // abstract propertyfun absMethod():String // abstract method
fun hello() {
println("Hello there, Welcome to TutorialsPoint.Com!")
}
}
class InterfaceImp : ExampleInterface {
override var myVar: Int = 25override fun absMethod() = "Happy Learning "
}
fun main(args: Array<String>) {
val obj = InterfaceImp()
println("My Variable Value is = ${obj.myVar}")
print("Calling hello(): ")
obj.hello()
print("Message from the Website-- ")
println(obj.absMethod())
}
All the above we completed the basic of Kotlin, In the following blog we cover introduction of kolin, class , inheritance and interfaces. It is more than enough for today into the next blog let’s come with some more basic, for now please read and comment how’s it is .