Posts

Kotlin Abstract Class With Examples

Image
What are Abstract Classes ? A class that cannot be instantiated i.e. we cannot create the objects of an abstract class. Generally used as a parent class that defines the common protocol for the child classes. What are Abstract Methods? Abstract methods are those methods that do not have a body. These methods can only be defined inside an abstract class i.e. if you mark the method as abstract, you need to mark that class as abstract. Let's understand this with an example -  Suppose, we have this class hierarchy and we need to display these shapes and there is a method to calculate the area of these shape objects. We know how to display a square, a circle, or a triangle. But what about shape? If we define a shape class as a normal class - we can also create an instance of this Shape class. But there is a problem here. We don't know how a Shape looks like and we cannot even calculate its area because we don't know how it looks. So does it make sense to allow users to create a

Polymorphism in Kotlin With Example

Image
Polymorphism is one of the most important topics in Object-Oriented Programming. It goes hand in hand with Inheritance . Poly means many and morph means forms i.e. the same method behaves differently based on the object. Here we have different forms of the method that exhibits different behavior based on the object. To define Polymorphism in simple words - A parent can hold a reference to its child and can call methods which are provided by the parent class. Let's understand this with an example -  fun main() { val circle : Shape = Circle(4.0) val square : Shape = Square(4.0) } open class Shape{ open fun area() :Double{ return 0.0 } } class Circle(val radius:Double) : Shape(){ override fun area(): Double { return Math.PI * radius * radius } } class Square(val side:Double) : Shape(){ override fun area(): Double { return side * side } } Explanation -   Here we have defined a Shape class which is a base class and open for i

Overriding in Kotlin and Any Class

Image
Overriding in Kotlin Methods and properties defined in the parent class can be used in child classes using Inheritance. This prevents code duplication as child classes can re-use the functionality provided by the parent. But sometimes, we don't want to use the same behavior provided by the parent - we want to override a few of the methods that are specific to the child's class. We can do so using the override keyword in Kotlin. To make this work, we have to mark the property or method in the parent class as open and then in the child can we can override that. Let's see this in action -  fun main() { val square = Square() println(square.name) square.display() } open class Shape { open var name: String = "Shape" open fun display() = println("Shape is displayed") } class Square: Shape() { override var name: String = "Square" override fun display() = println("Square is displayed") } Explanation

Kotlin Inheritance With Example

Image
What is Inheritance? Just like in real life, we inherit features and behaviors from our parents. Our parents inherit from their grandparents. We can apply this same logic in programming.  While designing the classes we can extract out common properties and behavior in a separate class and other classes that require these features will inherit from this class - this will create a parent-child relationship among the classes. In simple words, Inheritance is a way of organizing code in a parent-child relationship that helps in reducing code duplication. You define the functionality in a parent class that will be inherited by child classes. You don't need to define the same functionality in a child class - child classes will have it from its parent. Inheritance in Kotlin Inheritance in Kotlin is no different than any other programming language that supports Object-Oriented Programming.  But in Kotlin, if you want to allow a class to be inheritable, you have to explicitly mark it as Open

Kotlin Getters And Setter + LateInit

Image
  Kotlin Getters & Setters With Example If you want to execute some logic before setting or getting the property of an object, you can use getters and setters.  You can consider these as functions that are executed when you either get the property or set the property of an object. But why we want to do this? Why we want to execute code before accessing it? Let's understand this with an example -  fun main() { val user = User("John", 20, "abc@cheezycode.com") user.age = -10 } class User(nameParam: String, ageParam: Int, emailParam: String) { var name : String = nameParam var age: Int = ageParam var email: String = emailParam } Explanation -  Here we have defined a User class - inside the main function, we are setting the value of age to a negative value.  In a real-world scenario, you don't want any user to have negative age. So you also want to prevent this in your code. You don't want anybody to set a negative value to a

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