Object-Oriented Programming (OOP) is a paradigm that organizes code into "objects" that combine data and methods. Kotlin fully supports OOP principles like classes, inheritance, and polymorphism.
In Kotlin, a class is a blueprint for creating objects. You can define properties and methods inside a class, and then instantiate objects from that class.
// Define a class class Car(val make: String, val model: String) { fun drive() { println("Driving the $make $model") } } // Create an object from the class val myCar = Car("Toyota", "Camry") myCar.drive() // Output: Driving the Toyota Camry
Constructors are used to initialize the objectโs properties when it's created. In Kotlin, you can define a primary constructor directly in the class header.
// Class with a primary constructor class Person(val name: String, val age: Int) { fun greet() { println("Hello, my name is $name and I am $age years old.") } } val person1 = Person("Alice", 25) person1.greet() // Output: Hello, my name is Alice and I am 25 years old.
Inheritance allows one class to inherit properties and methods from another class. In Kotlin, inheritance is implemented using the open
keyword.
// Base class open class Animal(val name: String) { fun speak() { println("$name makes a sound.") } } // Derived class inherits from Animal class Dog(name: String) : Animal(name) { fun bark() { println("$name is barking!") } } val dog = Dog("Buddy") dog.speak() // Output: Buddy makes a sound. dog.bark() // Output: Buddy is barking!
Polymorphism allows methods to behave differently based on the object calling them. In Kotlin, polymorphism is achieved through method overriding.
// Base class with open function open class Shape { open fun draw() { println("Drawing a shape") } } // Derived class overrides the draw function class Circle : Shape() { override fun draw() { println("Drawing a circle") } } // Derived class overrides the draw function class Square : Shape() { override fun draw() { println("Drawing a square") } } val circle = Circle() val square = Square() circle.draw() // Output: Drawing a circle square.draw() // Output: Drawing a square
Abstraction allows you to hide complex implementation details and expose only essential features. In Kotlin, abstraction is implemented using abstract
classes and abstract
methods.
// Abstract class abstract class Vehicle { abstract fun start() } // Derived class provides implementation of the abstract method class Car : Vehicle() { override fun start() { println("Car is starting.") } } val myCar = Car() myCar.start() // Output: Car is starting.
An interface defines a contract that implementing classes must fulfill. In Kotlin, interfaces can contain method declarations without a body.
// Interface interface Drivable { fun drive() } // Class implements the interface class Car : Drivable { override fun drive() { println("The car is driving.") } } val car = Car() car.drive() // Output: The car is driving.
Here are some challenges for you to practice:
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!