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
Class & Object
Kindly go through my article, "Understanding Classes in Kotlin" to learn how to implement classes and objects in Kotlin.
Inheritance
For 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))
- }
-
-
-
-
-
Explanation
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 Overloading
You 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)
- }
-
Binary Operator OverloadingBinary 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.
- data class BinaryOperatorOverloading(var result:String){
- operator fun plus(number:Long):BinaryOperatorOverloading{
- return BinaryOperatorOverloading(result+number)
- }
- }
- fun main(args:Array<String>){
- var boolobj=BinaryOperatorOverloading("WhatsApp No.:")
- println(boolobj.plus(8882265032))
- }
-
-
In the above example, you can see ‘BinaryOperatorOverloading’ class which has a function ‘plus’ being defined as how this function will work.
Dynamic Polymorphism
This is also known as runtime polymorphism or late binding – this is used to decide which block of code of a function will be executed after the function call at the runtime of the program. Let us see with an example.
- open class Car(var speed:Int){
- open fun show(){
- println("car is running on $speed km/hrs")
- }
- }
- class Mercedize(var mspeed:Int) : Car(mspeed){
- override fun show(){
- println("Mercedize is running on $mspeed km/hrs")
- }
- }
- fun main(args:Array<String>){
- var newcar=Mercedize(150);
- println(newcar.show())
- }
-
-
-
Abstraction
Abstraction is a technique to show the most important information and hiding less important information in order to reducing the complexity. We can use Abstract classes and interface too for this purpose.
Abstract class
It has the abstract and concurrent methods. An abstract class can’t initiate but it can be inherited. You can create and use abstract class in following way.
- abstract class Policy{
- abstract fun dept()
- fun operations(){
- println("It softwares")
- }
- }
- class Company:Policy(){
- override fun dept() {
- println("Departmental Polycies")
- }
- }
- fun main(args:Array<String>){
- var com=Company()
- com.operations()
- com.dept()
- }
-
-
InterfaceInterface contains method without definition (abstract methods). You have to implement these methods of the interface in class. Interface in Kotlin is
- interface Furniture{
- fun show()
- fun message(){
- println("This is concurrent method message")
- }
- }
- class Chair : Furniture{
- override fun show() {
- println("This is show method message")
- }
- }
- fun main(args:Array<String>){
- var wheel=Chair()
- wheel.message()
- wheel.show()
- }
-
-
-
-
-
In this example, you can judge that the working of interface is very close to abstract classes. The only difference between abstract class and interface is that interface does not maintain the state while an abstract class maintains. It means that you cannot initialize a variable in an interface.