Skip to main content

Kotlin Abstract Class

Abstract class is defined by abstract keyword. Abstract is cannot be instantiated. That means this class are not possible to create any objects. But that is like a special class. That are allowed to create abstract and non abstract method of this class. Abstract method should be overwritten by inherited class (subclass,child class). That is speciality of this class that there is by default open. There is syntax as follows.

abstract class className(variable:Type){//header properties (optional)

//Abstract and non Abstract methodes


For example.

abstract class Game(val type:String,val teamName:String,val totalPlayer:Short){//Non Abstract parameter

  //abstract properties
  abstract var result:String
  abstract fun matchResult()

  //non abstract properties
  fun details(){
    //display basic information
    println("Game Type :$type ")
    println("Game Of : $teamName ")
    println("Total Player : $totalPlayer Each Team")
class Cricket( teamName:String,
  override var result:String): Game("Cricket",teamName,11){

  override fun matchResult(){
    println("Result of match : $result")
class Hockey(
  override var result:String): Game("Hockey",teamName,11){
  var timeOut:Short=0
    //overwrite abstract method
  override fun matchResult(){
    println("Wao this match is win by : $result")
  fun getRemainingTime():Long{
    return (timeOut*60).toLong()
  fun setRemainingTime(timeLimit:Short=0){
    timeOut=timeLimit // minutes
fun main(args: Array<String>) {
  val cricket=Cricket("Ind vs Pak","India win by 5 wicket")
  val hockey=Hockey("Aus vs Ned","Aus")


  println("${hockey.getRemainingTime()} Second Remaining")


Game Type :Cricket 
Game Of : Ind vs Pak 
Total Player : 11 Each Team
Result of match : India win by 5 wicket
Game Type :Hockey 
Game Of : Aus vs Ned 
Total Player : 11 Each Team
120 Second Remaining
Wao this match is win by : Aus

In this example Game is an abstract class. Which are define abstract methods and fields. And here two class Cricket and Hockey are inherit this class. so this abstract method definition is overwritten by this derived classes.

This method overwrite is similar to pure virtual function in c++ programming.


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment