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 the 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 headphone jack in the phones and radios, or the different chargers for different mobiles, which were available till 2008 (approximately, i donot know the exact year). Previously we had different phones with different chargers and different headphones jacks

After 2008 most of he 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 interchangeable use the chargers and the headphones.

Like you can use samsung headphone with onePlus mobile

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

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

Kotlin provides Interface and Abstract class to achieve this abstraction.

Inheritance in kotlin

Interface in Kotlin

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

Interface works like a blue print, 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 subclass of Interface.

Interfaces are used 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 the all the abstract methods present in the interface but the behavior of the methods could be on 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 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 implementation for 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 object to a abstract class, but we can create 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 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()
}

Kotlins Enums as Anonymous classes

aaaaaaaaaaaaa
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group