Introduction
Abstract classes and methods are fundamental concepts in object-oriented programming (OOP). An abstract class cannot be instantiated and may contain abstract methods that must be implemented by subclasses. Abstract methods are defined without a body and must be overridden in the subclasses. This chapter will cover the syntax and usage of abstract classes and methods in Kotlin with examples.
Abstract Classes
An abstract class in Kotlin is defined using the abstract
keyword. It can contain both abstract methods (without implementation) and concrete methods (with implementation).
Syntax
abstract class AbstractClass {
abstract fun abstractMethod()
fun concreteMethod() {
// Method implementation
}
}
Example
fun main() {
val circle = Circle(5.0)
println("Area of circle: ${circle.area()}")
val rectangle = Rectangle(4.0, 5.0)
println("Area of rectangle: ${rectangle.area()}")
}
abstract class Shape {
// Abstract method
abstract fun area(): Double
// Concrete method
fun displayShape() {
println("This is a shape.")
}
}
class Circle(val radius: Double) : Shape() {
// Implementing abstract method
override fun area(): Double {
return Math.PI * radius * radius
}
}
class Rectangle(val width: Double, val height: Double) : Shape() {
// Implementing abstract method
override fun area(): Double {
return width * height
}
}
Explanation:
abstract class Shape { ... }
: Defines an abstract classShape
with an abstract methodarea
and a concrete methoddisplayShape
.class Circle(val radius: Double) : Shape() { ... }
: Defines a subclassCircle
that implements thearea
method.class Rectangle(val width: Double, val height: Double) : Shape() { ... }
: Defines a subclassRectangle
that implements thearea
method.
Output:
Area of circle: 78.53981633974483
Area of rectangle: 20.0
Abstract Methods
Abstract methods in an abstract class are defined without a body. Subclasses that inherit from the abstract class must provide implementations for these methods.
Syntax
abstract class AbstractClass {
abstract fun abstractMethod()
}
Example
fun main() {
val dog = Dog()
dog.makeSound()
val cat = Cat()
cat.makeSound()
}
abstract class Animal {
// Abstract method
abstract fun makeSound()
}
class Dog : Animal() {
// Implementing abstract method
override fun makeSound() {
println("Woof!")
}
}
class Cat : Animal() {
// Implementing abstract method
override fun makeSound() {
println("Meow!")
}
}
Explanation:
abstract class Animal { ... }
: Defines an abstract classAnimal
with an abstract methodmakeSound
.class Dog : Animal() { ... }
: Defines a subclassDog
that implements themakeSound
method.class Cat : Animal() { ... }
: Defines a subclassCat
that implements themakeSound
method.
Output:
Woof!
Meow!
Using Abstract Properties
Abstract classes can also have abstract properties, which must be overridden in the subclasses.
Example
fun main() {
val car = Car()
car.drive()
println("Car brand: ${car.brand}")
val bike = Bike()
bike.drive()
println("Bike brand: ${bike.brand}")
}
abstract class Vehicle {
// Abstract property
abstract val brand: String
// Abstract method
abstract fun drive()
}
class Car : Vehicle() {
// Implementing abstract property
override val brand: String = "Toyota"
// Implementing abstract method
override fun drive() {
println("Driving a car.")
}
}
class Bike : Vehicle() {
// Implementing abstract property
override val brand: String = "Honda"
// Implementing abstract method
override fun drive() {
println("Riding a bike.")
}
}
Explanation:
abstract val brand: String
: Defines an abstract propertybrand
in theVehicle
abstract class.override val brand: String = "Toyota"
: Implements the abstract property in theCar
class.override fun drive() { ... }
: Implements the abstract methoddrive
in theCar
class.override val brand: String = "Honda"
: Implements the abstract property in theBike
class.override fun drive() { ... }
: Implements the abstract methoddrive
in theBike
class.
Output:
Driving a car.
Car brand: Toyota
Riding a bike.
Bike brand: Honda
Example Program with Abstract Classes and Methods
Here is an example program that demonstrates various aspects of abstract classes and methods in Kotlin:
fun main() {
// Creating instances of Circle and Rectangle and calculating areas
val circle = Circle(5.0)
circle.displayShape()
println("Area of circle: ${circle.area()}")
val rectangle = Rectangle(4.0, 5.0)
rectangle.displayShape()
println("Area of rectangle: ${rectangle.area()}")
// Creating instances of Dog and Cat and making sounds
val dog = Dog()
dog.makeSound()
val cat = Cat()
cat.makeSound()
// Creating instances of Car and Bike and displaying brands
val car = Car()
car.drive()
println("Car brand: ${car.brand}")
val bike = Bike()
bike.drive()
println("Bike brand: ${bike.brand}")
}
// Abstract class with abstract and concrete methods
abstract class Shape {
abstract fun area(): Double
fun displayShape() {
println("This is a shape.")
}
}
class Circle(val radius: Double) : Shape() {
override fun area(): Double {
return Math.PI * radius * radius
}
}
class Rectangle(val width: Double, val height: Double) : Shape() {
override fun area(): Double {
return width * height
}
}
// Abstract class with abstract method
abstract class Animal {
abstract fun makeSound()
}
class Dog : Animal() {
override fun makeSound() {
println("Woof!")
}
}
class Cat : Animal() {
override fun makeSound() {
println("Meow!")
}
}
// Abstract class with abstract property and method
abstract class Vehicle {
abstract val brand: String
abstract fun drive()
}
class Car : Vehicle() {
override val brand: String = "Toyota"
override fun drive() {
println("Driving a car.")
}
}
class Bike : Vehicle() {
override val brand: String = "Honda"
override fun drive() {
println("Riding a bike.")
}
}
Output:
This is a shape.
Area of circle: 78.53981633974483
This is a shape.
Area of rectangle: 20.0
Woof!
Meow!
Driving a car.
Car brand: Toyota
Riding a bike.
Bike brand: Honda
Conclusion
In this chapter, you learned about abstract classes and methods in Kotlin, including how to define abstract classes, implement abstract methods, and use abstract properties. Abstract classes and methods are essential for defining templates for subclasses and achieving a higher level of abstraction in your code. Understanding and applying these concepts is crucial for writing robust and maintainable Kotlin programs.