Abstraction in Kotlin

Okay Guys, Forgive me for the information I am about to talk because I am not able to accept the descriptions present in the Internet about Abstraction.

Internet Says (almost all websites) : Hiding internal details and showing functionality is known as abstraction., I do not Agree with this because this is what Encapsulation. isn't it ?

I could be wrong in my description, but I will continue with my perception if you are not comfortable please skip this topic

Abstraction is a process of giving an set of guidelines to the user, who are going to implement these guidelines based on their needs.

For Example, I hope you are aware of the headphone jack in the phones and radios, or the different chargers for different mobiles, which were available till 2008 (approximately, I do not know the exact year). Previously we had different phones with different chargers and different headphones jacks

After 2008 most of the companies came to an agreement that everyone should manufacture the Phones based on few specifications, which included headphone, charger ports(except Apple).

So they have concluded with 3.5mm jack and standard charger port, now you can interchangeably use the chargers and the headphones.

Like you can use Samsung headphone with onePlus mobile

Similar to the above scenario, when an architect designs a framework, or a library or software, he will give specifications/guidelines.

If somebody wants to take advantage of the framework/library they have to implement those specifications or guidelines.

Kotlin providesInterface and Abstract classto achieve thisabstraction.



Inheritance in kotlin

Interface in Kotlin

The interface defines a set of functions as a contract or as guidelines. The class that is implementing this interface must implement these functions in the concrete class.

The Interface works like a blueprint, Interfaces will not have any functions implementations.

Interface is defined using the interface keyword

// interface
interface SampleInterface{
    // abstract method
    fun someFunction();
}

The interfaces add value when you are building a larger system or a library. Whenever there is interface there is Inheritance, as the class which change/ overrides the interface method must be a subclass of Interface.



Interfaces are used to provide the standardization in kotlin, Now if any class want to implement the interface then that class should provide the implementation of the abstract methods present in the Interface.

You must implement all the abstract methods present in the interface but the behavior of the methods could be in your own way.

// interface
interface SampleInterface{
    // abstract method
    fun someFunction();
}
class DerivedClass1:SampleInterface{
    override fun someFunction() {
        // some calculation or some code
        println("implemented a interface method")
    }
}
fun main(args: Array<String>) {
    val derived = DerivedClass1()
    derived.someFunction()
}

With the interface, we can make sure that all the classes implement a particular interface so that every class will adhere to interface functions, apart from interface function, the classes can have their own functions.

// interface
interface Button{
    // abstract method
    fun clicked();
}
class Phone:Button{
    override fun clicked() {
        println("Phone Button clicked")
    }
}
class Laptop:Button{
    override fun clicked() {
        println("Laptop Button clicked")
    }
}
fun main(args: Array<String>) {
    val derivedPhone = Phone()
    derivedPhone.clicked()
    val derivedLaptop = Laptop()
    derivedLaptop.clicked()
}

Sealed Classes in kotlin

Abstract Classes in Kotlin

We use abstract classes when we know the functions, but we don’t know how they would be implemented.

There are very few instances where implementing an empty function in Kotlin is a good idea. The reason is, if someone were using our class, they wouldn’t know they needed to provide an implementation.

When we mark the method as abstract, anyone using our class has no choice but to provide an implemented child class. They can’t instantiate the class otherwise.

This is called compile-time safety. Compile-time safety ensures that any classes that extend our abstract class must provide the bare minimum functionality to work.

Then why cannot I use Interface instead of Abstract class, you cannot because if you want to provide an implementation for a few functions (concrete functions) then you must use Abstract class

Abstract method : method with no implementation

Concrete Method : method with implementation

We can override both abstract and concrete functions in subclasses, but we cannot create an object to an abstract class, but we can create an object to the subclass which overrides the abstract class completely.

Abstract classes are defined using the abstract keyword, abstract function in Abstract class must have the abstract keyword in function declaration so abstract functions.
open abstract class Phone{
    abstract fun call()
    fun wiFi(){
        println("this has wifi")
    }
}
class Mobile:Phone(){
    override fun call() {
        println("Dialing thru qwerty keys")
    }
}
fun main(args: Array<String>) {
    val derivedPhone = Mobile()
    derivedPhone.call()
    derivedPhone.wiFi()
}

Kotlin's Enums as Anonymous classes



Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions