Lesson: 34 OOP – inheritance – abstract classes in kotlin language

Lesson: 34 OOP – inheritance – abstract classes in kotlin language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

In some situations we have methods and properties common to a set of classes, we can group these methods and properties into an abstract class.

There is a special syntax in Kotlin to indicate that a class is abstract.
You can not define objects of an abstract class and surely will be inherited by other classes of which we can define objects.

Problem 1:

Declare an abstract class that represents an Operation. Define in the same three properties value1, value2 and result, and two methods: calculate and print.
Put two classes called Sum and Subtraction that inherit from the Operation class.

Project140 – Principal.kt

abstract class Operation (val value1: Int, val value2: Int) {
    protected var result: Int = 0

    abstract fun operate ()

    fun print () {
        println ("Result: $ result")
    }
}

class Sum (value1: Int, value2: Int): Operation (value1, value2) {
    override fun operate () {
        result = value1 + value2
    }
}

class Subtraction (value1: Int, value2: Int): Operation (value1, value2) {
    override fun operate () {
        result = value1 - value2
    }
}

fun main (parameter: Array <String>) {
    val sum1 = Sum (10, 4)
    sum1.operate()
    sum1.print()

    val sub1 = Subtraction (20, 5)
    sub1.operate ()
    sub1.print ()
}

An abstract class is declared by preceding the abstract keyword to the keyword class:

Abstract class Operation (val value1: Int, val value2: Int) {

We can define properties that will be inheritable:

    Protected var result: Int = 0

We can declare abstract methods that force the classes that inherit from it to its implementation (this helps to unify the functionalities of all its subclasses, it makes sense that every class that inherits from the class Operation implements a method to operate):

    Abstract fun operate ()

An abstract class can also have concrete, ie implemented, methods:

    Fun print () {
        Println ("Result: $result")
    }

The syntax for declaring that a class inherits from an abstract class is the same thing we saw in the previous concept:

Class Sum (value1: Int, value2: Int): Operation (value1, value2) {

What if it is obligatory to implement the abstract method to operate, in case it is not done a syntactic error appears:

    Override fun operate () {
        Result = value1 + value2
    }

In the main function we can define objects of the key Sum and Subtract, but we can not define objects of the class Operation since it is abstract:

Fun main (parameter: Array <String>) {
    Val sum1 = Sum (10, 4)
    Sum1.operate ()
    Sum1.print ()
    Val sub1 = Subtraction (20, 5)
    sub1.operate ()
    sub1.print ()
}

Proposed problem

  • Declare an abstract class Account and two subclasses. Define the common properties and methods between a saving box and a fixed term and group them into the Account class.
    A savings and a fixed term have a name of the holder and an amount. A fixed term adds a term of taxation in days and an interest rate. Make the savings bank do not generate interest.
    In the main function of the program define an object of the class SafeCase and another of the class FixCode.
Solution

 

About The Author

Related posts

Leave a Reply