Kotlin   A Programming Language Part I

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 .





To view or add a comment, sign in

More articles by Rajendra Verma

  • Whisper On-Device Implementation (Android)

    In the world of mobile architecture, we are constantly told to keep our ViewModels light and our heavy lifting in the…

    1 Comment
  • 🚀 From Server to Pocket: The Evolution of Meeting Maker

    I am excited to share a major technical milestone for Meeting Maker, a mobile app designed to convert your voice into…

  • Mastering the Unpredictable: A Guide to Jetpack Compose Side Effect APIs

    Jetpack Compose revolutionizes Android development by making the UI declarative: you describe what the screen should…

  • Gold goes ⬆️ / goes ⬇️

    I had a discussion in the family about when gold goes up and when it goes down, and then I did some research and wrote…

  • Prompt Engineering

    In the previous article we learned about generative AI and the basics of prompts, but here we will learn about prompt…

    2 Comments
  • Basics of prompt

    A generative AI is a type of artificial intelligence that can create new, original content. Unlike traditional AI that…

    1 Comment
  • A Fundamental Guide to Jira for Beginners

    Jira is a powerful and widely used tool for agile project management, particularly for Scrum teams. As a Scrum Master…

  • Experiment with Location, geofencing, and a MQTT button

    TL;DR, This Saturday I did experiment with my Node-RED server and some geo fencing APIs. I create a location-sensitive…

  • My First AI Model

    If you will check the below table, then you will get to know that Y is dependent on X and the relationship is. y = 2x -…

  • Evolution of Neural Network

    This is a very intersecting topic, just like an Netflix webseries. Now a days everyone is talking about AI, but no one…

Others also viewed

Explore content categories