Class, Inheritance, and Enum Class
In mobile application development, object-oriented programming (OOP) principles such as
classes, inheritance, and enum classes are essential for creating structured and efficient code.
These principles allow for better organization, modularity, and readability, which are crucial for
maintaining large applications (Nicholas et al., 2021).
Class
A class is a blueprint for creating objects, defining their attributes (data) and behaviors
(methods). It encapsulates data and the functions that operate on that data, providing a way to
organize and structure code efficiently (Thomas & Jayanthila Devi, 2021).
Example in Kollin:
kollin
CopyEdit
class Car {
var brand: String
var model: String
var year: Int
// Constructor to initialize attributes
constructor(brand: String, model: String, year: Int) {
[Link] = brand
[Link] = model
[Link] = year
}
// Method to return car details
fun displayInfo(): String {
return "$year $brand $model"
}
}
// Creating an object of Car
val car1 = Car("Toyota", "Corolla", 2022)
println([Link]()) // Output: 2022 Toyota Corolla
In this example, the Car class defines properties such as brand, model, and year, with a method
displayInfo() to return a formatted string (Thomas & Jayanthila Devi, 2021).
Inheritance
Inheritance is an OOP principle where a class (child class) inherits attributes and methods from
another class (parent class), allowing code reuse and extension. It helps in creating a hierarchy of
classes and improves modularity (Nicholas et al., 2021).
Example in Kollin:
kollin
CopyEdit
class ElectricCar : Car { // Inheriting from the Car class
var batteryCapacity: Int
// Constructor to initialize attributes
constructor(brand: String, model: String, year: Int, batteryCapacity: Int)
: super(brand, model, year) { // Call to parent constructor
[Link] = batteryCapacity
}
// Overriding the displayInfo method
override fun displayInfo(): String {
return "${[Link]()}, Battery: $batteryCapacity kWh"
}
}
// Creating an object of ElectricCar
val tesla = ElectricCar("Tesla", "Model 3", 2023, 75)
println([Link]()) // Output: 2023 Tesla Model 3, Battery: 75 kWh
In this example, the ElectricCar class inherits from the Car class and adds the batteryCapacity
attribute. The displayInfo() method is overridden to include the battery information (Thomas &
Jayanthila Devi, 2021).
Enum Class
An Enum class is a special class in which each value is a constant. It allows for defining a fixed
set of values that can be used throughout the application. Enum classes are often used to
represent a collection of related constants like status codes, categories, or types (Nicholas et al.,
2021).
Example in Kollin:
kollin
CopyEdit
enum class VehicleType {
SEDAN, SUV, TRUCK
}
// Using Enum in code
val myVehicle = [Link]
println(myVehicle) // Output: SUV
Here, the VehicleType enum defines three vehicle types. Enums are useful for improving code
readability and maintaining fixed values without the risk of errors from hardcoding values like
strings or integers (Nicholas et al., 2021).
Conclusion
In conclusion, classes, inheritance, and enum classes are fundamental concepts in object-oriented
programming, which help to organize, extend, and simplify code in mobile applications. Classes
serve as templates for creating objects, inheritance allows for the extension of existing
functionality, and enum classes provide a way to handle fixed sets of related constants.
Word Count: 516
References
Nicholas, C., Liu, T., & Zhang, Y. (2021). Development and feasibility of a mobile phone
application designed to support physically inactive employees to increase walking. BMC
Medical Informatics and Decision Making, 21(23). [Link]
Thomas, C. G., & Jayanthila Devi, A., (2021). A Study and Overview of the Mobile App
Development Industry. International Journal of Applied Engineering and Management
Letters (IJAEML), 5(1), 115-130. [Link]