BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin Abstract Class with examples

Last Updated: March 11, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

An abstract class cannot be instantiated, which means we cannot create the object of an abstract class. Unlike other class, an abstract class is always open so we do not need to use the open keyword.

Points to Note:
1. We cannot create the object of an abstract class.
2. Property and member function of an abstract class are by default non-abstract. If you want to override these in the child class then you need to use open keyword for them.
3. If a member function is abstract then it must be implemented in the child class. An abstract member function doesn’t have a body only method signature, the implementation is done in the child class.

Abstract class Example

In the following example we have an abstract class Student, we cannot create an object of this class. However we can inherit this class, like we did in the following example. Class College inherits abstract class Student.

The function func() is an abstract function which means it doesn’t have a body and only method signature, however since it is an abstract function, it must be overriden in the child class.

abstract class Student(name: String, age: Int) {

    init {
        println("Student name is: $name")
        println("Student age is: $age")
    }

    //non-abstract function
    fun demo() {
        println("Non-abstract function of abstract class")
    }

    //abstract function
    abstract fun func(message: String)
}

class College(name: String, age: Int): Student(name, age) {

    override fun func(message: String) {
        println(message)
    }
}

fun main(args: Array<String>) {
    val obj = College("Chaitanya", 31)
    obj.func("I'm a Blogger")
    obj.demo()
}

Output:
Kotlin abstract class

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Class and Objects – Object Oriented Programming (OOP)
  2. Kotlin Constructors with examples
  3. Kotlin Visibility Modifiers – public, private, protected and internal
  4. Kotlin Nested and Inner Class with examples
  5. Kotlin Try as an expression in Exception handling

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap