๐ Scenario: You are creating a simple program to represent different types of vehicles. Each vehicle has a way to describe itself, but the exact description depends on the type of vehicle.
๐ฏ Goal: Build an abstract class called Vehicle with an abstract method describe(). Then create two classes Car and Bike that inherit from Vehicle and provide their own description. Finally, print the descriptions of both vehicles.
๐ What You'll Learn
Create an abstract class called Vehicle
Add an abstract method describe() that returns a String
Create a class Car that inherits from Vehicle and implements describe()
Create a class Bike that inherits from Vehicle and implements describe()
Create instances of Car and Bike
Print the result of calling describe() on both instances
๐ก Why This Matters
๐ Real World
Abstract classes help organize code when you have different types of things that share some common behavior but also have their own specific details.
๐ผ Career
Understanding abstract classes and methods is important for designing clean and reusable code in many software development jobs.
Progress0 / 4 steps
1
Create the abstract class
Create an abstract class called Vehicle with an abstract method describe() that returns a String.
Kotlin
Hint
Use the abstract keyword before the class and the method. The method should have no body.
2
Create the Car class
Create a class called Car that inherits from Vehicle and implements the describe() method to return the string "This is a car.".
Kotlin
Hint
Use class Car : Vehicle() to inherit and override fun describe() to provide the method body.
3
Create the Bike class
Create a class called Bike that inherits from Vehicle and implements the describe() method to return the string "This is a bike.".
Kotlin
Hint
Similar to Car, inherit from Vehicle and override describe().
4
Create instances and print descriptions
Create an instance of Car called car and an instance of Bike called bike. Then print the result of calling describe() on both instances.
Kotlin
Hint
Create the instances with val car = Car() and val bike = Bike(). Use println() to print their descriptions.
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
Step 1: Understand abstract class instantiation rules
Abstract classes cannot create objects directly because they are incomplete by design.
Step 2: Check method types in abstract classes
Abstract classes can have both abstract methods (without body) and concrete methods (with body).
Final Answer:
It cannot be instantiated directly and may contain abstract methods. -> Option A
Quick Check:
Abstract class = no direct objects + can have abstract methods [OK]
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
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.
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.
Final Answer:
abstract fun calculate(): Int -> Option A
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
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".
Step 2: Analyze main function output
Creating a Dog object and calling sound() prints "Bark".
Final Answer:
Bark -> Option C
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
Step 1: Check method overriding rules in Kotlin
When a subclass implements an abstract method, it must use the override keyword.
Step 2: Identify missing override keyword
The Car class defines drive() without override, causing a compilation error.
Final Answer:
Missing override keyword in Car's drive method -> Option D
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
Step 1: Calculate area in Circle subclass
Area = ฯ * radiusยฒ = 3.14 * 2.0 * 2.0 = 12.56
Step 2: Call description method from Shape
Description returns string with area value: "Shape area is 12.56".