Posts

Kotlin Constructors & Init Block

Image
Every object has 2 things - Properties and Methods. To initialize properties with the default values, constructors are used. Once the object is created, properties are initialized using the constructor. Let's understand this with an example -  fun main() { var car = Automobile("Car", 4, 4) var auto = Automobile("Auto", 3, 3) } class Automobile(val name: String, val tyres: Int, val maxSeating: Int) { fun drive() {} fun applyBrakes() {} } Explanation -  Here we have defined a class named Automobile. It has 3 properties - name, tyres, and max seating. This portion of the class definition  (val name: String, val tyres: Int, val maxSeating: Int)  is known as Constructor. In the main method, we have created 2 instances of this Automobile class and passed the required values for properties. For car instance, we have passed, name as Car, tyres and max seating as 4.  Values we have passed are used to initialize the properties ...

Kotlin Class & Objects - OOPS

Image
In our previous post , we talked about the Classes and Objects in Kotlin. This post covers the same concept from a different perspective.  We have different types in Kotlin to store data such as Integers, Booleans, String, etc. These are pre-defined data types in Kotlin. What if we want to store Student Information or if we want to store information about Employees etc? For storing this data, we can define our own data types in Kotlin using classes i.e. User-Defined Data Types. User-Defined data types allow us to store related information for an entity.  Let's say we want to store information about Cars like the name of Car, type of engine, seating capacity - we can define a type of Car using a class. Defining a type helps to store information as a unit. We can also define the methods i.e. the actions a car can perform in the same unit.  fun main() { val beetle = Car("Beetle", "Petrol", 4) beetle.driveCar() beetle.applyBrakes() } class Car(val na...

Kotlin Class and Objects With Examples

Image
Kotlin is an Object-Oriented Programming Language. Object-Oriented Programming is a paradigm that allows us to solve problems with the help of objects which represent real-world entities i.e. we break application code into objects that interact with each other to solve a particular problem. Object-Oriented Programming in Layman Terms In our real life, we take help from others to solve problems. Let's take one scenario - You want to repair your car. You go to a car mechanic and ask for help. The car mechanic agrees to repair your car with his/her tools.  If we look at this scenario, different objects are working together to solve a problem. You are an object, Car Mechanic is an object, Your Car is an object, Car Mechanic uses different objects (tools)  to repair your car. So different objects are interacting with each other to repair your car. This same principle or approach is used in Object-Oriented programming to solve a given problem. We create different objects that repr...

Kotlin Array With Examples

Image
What is an Array?  Let's understand Array with an example -  You need to store 50 favorite songs of the user. If you define 50 variables for storing these songs then it becomes messy to manage the code. Why? You will be handling all these 50 variables - managing functionality like previous and next, playing a particular song will be too complex. Array comes into the picture to rescue. You will be only creating a single object that will store your 50 songs like a list i.e. list of 50 songs. Now you can access all these songs using just one variable i.e. array object. Array - is a simple object that can store multiple values of the  same type and has a  fixed size . You need to define the type of values that will be stored into the array and you need to define the size. Explanation - To define an array, we use arrayOf function and pass comma-separated values inside this function. In the above image, we have a created an array of 3 elements which are of string data typ...

Kotlin Function Types With Examples

Image
  In Kotlin, functions are treated as values. Just like your integer values or strings - you can assign functions to a variable. Kotlin is considered as First-Class Function Language i.e. functions are treated as values. You can store them in a variable, pass them as parameters and return a function from a function.  Function Type Since functions are just like other values, there is a type that is associated with the functions. That type is known as Function Type. Let's look at some examples -  1. (Double, Double) -> Double This is how we define a function type. If a variable has this data type then it can take any function which takes 2 doubles as input parameters and returns the output as Double. Function signature contains 2 things - What is the type of input parameters and what is the return type of the function. So when you define a function type you need to mention these 2 things. Before the arrow ( ->)  you define the types of input parameters i.e. Doubl...

Kotlin Function Variations With Examples

Image
In our previous post, we learned about functions. In Kotlin, we can define a function in different ways. Let's learn about these variations one by one.  Single Line Function If there is only one expression, we can shorten the function. Refer to the snippet below -  fun add(a: Int, b: Int) : Int { val total = a + b return total } // Single Line Function fun add(a: Int, b: Int) : Int = a + b //Kotlin can infer the return type fun add(a: Int, b: Int) = a + b Unit Data Type When a function does not return any value - the return data type, in this case, is Unit. You can explicitly define a function with a return type as Unit but Kotlin is smart enough to inference. fun printMessage(msg : String) : Unit { println("Hi ! " + msg) } // Kotlin can infer the return value in this case fun printMessage(msg : String) { println("Hi ! " + msg) } In other programming languages like Java or C#, when the function does not return any value it is...

Kotlin Functions With Examples

Image
Complex application has thousands of lines of code. Writing them inside the main function is certainly not a good idea.  We split this code into separate and manageable chunks - each chunk with its dedicated functionality. This chunk is known as Function. In simple words - a function is a block of code that performs a specific task or functionality.  Now whenever we need this functionality somewhere in our code, we call this function. This function can take inputs and based on the inputs, it performs some actions and provides the output. This prevents code duplication and helps in code reuse. You can just define a function and call it whenever you need that particular functionality. Let's take one example -  Here you can see that ATM Machine acts as a function i.e. a standalone unit that takes input and provides you the output. Once you provide the card information, pin, and amount you want - it does some processing and gives you the required money. Similarly, in programm...