what is abstract class in kotlin

An abstract class in Kotlin is a class that cannot be instantiated directly. It may contain abstract and non-abstract (concrete) members. Abstract members do not have a body and must be implemented by non-abstract subclasses.

Here are the key points about abstract classes in Kotlin:

  1. Declaration: To declare an abstract class, use the abstract keyword.

kotlin abstract class Shape {

  1. Abstract Members: Abstract classes can declare abstract members, which are properties or functions without an implementation.

kotlin abstract class Shape { abstract fun calculateArea(): Double

  1. Concrete Members: Abstract classes can also have concrete members with implementations.

```kotlin abstract class Shape { abstract fun calculateArea(): Double

   fun displayArea() {
       println("Area: ${calculateArea()}")
   }

```

  1. Inheritance: Abstract classes can be inherited by other classes using the :, and subclasses must provide implementations for all abstract members.

kotlin class Circle(val radius: Double) : Shape() { override fun calculateArea(): Double { return 3.14 radius radius }

  1. Instance Creation: Abstract classes cannot be instantiated directly, but instances can be created through subclass objects.

kotlin val circle = Circle(5.0) circle.displayArea()

  1. Abstract Properties: Abstract classes can also have abstract properties, which are declared without an initial value.

kotlin abstract class Shape { abstract val color: String

  1. Abstract Property Initialization: Abstract properties must be initialized in the subclass.

kotlin class Circle(override val color: String) : Shape() { // other members...

  1. Constructor in Abstract Class: Abstract classes can have constructors, and they are called when a subclass instance is created.

kotlin abstract class Shape(val name: String) { // other members...

  1. Superclass Initialization: Subclasses must call the constructor of the abstract superclass using the super keyword.

kotlin class Circle(radius: Double, color: String) : Shape("Circle") { // other members...

In summary, an abstract class in Kotlin is declared using the abstract keyword, may contain abstract and concrete members, and is meant to be inherited by subclasses, which must provide implementations for abstract members. Abstract classes cannot be instantiated directly.