Classes
Get introduced to the world of object-oriented programming in Kotlin and learn how to create classes and objects.
StartKey Concepts
Review core concepts you need to learn to master this subject
Classes
Classes
// A class with properties that contain default values
class Student {
var name = "Lucia"
var semester = "Fall"
var gpa = 3.95
}
// Shorthand syntax with no class body
class Student
A class is an object-oriented concept which resembles a blueprint for individual objects. A class can contain properties and functions and is defined using the class
keyword followed by a name and optional body.
If the class does not have a body, its curly braces can be omitted.
Classes
Lesson 1 of 1
- 1Kotlin, much like Java, is an object-oriented programming language. The term, “object-oriented” refers to a language that is made up of objects and can utilize various methodologies such as [encaps…
- 2Now let’s practice writing our own classes. A Kotlin class is declared at the top of a Kotlin file, outside the main() function, with the class keyword followed by its name: class Name { // cla…
- 3Now that we’ve created our first class, let’s create an instance of it. An instance of a class resembles an object that is a member of that given class. Recall the Car class from the previous e…
- 4Looking to customize your instances? You’ve come to the right place! Kotlin classes provide us with something called a primary constructor that allows us to declare properties and initialize them w…
- 5The primary constructor does not contain any code other than the properties and their specified data types. What if we’d like to calculate a person’s age using the year they were born? We can do th…
- 6Now let’s explore how to add even more functionality to our classes with functions. A function declared within a Kotlin class is known as a member function. Member functions possess the same qu…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory