Kotlin Abstract Class

Kotlin Abstract Class is one of the way to implement abstraction in Kotlin. Following is a sample of how a Kotlin Abstract Class looks.

*regular meaning the variable/function is initialized/defined.

About Kotlin Abstract Class

Let us consider following simple example that demonstrates the usage of Abstract class, and then draw the list of points about Koltin Abstract Class.

From the above example

  • abstract keyword is used to declare an abstract class.
  • Abstract classes cannot be instantiated.
    Error:(2, 27) Kotlin: Cannot create an instance of an abstract class
  • Abstract classes can be inherited.
  • Variables and functions of an abstract class could be declared either as abstract or not.
  • Abstract variables are not initialized. Abstract methods does not have an implementation.
  • In the inherited class, unless it is also an abstract class, variables and functions, not declared as abstract are not mandatory to override. Abstract variables and abstract functions must be overridden.
  • To override non-abstract methods, they must be declared open. You may refer Kotlin Inheritance for a detailed example.

Conclusion

With Kotlin Interfaces and Kotlin Abstract Classes, we have learnt how to implement Abstraction in Kotlin.