OOPs Kotlin

Android development with Kotlin
4 min readMay 8, 2023

--

Object-situated programming (OOP) is a programming paradigm that is broadly utilized in modern programming. It is based on the concept of “objects,” which are instances of classes that encapsulate information(data) and behavior. Kotlin, a famous programming language that runs on the Java Virtual Machine (JVM), fully supports object-oriented programming(OOPS). In this blog, we will explore how OOP is implemented in Kotlin.

Classes and Objects

In Kotlin, everything is an object, and every object belongs to a class. A class is like a blueprint for creating objects. It defines the properties and methods that objects of that class will have. To create an object, we need to create an instance of the class.

Here’s an example of a simple class definition:

class Employee{
var name: String = ""
var salary: Int = 0
}

This class defines a person with a name and an age. The var keyword indicates that these properties are mutable, meaning they can be changed. We can create an instance of this class like this:

val employee= Employee()

This creates a new instance of the Person class and assigns it to the variable person. We can set the properties of this instance like this:

employee.name = "Alem"
employee.salary= 50000

Inheritance

Inheritance is a key feature of OOP that allows us to create new classes based on existing ones. In Kotlin, you can create a subclass by using the : (colon) symbol followed by the name of the superclass.

Here’s an example of a simple inheritance hierarchy:

open class Car{
fun run() {
println("Car is running.")
}
}

class PetrolCar: Car() {
fun seat() {
println("Five seater car!")
}
}

This defines an Car class with an run method, and a PetrolCar class that inherits from Car and adds a seat method. You can create an instance of the Car class and call its methods like this:

val car = Car()
car.run() // Car is running.
cat.seat() // Five seater car!

Polymorphism

Polymorphism is the ability of a method to take on different forms depending on the type of object it is called on. In Kotlin, we can achieve polymorphism using the override and super keywords. The override keyword is used to indicate that a method is overriding a method in the parent class, and the super keyword is used to call the parent class’s implementation of the method. Here is an example:

open class Shape {
open fun draw() {
println("Drawing a shape")
}
}

class Circle: Shape() {
override fun draw() {
println("Drawing a circle")
super.draw()
}
}

class Square: Shape() {
override fun draw() {
println("Drawing a square")
super.draw()
}
}

In this example, we have defined a class called Shape with a draw method. We have also defined two subclasses called Circle and Square that inherit from Shape and override the draw method. The Circle and Square classes also call the parent class’s implementation of the draw method using the super keyword.

We will briefly discuss the override and super in another blog post.

Encapsulation

Encapsulation is one of the key principles of Object-Oriented Programming (OOP) and is used to protect the data and methods of a class from external interference. In Kotlin, we can achieve encapsulation using access modifiers such as public, protected, and private.

The private modifier is used to restrict access to a property or method to only within the same class. For example:

class BankAccount {
private var balance = 0

fun deposit(amount: Int) {
balance += amount
}

fun withdraw(amount: Int) {
if (amount <= balance) {
balance -= amount
} else {
println("Insufficient funds")
}
}
}

In this example, the balance property is declared private, so it can only be accessed within the BankAccount class. The deposit and withdraw methods can access the balance property because they are also part of the same class.

The protected modifier is used to restrict access to a property or method to only within the same class and its subclasses. For example:

open class Animal {
protected var name = ""

fun setName(name: String) {
this.name = name
}
}

class Dog: Animal() {
fun speak() {
println("Woof, my name is $name")
}
}

In this example, the name property is declared as protected, so it can be accessed within the Animal class and its subclasses. The setName method can access the name property because it is part of the Animal class. The speak method in the Dog class can also access the name property because Dog is a subclass of Animal.

The public modifier is used to allow unrestricted access to a property or method from anywhere in the code.

class Car {
var model = ""
var year = 0

fun startEngine() {
println("Starting engine...")
}
}

In this example, both the model and year properties are declared as public, so they can be accessed from anywhere in the code. The startEngine method is also declared public, so it can be called from anywhere in the code. Encapsulation in Kotlin helps to keep the internal workings of a class hidden from other parts of the program. This makes it easier to maintain and modify the class without affecting other parts of the program. It also helps to prevent unintended modifications to the class by other parts of the program. By using access modifiers such as private, protected, and public, we can control the level of access to properties and methods in a class, making it a powerful tool for building robust and secure applications.

Please let me know your suggestions and comments. Follow for more such easy and helpful tutorials.

Thanks for reading…

Happy Coding!

--

--

Responses (1)