OOPs in Kotlin

Ashfaque
4 min readFeb 29, 2024

--

In Kotlin, Object-Oriented Programming (OOP) is a fundamental paradigm used for organizing code into objects that encapsulate data and behavior. Kotlin supports all the standard OOP concepts such as classes, objects, inheritance, encapsulation, and polymorphism. Here’s an overview of how OOP is implemented in Kotlin:

Classes:

In Kotlin, a class is a blueprint for creating objects. It encapsulates data for the object and defines behavior through member functions.

Classes can have properties (fields) and methods (functions).

You can define a class using the class keyword followed by the class name.

Classes can also have constructors, which are used to initialize class properties when an object is created.

class MyClass {
// Properties
var myProperty: Int = 0

// Methods
fun myMethod() {
// Method implementation
}
}

Objects:

An object is an instance of a class.

In Kotlin, you create objects using the object keyword. This keyword can be used to define a singleton object (a class that has only one instance).

You can also create anonymous objects using object expressions, which are useful for implementing interfaces or creating objects without explicitly declaring a new subclass.

val obj = MyClass() // Creating an instance of MyClass

val anonymousObj = object {
val x: Int = 10
val y: Int = 20
}

Inheritance:

Inheritance allows a class (subclass/derived class) to inherit properties and behavior from another class (superclass/base class).

In Kotlin, you use the : symbol to denote inheritance. For example, class Subclass : Superclass().

Kotlin supports single inheritance, meaning a class can inherit from only one superclass.

The open keyword is used to mark a class or a member of a class as inheritable. By default, classes and their members are final (not inheritable).

open class Superclass {
// Superclass members
}

class Subclass : Superclass() {
// Subclass members
}

Encapsulation:

Encapsulation is the bundling of data (properties) and methods (functions) that operate on the data into a single unit (class).

Kotlin supports encapsulation through access modifiers:

public (default): Accessible from anywhere.

private: Accessible only within the same class.

protected: Accessible within the same class and its subclasses.

internal: Accessible within the same module (a set of Kotlin files compiled together).

class Example {
private var privateVar: Int = 0
protected var protectedVar: Int = 0
internal var internalVar: Int = 0
var publicVar: Int = 0
}

Polymorphism:

Polymorphism allows objects of different types to be treated as objects of a common superclass.

Method overriding is a form of polymorphism where a subclass provides a specific implementation of a method that is already defined in its superclass.

Method overloading is another form of polymorphism where multiple methods can have the same name but different parameter lists within the same class.

Method Overriding Example:

// Base class
open class Animal {
// Method to make sound
open fun makeSound() {
println("Animal makes a sound")
}
}

// Subclass Dog inheriting from Animal
class Dog : Animal() {
// Override makeSound method
override fun makeSound() {
println("Dog barks")
}
}

// Subclass Cat inheriting from Animal
class Cat : Animal() {
// Override makeSound method
override fun makeSound() {
println("Cat meows")
}
}

fun main() {
val dog = Dog()
val cat = Cat()

// Polymorphism: Same method name, different behavior based on the object type
val animals = arrayOf(dog, cat)
for (animal in animals) {
animal.makeSound() // Output depends on the actual type of the object
}
}

Method Overloading Example:

class Calculator {
// Method to add two integers
fun add(a: Int, b: Int): Int {
return a + b
}

// Method to add three integers
fun add(a: Int, b: Int, c: Int): Int {
return a + b + c
}

// Method to add two doubles
fun add(a: Double, b: Double): Double {
return a + b
}
}

fun main() {
val calculator = Calculator()

// Using the add method with two integers
val sum1 = calculator.add(5, 3)
println("Sum of 5 and 3 is $sum1")

// Using the add method with three integers
val sum2 = calculator.add(5, 3, 2)
println("Sum of 5, 3, and 2 is $sum2")

// Using the add method with two doubles
val sum3 = calculator.add(2.5, 3.7)
println("Sum of 2.5 and 3.7 is $sum3")
}

Abstraction:

Abstraction is the concept of hiding the implementation details and showing only the necessary features of an object.

In Kotlin, abstraction is achieved using abstract classes and interfaces.

Abstract classes are declared with the abstract keyword and can have abstract (unimplemented) methods.

Interfaces define a contract for implementing classes and can contain abstract methods as well as method implementations (default methods).

abstract class Shape {
abstract fun draw()
}

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

interface Drawable {
fun draw()
}

#KotlinProgramming #AndroidDevLife #GradleBuilds #AndroidApps #KotlinCoder #GoogleAndroidDev #KotlinLove #AndroidStudio #SoftwareDevelopmentLifeCycle #LinkedInTips #KotlinCommunity #AndroidCoding #GradleConfig #KotlinCode #AndroidSDK #GradleDependencies #AndroidDevelopmentTips #KotlinExperts #KotlinApps #AndroidLife #GoogleDevelopersGroup #GradlePlugin #Java #Activity #AndroidActivity #AndroidApp #SOLID

--

--