Like other OOP languages, we can implement all the features of OOP in Kotlin too. In this article, I am going to show how to implement all the features.These features are,
- Class
- Object
- Inheritance
- Polymorphism
- Abstraction
- Interface
Kindly go through my article, "Understanding Classes in Kotlin" to learn how to implement classes and objects in Kotlin.
InheritanceFor inheritance also, refer to my previous article - Code Reusability In Kotlin
Polymorphism
When any variable, function, or object has more than one forms, this concept is known as polymorphism. There are two types of polymorphism.

Static polymorphism is also known as compile time polymorphism. Let us understand it by method overloading.
Method Overloading
When methods have the same name but different signature, which are used to performing different kinds of operations.
- class MethodOverloading{
- fun area(a:Int):Int{
- return a*a
- }
- fun area(length:Int,height:Int):Int{
- return length*height
- }
- fun area(base:Float,height:Float):Float{
- return (base*height)/2
- }
- }
- fun main(args:Array<String>){
- var obj=MethodOverloading()
- println("Area of Square="+obj.area(5))
- println("Area of Rectangle="+obj.area(5,4))
- println("Area of Triangle="+obj.area(10.05f,5.5f))
- }
- /*output:
- Area of Square=25
- Area of Rectangle=20
- Area of Triangle=27.6375
- */
In the above example, all these three methods are having the same name but they are used to calculate the areas of a square, a rectangle, and a triangle respectively.
Operator Overloading
There are two categories to perform operator overloading – 1. Unary Operator Overloading and 2. Binary Operator Overloading. Kotlin provides the specific name of functions associated with the operators to overload them, like – dec, unaryPlus, plus, plusAssign, div, divAssign, and equals etc. Let’s see how you can perform these operations.
Unary Operator OverloadingYou can use Unary increment and decrement operators in your own way by overloading them. Even those will have their same precedence.
Example
Let‘s understand unary decrement operator overloading with an example.
- data class Unary(var number : Int)
- {
- operator fun dec( ) : Unary{
- var newnum=this.number - 1
- return Unary(newnum)
- }
- }
- fun main(args:Array<String>){
- var unaryobj=Unary(5)
- println(--unaryobj)
- }
- //Unary(number=4)
Binary operator overloading is similar to Unary Operator overloading; the only difference is that the binary operators require at least two operands whereas the unary operators require only one.
Example
Here is an example of Binary Operator overloading to add a string with a number.

Join the conversation! Your thoughts help the community grow.