Bird
Raised Fist0
Kotlinprogramming~10 mins

Abstract classes and methods in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Abstract classes and methods
Define abstract class
↓
Declare abstract method(s)
↓
Create subclass
↓
Implement abstract method(s)
↓
Instantiate subclass and use methods
An abstract class defines a template with abstract methods that subclasses must implement before creating objects.
Execution Sample
Kotlin
abstract class Animal {
    abstract fun sound(): String
}

class Dog : Animal() {
    override fun sound() = "Bark"
}

fun main() {
    val dog = Dog()
    println(dog.sound())
}
Defines an abstract class Animal with an abstract method sound, then Dog implements sound and prints its result.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with abstract method sound()Class Animal created with abstract methodNo instance can be created from Animal
2Define subclass Dog inheriting AnimalDog class createdDog must implement sound()
3Implement sound() in Dog returning "Bark"sound() overriddenDog is concrete and instantiable
4Create instance dog of Dogdog = Dog()dog object created
5Call dog.sound()dog.sound() returns "Bark"Output: Bark
💡 Execution ends after printing the dog's sound.
Variable Tracker
VariableStartAfter Step 4After Step 5
dogundefinedDog instance createdDog instance unchanged
Key Moments - 3 Insights
Why can't we create an instance of the abstract class Animal?
Because Animal has an abstract method without implementation (see execution_table step 1), Kotlin prevents creating objects from it to ensure all abstract methods are implemented.
What happens if Dog does not implement the abstract method sound()?
Dog would remain abstract and cannot be instantiated (see execution_table step 2 and 3). Kotlin requires all abstract methods to be implemented in concrete subclasses.
Why do we override sound() in Dog?
To provide the actual behavior for the abstract method declared in Animal (see execution_table step 3), making Dog a concrete class that can be used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when dog.sound() is called at step 5?
A"Meow"
B"Bark"
CCompilation error
DNo output
💡 Hint
Refer to execution_table row 5 where dog.sound() returns "Bark".
At which step is the Dog class confirmed to be instantiable?
AStep 3
BStep 4
CStep 1
DStep 2
💡 Hint
Check execution_table step 3 where sound() is overridden making Dog concrete.
If we remove the override of sound() in Dog, what changes in the execution table?
Adog.sound() returns null
Bdog object is created but sound() throws error
CDog becomes abstract and cannot be instantiated at step 4
DNo change, program runs normally
💡 Hint
Refer to key_moments about implementing abstract methods and execution_table steps 2 and 3.
Concept Snapshot
abstract class ClassName {
    abstract fun methodName(): ReturnType
}

class SubClass : ClassName() {
    override fun methodName() = implementation
}

- Abstract classes cannot be instantiated.
- Abstract methods have no body and must be overridden.
- Subclasses must implement all abstract methods to be concrete.
Full Transcript
This example shows how Kotlin uses abstract classes and methods. First, an abstract class Animal is defined with an abstract method sound. This method has no body, so Animal cannot be instantiated. Then, a subclass Dog inherits Animal and overrides the sound method to return "Bark". Because Dog implements all abstract methods, it becomes a concrete class that can be instantiated. Finally, we create a Dog object and call its sound method, which prints "Bark". This flow ensures that abstract classes provide a template, and subclasses provide specific behavior before objects are created.

Practice

(1/5)
1. What is true about an abstract class in Kotlin?
easy
A. It cannot be instantiated directly and may contain abstract methods.
B. It can be instantiated like any other class.
C. It must not contain any methods with a body.
D. It is the same as an interface.

Solution

  1. Step 1: Understand abstract class instantiation rules

    Abstract classes cannot create objects directly because they are incomplete by design.
  2. Step 2: Check method types in abstract classes

    Abstract classes can have both abstract methods (without body) and concrete methods (with body).
  3. Final Answer:

    It cannot be instantiated directly and may contain abstract methods. -> Option A
  4. Quick Check:

    Abstract class = no direct objects + can have abstract methods [OK]
Hint: Abstract classes can't create objects directly [OK]
Common Mistakes:
  • Thinking abstract classes can be instantiated
  • Believing abstract classes can't have method bodies
  • Confusing abstract classes with interfaces
2. Which of the following is the correct way to declare an abstract method in a Kotlin abstract class?
easy
A. abstract fun calculate(): Int
B. fun abstract calculate(): Int
C. fun calculate() abstract: Int
D. abstract calculate fun(): Int

Solution

  1. Step 1: Recall Kotlin syntax for abstract methods

    In Kotlin, abstract methods are declared with the abstract keyword before fun, followed by the method signature without a body.
  2. Step 2: Check each option's syntax

    Only abstract fun calculate(): Int matches the correct syntax: abstract fun calculate(): Int. Others have incorrect keyword order or punctuation.
  3. Final Answer:

    abstract fun calculate(): Int -> Option A
  4. Quick Check:

    abstract fun method() = correct syntax [OK]
Hint: abstract keyword comes before fun in method declaration [OK]
Common Mistakes:
  • Placing abstract after fun
  • Adding a method body to abstract methods
  • Incorrect keyword order
3. What will be the output of this Kotlin code?
abstract class Animal {
    abstract fun sound(): String
}

class Dog : Animal() {
    override fun sound() = "Bark"
}

fun main() {
    val dog = Dog()
    println(dog.sound())
}
medium
A. Animal
B. sound
C. Bark
D. Compilation error

Solution

  1. Step 1: Understand class inheritance and method override

    The abstract class Animal has an abstract method sound(). The subclass Dog overrides it to return "Bark".
  2. Step 2: Analyze main function output

    Creating a Dog object and calling sound() prints "Bark".
  3. Final Answer:

    Bark -> Option C
  4. Quick Check:

    Overridden method returns "Bark" [OK]
Hint: Overridden abstract method returns subclass value [OK]
Common Mistakes:
  • Expecting abstract class method to run
  • Confusing method name with output
  • Thinking abstract class can be instantiated
4. Identify the error in this Kotlin code:
abstract class Vehicle {
    abstract fun drive()
}

class Car : Vehicle() {
    fun drive() {
        println("Driving")
    }
}
medium
A. drive method in Vehicle should have a body
B. Vehicle class cannot be abstract
C. Car class cannot inherit from Vehicle
D. Missing override keyword in Car's drive method

Solution

  1. Step 1: Check method overriding rules in Kotlin

    When a subclass implements an abstract method, it must use the override keyword.
  2. Step 2: Identify missing override keyword

    The Car class defines drive() without override, causing a compilation error.
  3. Final Answer:

    Missing override keyword in Car's drive method -> Option D
  4. Quick Check:

    Override keyword required for abstract method implementation [OK]
Hint: Use override keyword when implementing abstract methods [OK]
Common Mistakes:
  • Omitting override keyword
  • Adding body to abstract method in base class
  • Misunderstanding inheritance rules
5. Given this abstract class and subclass, what will be the output?
abstract class Shape {
    abstract fun area(): Double
    fun description() = "Shape area is ${area()}"
}

class Circle(val radius: Double) : Shape() {
    override fun area() = 3.14 * radius * radius
}

fun main() {
    val c = Circle(2.0)
    println(c.description())
}
hard
A. Shape area is 6.28
B. Shape area is 12.56
C. Shape area is 4.0
D. Compilation error due to abstract method

Solution

  1. Step 1: Calculate area in Circle subclass

    Area = π * radius² = 3.14 * 2.0 * 2.0 = 12.56
  2. Step 2: Call description method from Shape

    Description returns string with area value: "Shape area is 12.56".
  3. Final Answer:

    Shape area is 12.56 -> Option B
  4. Quick Check:

    Abstract method overridden, description uses area() [OK]
Hint: description calls overridden area() method [OK]
Common Mistakes:
  • Forgetting to override abstract method
  • Miscomputing area formula
  • Expecting abstract class method to run alone
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard