Abstract functions

An abstract function is a function without a function body. To understand this concept, let's take the example of the Shape class:

abstract class Shape(val name: String) {
init {
println("Drawing $name")
}
open fun draw(){}
open fun getArea() : Double{ return 0.0 }
}

The Shape abstract class contains two normal functions: draw and getArea. Both functions are open and available for inheritance. Having non-abstract functions in an abstract class, however, does have some drawbacks. If a function is a normal or non-abstract function, it must provide a function body even if it is empty. If a child class inherits a parent class, it won't know whether it is necessary to provide an implementation of the inherited functions.

Abstract functions are similar to abstract classes. They do not contain a function body. The biggest advantage of declaring an abstract function is that any class that inherits the parent class has no choice but to provide the implementation of this abstract function. By declaring an abstract function in the parent class, we can force the child classes to use similar signatures with their own implementation.

Shape is an abstract class because it is a generic idea. The draw and getArea functions must also be abstract, because we cannot draw a generic shape. Remove the open keyword from the function body and add the abstract keyword at the beginning of the function signature:

abstract class Shape(val name: String) {
init {
println("Drawing $name")
}
abstract fun draw()
abstract fun getArea():Double
}

Now, each child class has to provide the implementation of all abstract functions of the parent class. In our shape example, all shapes are different to other shapes but each shape must implement two functions: getArea(), to calculate the area, and draw(), to draw the shape on the screen:

class Rectangle(_width : Double, _height : Double, name: String) : Shape( name) {}

As soon as the child class extends the abstract class with the abstract function, the compiler will immediately trigger a compile-time error:

'Rectangle' is not abstract and does not implement abstract base class member public abstract fun draw(): 

This is an indication that the child class has to provide the implementation of all abstract functions that are listed in the parent class.

 There are a few key things to remember with regard to abstract classes:

  • Abstract classes are like normal classes. They contain properties, functions, and constructors. Abstract classes, however, cannot be instantiated.
  • Abstract classes are open by default.
  • Only abstract classes can have abstract functions.
  • Abstract functions are declared without a function body.
  • Abstract functions cannot be declared privately.

Abstract functions must be implemented by child classes. This ensures that we conform to compile-time safety, which means that any class that extends the abstract class must provide an implementation to continue. If the child class does not provide the implementation, the function must be declared as an abstract function so that another child class can provide the implementation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.233