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)

    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.

[sociallocker id="958"]
abstract class account (val holder: String, val amount: Double) {
    open fun print () {
        println ("Holder: $holder")
        println ("Amount: $amount")

class Cashier (holder: String, amount: Double): account (holder, amount) {
    override fun print () {
        println ("Savings Account")
        super.print ()

class Accountname (holder: String, amount: Double, val term: Int, val interest: Double): account (holder, amount){
    override fun print() {
    println ("Fixed Term Account")
    println ("Term in days: $term")
    println ("Interests: $interest")
    val gain = amount * interest / 100
    println ("Amount of interest: $gain")

fun main (parameter: Array <String>) {
    val Cashier1 = Cashier ("juan", 10000.0)

    val Accountname1 = Accountname ("ana", 5000.0, 30, 1.23)


How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!