Classes and Member Functions
Explore the fundamentals of classes and member functions.
Introduction to classes
If we take a look at the world around us, we will likely notice plenty of objects. It might be a book, an e-book reader, a monitor, or a mug of coffee. We are surrounded by objects. This idea leads to the conclusion that we are living in a world of objects; therefore, our programs should be constructed in the same way. This is the conceptual basis of the object-oriented programming (OOP) approach. Not everyone shares this worldview—some prefer to see the world as a place of possible actions, which is the conceptual basis of the functional programming (FP) approach. But whichever approach we prefer, classes and objects are important structures in Kotlin programming.
What is a class?
A class is a template used to create an object with concrete characteristics. To create a class in Kotlin, we use the class
keyword followed by the name. This is all that is needed to create the simplest class because a class body is optional. To create an object, which is an instance of a class, we use the default constructor function, which is the class name and parentheses. Unlike in other languages like C++ or Java, we do not use the new
keyword in Kotlin.
Get hands-on with 1200+ tech skills courses.