Abstraction is a fundamental concept in object-oriented programming languages. It defines an object’s essential properties without requiring a complete function body within a class. In Kotlin, abstraction can be achieved through abstract classes and interfaces. In today’s tutorial, we will see an example of how to apply abstraction in Kotlin.
Abstraction in Kotlin: Using Abstract Classes and Interfaces
Difference between Abstract class and Interface:
- Abstract classes: Represents a base class that provides a blueprint for a subclass in Kotlin. An abstract class can only be inherited into one subclass and support a single inheritance. An abstract class can have both abstract(no implementation) and non-abstract methods(with implementation). It supports a constructor to initialize properties.
- Interface classes: It Allows us to define methods that a class should implement but there is no need to define actual implementation. In Kotlin classes can inherit multiple interfaces. This is called multiple inheritance. It does not support constructors to initialize properties. All the member functions are public by default and we can make them protected.
Example of Abstract class in Kotlin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// Abstract Class with Parameter // This is Super Class - Base Class abstract class StudentCommon(val sirName: String) { // Abstract method: each subclass must implement this abstract fun goodMorningSir() // Concrete method: shared by all subclasses fun goodNightSir() { println("Good Night $sirName Sir") } } // This is SubClass - Derived Class class Student(sirName: String) : StudentCommon(sirName) { override fun goodMorningSir() { println("Good Morning Sir") } } fun main(){ // Usage val studentObj = Student("Jim") studentObj.goodMorningSir() studentObj.goodNightSir() } // Output // Good Morning Sir // Good Night Jim Sir |
Example of Interface class in Kotlin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// Interface Class // This is Super Class - Base Class interface StudentCommon { fun goodMorningSir() fun goodNightSir() } // Interface Class // This is Super Class - Base Class interface ClassCommon { fun showClassCount() fun showClassBooks() } // This is SubClass - Derived Class class Student() : StudentCommon, ClassCommon { override fun goodMorningSir(){ println("Good morning sir function called") } override fun goodNightSir(){ println("Good Night sir function called") } override fun showClassCount(){ println("Good show class count function called") } override fun showClassBooks(){ println("Good show class books function called") } } fun main(){ val studentObj = Student() studentObj.goodMorningSir() studentObj.goodNightSir() studentObj.showClassCount() studentObj.showClassBooks() } // Output // Good morning sir function called // Good Night sir function called // Good show class count function called // Good show class books function called |
Conclusion:
Both abstract and interface classes have their unique usages and completely depend on our usages. If we want to define a function with definitions and want to inherit into a single class then the Abstract class is a good choice. But If we want to make common utils functions without their definitions and want to implement multiple inheritance then Interface is a good choice. So keep practicing, Happy coding.