In this tutorial, we will be covering the basics of Object-Oriented Programming (OOP) using Kotlin. Our goal is to understand the key features of OOP and how they can be applied in Kotlin.
By the end of this tutorial, you will learn about:
Prerequisites: Basic understanding of Kotlin syntax and programming concepts.
In OOP, a class is a blueprint for creating objects (a particular data structure). An object is an instance of a class.
class Car { // defining a class
var color: String = "Red" // property of class
}
fun main() {
val myCar = Car() // creating an object
println(myCar.color) // accessing property
}
Inheritance is a mechanism where you can derive a class from another class for a hierarchy of classes. The derived class inherits the members of the base class.
open class Vehicle { // base class
open fun ride() { println("Riding a vehicle") }
}
class Bike : Vehicle() { // derived class
override fun ride() { println("Riding a bike") }
}
fun main() {
val myBike = Bike()
myBike.ride() // accessing overridden method
}
Polymorphism allows us to perform a single action in different ways. In Kotlin, we can achieve this by method overriding and interfaces.
open class Animal {
open fun sound() { println("The animal makes a sound") }
}
class Dog : Animal() {
override fun sound() { println("The dog barks") }
}
fun main() {
val myDog: Animal = Dog() // polymorphic declaration
myDog.sound() // accessing overridden method
}
Abstraction hides the internal details and shows only functionalities. In Kotlin, we achieve abstraction using interfaces and abstract classes.
interface Vehicle {
fun ride() // abstract method
}
class Bike : Vehicle {
override fun ride() { println("Riding a bike") }
}
fun main() {
val myBike: Vehicle = Bike() // accessing method through interface
myBike.ride()
}
Encapsulation hides the data from outside the class. In Kotlin, we achieve encapsulation using private visibility modifiers.
class Car(private var color: String) { // private property
fun displayColor() { println("Color: $color") } // method to access private property
}
fun main() {
val myCar = Car("Blue")
myCar.displayColor() // accessing private property using method
}
In the previous section, we have seen different concepts of OOP. Let's see a practical example of how all these concepts can be combined.
interface Animal {
fun sound() // abstract method
}
class Dog : Animal {
private var color: String = "Brown" // encapsulated property
override fun sound() {
println("The $color dog barks") // accessing private property
}
}
fun main() {
val myDog: Animal = Dog() // polymorphic declaration
myDog.sound() // accessing overridden method
}
When you run the program, the output will be: The Brown dog barks
In this tutorial, we have learned about the key concepts of OOP in Kotlin:
To learn more about OOP in Kotlin, you can refer to the official Kotlin documentation.
Exercise 1: Create a Person
class with name
and age
properties and a display()
function.
Solution:
```kotlin
class Person(var name: String, var age: Int) {
fun display() { println("Name: $name, Age: $age") }
}
fun main() {
val person = Person("John", 25)
person.display()
}
```
Exercise 2: Create a Shape
interface with an area()
function. Implement this interface in Circle
and Rectangle
classes.
Solution:
```kotlin
interface Shape {
fun area(): Double
}
class Circle(private val radius: Double) : Shape {
override fun area() = Math.PI * Math.pow(radius, 2.0)
}
class Rectangle(private val length: Double, private val breadth: Double) : Shape {
override fun area() = length * breadth
}
fun main() {
val circle: Shape = Circle(3.0)
println("Circle Area: ${circle.area()}")
val rectangle: Shape = Rectangle(4.0, 5.0)
println("Rectangle Area: ${rectangle.area()}")
}
```
Continue practicing to get better at Kotlin and OOP concepts. Happy coding!