Left Index

Kotlin Nested Class

A class is declared within another class then it is called as nested class. By default nested class is static so we can access the nested class property or variables using dot(.) notation without creating object of the class.

Syntax:
class ClassName1 {
	// ... properties and member functions

	// nested class 1	
	class ClassName2 {
		// ... properties and member functions
		
		// nested class 2
		class ClassName3 {
			// ... properties and member functions
		}
	} 
}

Let's have an example to create nested class. We are going to create class for Car and it has a nested class i.e. Engine and in Class Car we define model, color and varient and its inner class we define fuelType, mileage and maxPowerInBHP.

Example:
class Car {
	var model = "Fortuner 2018"
	var color = "While"
	var varient = "4x4 AT"

	// nested class 1	
	class Engine {
		var fuelType = "Diesel"
		var milage = 15.04f
		var maxPowerInBHP = 174.5
	}
}

Note: Nested class can not access the members of outer class, but we can access the property of nested class from the outer class without creating object for nested class.


Accessing nested class properties
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap

fun main(args: Array<String>) {
	println("Car model: " + Car().model)
	println("Car Fuel type: " + Car.Engine().fuelType)
}

class Car {
	var model = "Fortuner 2018"
	var color = "While"
	var varient = "4x4 AT"

	// nested class 1	
	class Engine {
		var fuelType = "Diesel"
		var milage = 15.04f
		var maxPowerInBHP = 174.5
	}
}
Example:
Car model: Fortuner 2018
Car Fuel type: Diesel

Accessing nested class member function
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap

fun main(args: Array<String>) {
	Car().displayCarInfo()
}

class Car {
	var model = "Fortuner 2018"
	var color = "While"
	var varient = "4x4 AT"
	
	// nested class 1	
	class Engine {
		var fuelType = "Diesel"
		var milage = 15.04f
		var maxPowerInBHP = 174.5
	}
	
	fun displayCarInfo() {
		println("Model: " + model)
		println("Color: " + color)
		println("Varient: " + varient)
		
		println()
		
		var engine = Engine()
		println("Fuel Type: " + engine.fuelType)
		println("Milage: " + engine.milage)
		println("Power: " + engine.maxPowerInBHP + "@bhp")
	}
}
Output:
Model: Fortuner 2018
Color: While
Varient: 4x4 AT

Fuel Type: Diesel
Milage: 15.04
Power: 174.5bhp

Inner Class

In Kotlin if we want to access outer class properties, we get error i.e. unresolved error like shown in image below

To resolve above problem, we use inner class

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList
import java.util.HashMap
import com.sun.xml.internal.ws.api.pipe.Engine

fun main(args: Array<String>) {
	Car().displayCarInfo()
	
	var innerDemo = Car().Engine()
	innerDemo.nestedFun()
}

class Car {
	var model = "Fortuner 2018"
	var color = "While"
	var varient = "4x4 AT"
	
	// nested class 1	
	inner class Engine {
		var fuelType = "Diesel"
		var milage = 15.04f
		var maxPowerInBHP = 174.5
		
		fun nestedFun() {
			println("\nThis is inner class method. And Car Model: " + model)
		}
	}
	
	fun displayCarInfo() {
		println("Model: " + model)
		println("Color: " + color)
		println("Varient: " + varient)
		
		println()
		
		var engine = Engine()
		println("Fuel Type: " + engine.fuelType)
		println("Milage: " + engine.milage)
		println("Power: " + engine.maxPowerInBHP + "@bhp")
	}
}
Output:
Model: Fortuner 2018
Color: While
Varient: 4x4 AT

Fuel Type: Diesel
Milage: 15.04
Power: 174.5@bhp

This is inner class method. And Car Model: Fortuner 2018