Kotlin Class Properties & Primary Constructor | Android Introduction #18
In this reading, you will explore examples of how classes are used in practice.
Modeling the World
In programming, programmers often try to model the real world. For that, classes with properties that express whatever is important in a system are used.
Let’s say you implement a system to manage a school.
When you think about a school, you may think about students, teachers, subjects and so on. It’s convenient to represent them in your system by using classes.
class Student
class Teacher
class Subject
For those classes, you should specify properties. In your system, you do not need to know everything about each person, only what is relevant to the system. So, you should specify only the data that is necessary for the program to work correctly.
For example, a subject might be defined by the name of the teacher who is teaching it. For a teacher, it might be: name, surname, birthday and work status. Of course, those properties might be completely different in a different system. Let’s assume that your system sends greetings for teachers’ birthdays, and that is why you need the birthday property, but in other systems, you might not be interested in keeping such a value.
class Subject(
val name: String,
val teacher: Teacher,
val isObligatory: Boolean,
)
class Teacher(
val name: String,
val surname: String,
val birthday: String,
val status: String,
)
fun main() {
// I use named arguments convention here
val remigiuszFrost =
Teacher(name = "Remigiusz", surname = "Frost", birthday = "23.07.1987", status = "ACTIVE")
val biology1 = Subject(name = "Biology 1", teacher = remigiuszFrost, isObligatory = true)
println(biology1.isObligatory) // true
println(biology1.teacher.birthday) // 23.07.1987
println(biology1.teacher.status) // ACTIVE
}
Notice that in the above example, in the class Subject you keep the property type Teacher. That’s a popular practice — now someone who has access to an object of type Subject can also read the properties of the teacher teaching this subject.
Every additional property in a class is a cost — the value for those properties needs to be collected, stored and maintained (as a status can change). That is why you should try to limit the number of properties in your classes to the minimum required by your system.
Nevertheless, complex systems tend to grow over time, and their object can also have a lot of properties, all required by the functionalities of this system. It is hard to work on such systems, considering how hard it is to create an instance of a class that has over 20 properties. In some systems, it cannot be avoided, but you can try to postpone it by limiting the number of properties at the start.
The ID Property
In real-life systems, classes often have a property called id. Let’s consider the class below:
class Student(
val id: Int,
val name: String,
val surname: String,
)
The id property uniquely identifies a user.
Let’s say you have two users with the same name and surname, and one of them got a bad grade. If you associate this grade with the student’s name and surname, it might be associated with the incorrect person. That’s why you use student id instead. Because it’s unique, you eliminate the risk of making a mistake.
class Grade(val points: Int, val studentId: Int, val topicId: Int)
class Student(
val id: Int,
val name: String,
val surname: String,
)
You should now know more about how classes are used in practice.